AttributeError:'模块'对象没有属性' tk'

时间:2014-03-21 21:11:53

标签: python user-interface tkinter

嘿伙计们我试图用tkinter创建一个简单的程序,现在我想要它做的就是打开一个空白的GUI。目前我收到此错误:

Traceback (most recent call last):
  File "C:/Users/User/Desktop/firstTkinter.py", line 3, in <module>
    class simpleapp_tk(tkinter.tk):
AttributeError: 'module' object has no attribute 'tk'

这就是我写的代码:

import tkinter

class simpleapp_tk(tkinter.tk):

    def __init__(self,parent):
        tkinter.tk.__init__(self,parent)
        self.parent=parent
        self.initialize()

    def initialize(self):
        'best to have a portion of code that creates all gui elements.  This is the point of initialize()'
        pass

    if __name__=='__main__':
        app=simpleapp_tk(None)
        app.title('my application')
        app.mainloop()

1 个答案:

答案 0 :(得分:2)

它是Tk(),而不是tk()

除非你准确地给出它,否则Python将无法识别该名称。因此,当您输入后者时,找不到该对象,当然,解释器会给您一个错误,让您知道您尝试访问的属性不存在。