【问题标题】:Tkinter error to return __get attr__Tkinter 错误返回 __get attr__
【发布时间】:2014-09-06 16:48:53
【问题描述】:

我是 python 新手,我尝试了一个 Gui 应用程序,但它导致错误如下:

错误:

Traceback (most recent call last):
  File "C:\Python27\aqw.py", line 22, in <module>
    app = myproject(None,None)
  File "C:\Python27\aqw.py", line 8, in __init__
    self.button()
  File "C:\Python27\aqw.py", line 13, in button
    button = Tkinter.Button(self,text=u"Succedd !")
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2106, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2027, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2000, in _setup
    if not master:
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1826, in __getattr__
    return getattr(self.tk, attr)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1826, in __getattr__
    return getattr(self.tk, attr)

请帮我修复我的代码!

我的代码:

import Tkinter
from Tkinter import *

class myproject(Tkinter.Tk):
   def __init__(self,parent, master):
      self.button()
      self.checkbox()
      self.radiobutton()
   def button(self):
        #add quit button
      button = Tkinter.Button(self,text=u"Succedd !")                                                               
      button.grid(column=3,row=1)
   def checkbox(self):
      checkbox = Checkbutton(self, text = "Music", variable = CheckVar2)
      checkbox.grid(column=3,row=1)
   def radiobutton(self):
      radiobutton = Tkinter.Radiobutton(self, text="Option 2", variable=var, value=2)


app = myproject(None,None)
app.mainloop()

请帮忙!我们将不胜感激!

【问题讨论】:

  • 请发布错误的完整回溯,而不仅仅是最后一行。

标签: python python-2.7 tkinter


【解决方案1】:

你需要调用超类'__init__方法:

class myproject(Tkinter.Tk):
   def __init__(self, parent, master):
      Tkinter.Tk.__init__(self) # <----
      self.button()
      self.checkbox()
      self.radiobutton()
   ...

除此之外,还有未定义的变量CheckVar2var

【讨论】:

  • 如果我只提供Tkinter.Tk.__init__(self) 而不给父母,那么它就可以工作。这是避免在超类方法中使用除 self 以外的参数的好习惯
  • @adsqwqwe,我相应地更新了答案。试试help(Tkinter.Tk)Tkinter.Tk.__init__ 不带parentmaster 参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 2017-10-13
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多