python - 返回 __get attr__ 的 Tkinter 错误

标签 python python-2.7 tkinter

我是 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()

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

最佳答案

你需要调用父类(super class)的__init__方法:

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

除此之外,还有 undefined variable CheckVar2var

关于python - 返回 __get attr__ 的 Tkinter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25702512/

相关文章:

Python 滚动文本模块

python - pd to_datetime 不将 DDMMMYYYY 日期转换为 python 中的日期时间

python - Scrapy - 我在哪里可以在 Linux Ubuntu E4.5 上找到它

python - 如何将分类数据从 numpy 数组加载到指标或嵌入列中?

python - 用实例方法中的 float 替换实例?

python - 当我尝试将 excel 文件转换为列表时,“DataFrame”对象没有属性 'tolist'

linux - tkinter 复制粘贴到 Entry 不会删除选定的文本

python - 如何避免在 python 中创建对象?

python - 从 find_element 查询中提取数据 (Python)

python - 找出最长的连续数字系列的长度