退出python中的主循环 [英] quit mainloop in python
本文介绍了退出python中的主循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
虽然我是其他语言的实验程序员,但我对Python非常陌生.我一直在尝试做一个非常简单的事情,就是在启动后退出主循环.这似乎是一件大事.下面的程序仅生成一系列事件.似乎一切正常,但我无法关闭最终窗口...我该怎么办?
Although I am a kind of experimented programmer in other languages, I am very new in Python. I have been trying to do a very simple thing that is to quit the mainloop after starting. It seems that it is a big deal. The program below only makes a sequence of events. Everything seems to be working, but I am not able to close the final window... What should I do?
from Tkinter import *
root=Tk()
theMainFrame=Frame(root)
theMainFrame.pack()
class CloseAfterFinishFrame1(Frame): # Diz que herda os parametros de Frame
def __init__(self):
Frame.__init__(self,theMainFrame) # Inicializa com os parametros acima!!
Label(self,text="Hi",font=("Arial", 16)).pack()
button = Button (self, text = "I am ready", command=self.CloseWindow,font=("Arial", 12))
button.pack()
self.pack()
def CloseWindow(self):
self.forget()
CloseAfterFinishFrame2()
class CloseAfterFinishFrame2(Frame): # Diz que herda os parametros de Frame
def __init__(self):
Frame.__init__(self,theMainFrame) # Inicializa com os parametros acima!!
Label(self,text="Hey",font=("Arial", 16)).pack()
button = Button (self, text = "the End", command=self.CloseWindow,font=("Arial", 12))
button.pack()
self.pack()
def CloseWindow(self):
self.forget()
CloseEnd()
class CloseEnd():
theMainFrame.quit()
CloseAfterFinishFrame1()
theMainFrame.mainloop()
推荐答案
调用root.quit()
,而不是theMainFrame.quit
:
import Tkinter as tk
class CloseAfterFinishFrame1(tk.Frame): # Diz que herda os parametros de Frame
def __init__(self, master):
self.master = master
tk.Frame.__init__(self, master) # Inicializa com os parametros acima!!
tk.Label(self, text="Hi", font=("Arial", 16)).pack()
self.button = tk.Button(self, text="I am ready",
command=self.CloseWindow, font=("Arial", 12))
self.button.pack()
self.pack()
def CloseWindow(self):
# disable the button so pressing <SPACE> does not call CloseWindow again
self.button.config(state=tk.DISABLED)
self.forget()
CloseAfterFinishFrame2(self.master)
class CloseAfterFinishFrame2(tk.Frame): # Diz que herda os parametros de Frame
def __init__(self, master):
tk.Frame.__init__(self, master) # Inicializa com os parametros acima!!
tk.Label(self, text="Hey", font=("Arial", 16)).pack()
button = tk.Button(self, text="the End",
command=self.CloseWindow, font=("Arial", 12))
button.pack()
self.pack()
def CloseWindow(self):
root.quit()
root = tk.Tk()
CloseAfterFinishFrame1(root)
root.mainloop()
此外,如果您只想调用函数root.quit
,则无需创建CloseEnd
类.
Also, there is no need to make a class CloseEnd
if all you want to do is call the function root.quit
.
这篇关于退出python中的主循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文