如何触发 Tkinter 命令中按钮的更改?
How to trigger changes in button in a comand in Tkinter?
我是 TKinter
的新手。我需要在单击时更改 button
及其 state
的文本,然后执行一些操作,最后再次更改其文本和状态。
问题是更改仅在函数结束后应用,跳过了状态和文本的第一次更改。它永远不会将 Buttons
文本更改为 "loading" 并且按钮永远不会被禁用。
这是我遇到的问题的代码:
#!/usr/bin/env python
import tkinter as tk
import time
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack(fill=tk.BOTH, expand=1)
self.create_widgets()
def create_widgets(self):
self.master.title("CW POS")
cierre = tk.Button(
self.master,
command=self.re_imprimir_ultimo_cierre)
cierre["text"] = "foo"
cierre.pack(fill=tk.BOTH, expand=1)
self._cierre = cierre
salir = tk.Button(self.master, text="quit", command=self.salir)
salir.pack(fill=tk.BOTH, expand=1)
def salir(self):
exit()
def re_imprimir_ultimo_cierre(self):
self._cierre["text"] = "Loading..."
self._cierre["state"] = tk.DISABLED
# TODO: magic
time.sleep(2)
self._cierre["text"] = "foo"
self._cierre["state"] = tk.NORMAL
root = tk.Tk()
root.geometry("240x180")
root.resizable(False, False)
app = Application(root)
root.mainloop()
如何在按钮进行计算时让按钮显示 text="loading"
和 state=DISABLED
?
这个问题有一个非常快速的修复方法,您只需更新按钮,将其文本更改为 "Loading" (self._cierre["text"] = "Loading..."
)
def re_imprimir_ultimo_cierre(self):
self._cierre["text"] = "Loading..."
self._cierre["state"] = tk.DISABLED
self._cierre.update() # This is the line I added
# TODO: magic
time.sleep(2)
self._cierre["text"] = "foo"
self._cierre["state"] = tk.NORMAL
这只是在您更改文本和状态后更新按钮状态。
From what I understand this is because a button will run all the code within its command, before updating anything on the screen, so you essentially have to force the button to update itself within its command.
希望这对您有所帮助:)
我是 TKinter
的新手。我需要在单击时更改 button
及其 state
的文本,然后执行一些操作,最后再次更改其文本和状态。
问题是更改仅在函数结束后应用,跳过了状态和文本的第一次更改。它永远不会将 Buttons
文本更改为 "loading" 并且按钮永远不会被禁用。
这是我遇到的问题的代码:
#!/usr/bin/env python
import tkinter as tk
import time
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack(fill=tk.BOTH, expand=1)
self.create_widgets()
def create_widgets(self):
self.master.title("CW POS")
cierre = tk.Button(
self.master,
command=self.re_imprimir_ultimo_cierre)
cierre["text"] = "foo"
cierre.pack(fill=tk.BOTH, expand=1)
self._cierre = cierre
salir = tk.Button(self.master, text="quit", command=self.salir)
salir.pack(fill=tk.BOTH, expand=1)
def salir(self):
exit()
def re_imprimir_ultimo_cierre(self):
self._cierre["text"] = "Loading..."
self._cierre["state"] = tk.DISABLED
# TODO: magic
time.sleep(2)
self._cierre["text"] = "foo"
self._cierre["state"] = tk.NORMAL
root = tk.Tk()
root.geometry("240x180")
root.resizable(False, False)
app = Application(root)
root.mainloop()
如何在按钮进行计算时让按钮显示 text="loading"
和 state=DISABLED
?
这个问题有一个非常快速的修复方法,您只需更新按钮,将其文本更改为 "Loading" (self._cierre["text"] = "Loading..."
)
def re_imprimir_ultimo_cierre(self):
self._cierre["text"] = "Loading..."
self._cierre["state"] = tk.DISABLED
self._cierre.update() # This is the line I added
# TODO: magic
time.sleep(2)
self._cierre["text"] = "foo"
self._cierre["state"] = tk.NORMAL
这只是在您更改文本和状态后更新按钮状态。
From what I understand this is because a button will run all the code within its command, before updating anything on the screen, so you essentially have to force the button to update itself within its command.
希望这对您有所帮助:)