使用Python中的Tkinter创建一个简单的gui程序

时间:2015-12-05 17:52:59

标签: python-3.x tkinter

不明白为什么这个简单的代码不起作用。提前抱歉我的愚蠢。我有这样的错误:

Traceback (most recent call last):
  File "C:/python/coding/simple_gui", line 28, in <module>
     app=Application(root)
  File "C:/python coding/simple_gui", line 9, in __init__
    self.create_widgets()
  File "C:/python coding/simple_gui", line 14, in create_widgets
    self.bttnl.grid()
AttributeError: 'Application' object has no attribute 'bttnl'

我的代码是:

from tkinter import *


class Application(Frame):
    def __init__(self, master):
        """ Initialize frame"""
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()
    def create_widgets(self):
        """Create 3 useless buttons"""
        #first one
        self.bttn1=Button(self, text ="I do nothing!")
        self.bttnl.grid()
        #second button
        self.bttn2 = Button(self)
        self.bttn2.grid()
        self.bttn2.configure(text ="Me too!")
        #third one
        self.bttn3 = Button(self)
        self.bttn3.grid()
        self.bttn3["text"]="And me also!"

root=Tk()
#alter window
root.title("The simpliest gui")
root.geometry("200x100")
app=Application(root)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

您的代码中有拼写错误。你在第14行写了 bttnl 而不是 bttn1

from tkinter import *


class Application(Frame):
    def __init__(self, master):
        """ Initialize frame"""
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()
    def create_widgets(self):
        """Create 3 useless buttons"""
        #first one
        self.bttn1=Button(self, text ="I do nothing!")
        self.bttn1.grid()
        #second button
        self.bttn2 = Button(self)
        self.bttn2.grid()
        self.bttn2.configure(text ="Me too!")
        #third one
        self.bttn3 = Button(self)
        self.bttn3.grid()
        self.bttn3["text"]="And me also!"

root=Tk()
#alter window
root.title("The simpliest gui")
root.geometry("200x100")
app=Application(root)
root.mainloop()