MyApp“实例没有属性'var'”错误

时间:2015-04-16 18:48:47

标签: python tkinter

我有一个应用程序,用户应该选择一个选项,然后按下按钮后将打印该值。但是,当我尝试运行此错误时,我收到错误AttributeError: MyApp instance has no attribute 'var'。有什么想法吗?

class MyApp(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)  

        self.execute = Button(root, text="Convert", command=self.convert())
        self.execute.grid(row=4, column=3, pady=9, padx=3)

        self.var = StringVar()
        rb1 = Checkbutton(root, text="Path", variable=self.var, offvalue='marker', onvalue='path')#, value='path'
        rb1.grid(row=4, column=1, padx=3, sticky=W)
        rb2 = Checkbutton(root, text="Markers", variable=self.var, offvalue='path', onvalue='marker')
        rb2.grid(row=5, column=1, padx=3, sticky=W)

    def convert(self):
        print self.var.get()

1 个答案:

答案 0 :(得分:3)

问题在于这一行:

self.execute = Button(root, text="Convert", command=self.convert())

由于结尾self.convert,您正在呼叫(),此方法取决于self.var,但您尚未设置self.var

您需要删除()

self.execute = Button(root, text="Convert", command=self.convert)