2016-11-16 42 views
-1

我需要有一个最初禁用的按钮,以后启用事件。我有一个相当大的应用程序正在工作,除非我无法将状态更改为按钮。请不要告诉我,我需要彻底重塑代码。这是离开我使用的显着结构的所有代码中的一条。 ReadPort实际上是从'after'后面调用的,但是在这里按下按钮模拟它。如何将画布按钮状态设置为正常(Python 3)

我已阅读关于此主题的所有帮助,并尝试了答案。每个产生的Python语句和错误都是在这个完整的应用程序中,在所有尝试改变按钮状态的尝试中都失败了。请让我知道如何解决这个问题。

#!/usr/bin/python 3 

from tkinter import * 

def ReadPort(): 
    global VSM_Button 

# AttributeError: 'NoneType' object has no attribute 'config' 
## VSM_Button.config(state="normal") 

# AttributeError: 'NoneType' object has no attribute 'configure' 
## VSM_Button.configure(state="normal") 

# TypeError: 'NoneType' object does not support item assignment 
## VSM_Button['state'] = 'normal' 

# AttributeError: 'NoneType' object has no attribute 'configure' 
## VSM_Button.configure(state=NORMAL) 

# ??? How do I set the button state to 'normal' ? 

pass 

class Application: 
    def __init__(self, master): 
    #global VSM_Button # seems unnecessary. Same errors in or out. 
    frame = Frame(master) 
    frame.pack() 
    Button(frame, text='Press Me', width = 10, command=ReadPort).pack() 
    VSM_Button = Button(frame, text='Disabled', width = 10, state = DISABLED).pack() 

    pass # end def __init__ 
pass # end class Application 

root = Tk() 
root.wm_title('Button') 
app = Application(master=root) 
root.mainloop() 
+0

本网站上可能有数百个问题与答案有关,错误“NoneType”对象没有任何属性。 –

回答

0
VSM_Button = Button(frame, text='Disabled', width = 10, state = DISABLED).pack() 

你没有按钮本身分配给VSM_Button;您分配了调用pack()的结果,即None。您需要在作业的单独一行中完成此包。

global VSM_Button声明是绝对必需的。没有它,就不可能访问Application.__init__()以外的按钮。

+0

将定义分解为2个语句是必要的。我昨天意识到。仍然让所有这些细节或这(有时是棘手的)语言进入我的脑海。所以我将.grid语句分隔开来,并命名为Global。我把它放在顶部,但那不太好。我只是把它放在创建按钮的例程中,并将它们连接在一起。仍然习惯于我用过的其他语言的变化。感谢大家在这里分享有用的信息。我们的Python新手很欣赏它。 –