我是 Python 的新手,甚至是 GUI 编程的新手。
我有一个按钮和两个旋转框,我想在单击开始按钮后禁用它们。 我用谷歌搜索了 5 种不同的方法来禁用 Tkinter 按钮,但似乎都不起作用。理论上应该以相同的方式禁用旋转框,但我只是运气不好。 对整个 GUI 的事情感到非常沮丧。
self.gps_com 和 self.arduino_com 是两个 spinboxes
如您所见,我尝试对按钮使用 update(),但它不起作用。我已经看到在所有大写、首字母大写和引号的不同变体中使用 disabled 的代码变体。当前语法不会给出来自 Spyder 的警告/错误。
我原以为很容易找到这个问题的答案,但我现在已经花了几个小时了。
def OnButtonClickStart(self):
self.labelVariable.set( self.entryVariable.get())
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
self.button_start.config(state = 'DISABLED')
self.button_start.update()
self.gps_com.config(state = 'DISABLED')
self.arduino_com.config(state = 'DISABLED')
请您参考如下方法:
尝试这段代码,看看文本是否更新,按钮是否按预期禁用/重新启用:
import tkinter as tk
class Window():
def __init__(self, root):
self.frame = tk.Frame(root)
self.frame.grid()
self.i = 0
self.labelVar = tk.StringVar()
self.labelVar.set("This is the first text: %d" %self.i)
self.label = tk.Label(self.frame, text = self.labelVar.get(), textvariable = self.labelVar)
self.label.grid(column = 0, row = 0)
self.button = tk.Button(self.frame, text = "Update", command = self.updateLabel)
self.button.grid(column = 1, row = 0)
self.enableButton = tk.Button(self.frame, text = "Enable Update Button", state = 'disabled', command = self.enable)
self.enableButton.grid(column = 2, row = 0)
def updateLabel(self):
self.i += 1
self.labelVar.set("This is the new text: %d" %self.i)
self.button.config(state = 'disabled')
self.enableButton.config(state = 'active')
def enable(self):
self.button.config(state = 'active')
self.enableButton.config(state = 'disabled')
root = tk.Tk()
window = Window(root)
root.mainloop()
如果这行得通,那么您要么 a) 使用了错误的关键字('disabled',全部小写,在 Python 2.5/3.4(我昨晚测试了 3.4))要么 b) 函数您尝试调用的方法没有按照 tobias_k 的建议正确执行。
