2013-02-12 101 views
5

尽管我是其他语言的一种实验程序员,但我在Python中很新。我一直试图做一个非常简单的事情,就是在开始后退出主循环。这似乎是一个大问题。下面的程序只会产生一系列事件。一切似乎都在工作,但我无法关闭最后的窗口......我该怎么办?在Python中退出主循环

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() 
+0

你可以使用'root .withdraw()' – user19911303 2013-02-15 10:32:02

回答

4

呼叫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() 

而且,没有必要使一个类CloseEnd如果你想要做的就是调用该函数root.quit

+0

谢谢!但按下按钮后,程序就卡住了!我正在使用python 2.7.3!顺便说一下,将Tkinter定义为tk有什么优势?我看到其他人也这样做,但我不明白原因! – DanielTheRocketMan 2013-02-12 18:13:20

+0

您使用的是IDE吗?如果是这样,请尝试从命令行运行脚本:'python script.py'。我认为它应该可以正常工作。 – unutbu 2013-02-12 18:20:44

+1

虽然有些人可能会争辩说,从Tkinter导入*是可以的 - 事实上,我认为'Tkinter'的作者设计它是以这种方式导入的 - *一般来说*只能使用'import * ...'在交互式会话中,而不是在脚本中。通过使用更加详细的“导入Tkinter作为tk”,您可以清楚地确定每个对象来自哪里。这有助于调试或阅读其他人的代码,并防止两个模块使用相同名称时的名称冲突。例如,'numpy.sum'与内建的Python'sum'不同。 '从numpy'导入*会很糟糕。 – unutbu 2013-02-12 18:27:05