Tkinter按钮禁用无效

时间:2015-06-18 22:23:28

标签: python tkinter

我是Python的新手,甚至是GUI编程的新手。

单击开始按钮后,我有一个按钮和两个要禁用的旋转框。 我用Google搜索了5种不同的方法来禁用Tkinter按钮,但似乎都没有。从理论上讲,旋转箱应该以相同的方式禁用,但我只是没有运气。 对整个GUI事情感到非常沮丧。

self.gps_comself.arduino_com是两个旋转框

正如您所看到的,我尝试使用update()作为按钮,但它不起作用。我已经看到了在所有大写字母中使用禁用的代码的变体,首字母大写以及不同的引号变体。这种当前语法不会给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')

1 个答案:

答案 0 :(得分:1)

尝试这段代码,看看文字是否更新&按钮按预期禁用/重新启用:

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建议的那样正确执行。