【问题标题】:changing states of button in tkinter by self values通过 self 值更改 tkinter 中按钮的状态
【发布时间】:2020-01-23 04:56:54
【问题描述】:

我有一个类 GUI 创建一个带有几个小部件的 tk 窗口 我希望保存按钮的状态根据一些自我的值而改变

在def init里面:

btn_save['state']='disabled'
if self.basepath and (self.variable.get() == 0 or self.variable.get() =='1) and (self.secondOpt.get() == '0 or self.secondOpt.get() == 1):
        btn_save['state']='normal' #here I want to change the save button state

主程序:

window = Tk()
my_gui = GUI(window)
window.mainloop()

我该怎么做?

【问题讨论】:

    标签: python user-interface button tkinter state


    【解决方案1】:

    使用after方法如下:

    def state_change():
    '''your code here'''
    if self.basepath and (self.variable.get() == 0 or self.variable.get() =='1) and (self.secondOpt.get() == '0 or self.secondOpt.get() == 1):
        btn_save['state']='normal' #here I want to change the save button state
        root.after(100, state_change) #if you want to keep changing the state of the button
    root.after(100, state_change)
    

    使用Button 小部件专门更改它(您不需要的东西)

    【讨论】:

    • 所以我需要通过命令将其绑定到小部件,还是应该将此方法插入几个小部件中?
    • 这可以在__init__ 方法中添加。使回调方法成为类方法。仅当您对它感到满意时才嵌套方法
    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多