2017-04-11 92 views
0

我很新的python和一般编码。我正在尝试制作游戏的应用程序,但似乎无法销毁我创建的框架。Python Tkinter框架销毁

from tkinter import * 


class application: 

    def __init__(self,parent): 
     self.startContainer = Frame(parent) 
     self.startContainer.pack() 

     self.lbl = Label(startContainer,text="Please choose from the following: \nFaith Points (F) \nBanqueting Goods (B) \nEnter Honour (E) ") 
     self.lbl.pack() 

     self.btn1 = Button(startContainer,text="Votes",command=self.votes(startContainer)).pack() 
     self.btn2 = Button(startContainer,text="Gold Tithe",command=self.gold(startContainer)).pack() 

    def votes(parent,self): 
     parent.destroy() 

    def gold(parent,self): 
     pass 


window = Tk() 
app = application(window) 
window.title("Tools") 
window.geometry("425x375") 
window.wm_iconbitmap("logo.ico") 
window.resizable(width=False, height=False) 
window.mainloop() 
+1

定义方法时,第一个参数应该是'self'。这适用于投票方式和金牌方式。请参阅此链接(http://stackoverflow.com/questions/2709821/what-is-the-purpose-of-self)以获取更多信息。请注意,“self”的命名仅仅是一个约定,而不是一个保留字。 – arrethra

回答

0

您在主窗口内的窗口小部件上调用destroy。 调用“父”的销毁。将父类转换为self.parent:调用self.votes()时,实际上是调用函数,并且在打开函数之前将销毁该函数。

此外,尝试构建功能参数的类中(个体经营,...)不(...,个体经营)

from tkinter import * 


class application: 

    def __init__(self,parent): 
     self.parent = parent 
     startContainer = Frame(parent) 
     startContainer.pack() 

     self.lbl = Label(startContainer,text="Please choose from the following: \nFaith Points (F) \nBanqueting Goods (B) \nEnter Honour (E) ") 
     self.lbl.pack() 

     self.btn1 = Button(startContainer,text="Votes", command=self.votes).pack() 
     self.btn2 = Button(startContainer,text="Gold Tithe",command=self.gold(startContainer)).pack() 

    def votes(self): 
     print("test") 
     self.parent.destroy() 

    def gold(self, parent): 
     pass 


window = Tk() 
app = application(window) 
window.mainloop()