在下面的代码中,我想在按下GameButton按钮时销毁窗口根目录中的所有内容,但是我希望其他事情发生,所以唯一的方法就是让按钮运行一个函数。当我表演的时候自我毁灭在主类之外,没有任何内容被删除,有什么办法吗?在
from PIL import Image, ImageTk
from Tkinter import Tk, Label, BOTH,W, N, E, S, Entry, Text, INSERT, Toplevel
from ttk import Frame, Style, Button, Label
import Tkinter
import Callingwordlist
difficulty = ""
class MainMenuUI(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Type!")
self.pack(fill=BOTH, expand=1)
style = Style()
style.configure("TFrame", background="black")
Logo = Image.open("Type!.png")
TypeLogo = ImageTk.PhotoImage(Logo)
label1 = Label(self, image=TypeLogo)
label1.image = TypeLogo
label1.place(x=342,y=80)
label1.pack()
self.pack(fill=BOTH, expand=1)
GameButton = Button(self, text="Main Game", command=lambda: main2(self.parent,self))
GameButton.pack()
GameButton.place(x=344,y=200,height = 80,width = 176)
TutorialButton = Button(self,text="Tutorial Level")
TutorialButton.pack()
TutorialButton.place(x=344, y=300 ,height = 80,width = 176)
quitbutton = Button(self, text= "Quit",command=self.parent.destroy)
quitbutton.place(x=344, y=400,height = 80,width = 176)
def main2(root,self):
self.destroy
app = MainGameUI(root)
root.mainloop()
class MainGameUI(root):
....
def main():
root = Tk()
root.geometry("860x640+300+300")
app = MainMenuUI(root)
root.mainloop()
if __name__ == '__main__':
main()
你实际上并没有破坏你函数中的任何东西。看看这个代码:
注意函数的第一行,您试图销毁所有内容。您的代码是
self.destroy
-注意缺少括号。实际上,您并不是在调用函数,而是在引用它。添加圆括号称之为:self.destroy()
。在另一个问题是调用的函数会破坏调用该函数的小部件。但是,这个函数进入一个无休止的循环(
mainloop()
),因此button命令永远不会返回。我不太确定这里会发生什么,你可能会得到一些错误。底线是,从按钮命令调用mainloop
不是一个好主意。在由于你正在构建你的应用程序,使其成为一个框架(而不是根窗口),你不需要重新启动事件循环。销毁
MainMenuUI
小部件时,事件循环将继续运行。没有必要重新启动它。在相关问题 更多 >
编程相关推荐