我试图在Python中用类创建按钮,但是当运行它时,按钮不会出现。以下是我的代码
#Button_2
#Using Classes
from Tkinter import *
class Application(Frame):
"""A GUI application with three button"""
def _init_(self, master):
""" Initialise the Frame. """
Frame._init_(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
#"""Create three buttons"""
#Create first buttom
self.btn1 = Button(self, text = "I do nothing")
self.btn1.grid()
#Create second button
self.btn2 = Button(self)
self.btn2.grid()
self.btn2.configure(text = "T do nothing as well")
#Create third button
self.btn3 = Button(self)
self.btn3.grid()
self.btn3.configure(text = "I do nothing as well as well")
root = Tk()
root.title("Lazy Button 2")
root.geometry("500x500")
app = Application(root)
root.mainloop()
任何帮助都将不胜感激
好的,第一个问题是您已经声明了以下代码:
在班级内部。它应该在外部,所以这是一个缩进问题(可能是缩进的stackoverflow问题?)。
其次,我简化了代码以使其运行
这是一个起点,肯定可以证明如下:
你可以使用grid()而不是pack并从definit构造函数调用方法。希望有帮助。
此调用方法也可以工作:
我最后的尝试也有效:
接下:
最终代码:
您需要将“构造函数”方法命名为
__init__
,而不是_init_
。在本文中,您的grid
和create_widgets
方法从未被调用,因为_init_
从未被调用。相关问题 更多 >
编程相关推荐