【问题标题】:quit mainloop in python在python中退出主循环
【发布时间】:2015-08-27 08:41:40
【问题描述】:

虽然我是一种在其他语言方面经验丰富的程序员,但我对 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()

【问题讨论】:

  • 你可以使用root .withdraw()

标签: python tkinter


【解决方案1】:

致电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

【讨论】:

  • 谢谢!但是在按下按钮 theEnd 后,程序就卡住了!我正在使用 python 2.7.3!顺便问一下,将 Tkinter 定义为 tk 有什么好处?我看到其他人也这样做,但我不明白原因!
  • 您使用的是 IDE 吗?如果是这样,请尝试从命令行运行脚本:python script.py。我认为它应该可以正常工作。
  • 虽然有些人可能会争辩 import * from Tkinter 没问题 - 事实上,我认为 Tkinter 的作者将其设计为以这种方式导入 - 一般应该仅在交互式会话中使用import * ...,而不是在脚本中。通过使用更详细的import Tkinter as tk,您可以准确地阐明每个对象的来源。这有助于调试或阅读其他人的代码,并且在两个模块使用相同名称时防止名称冲突。例如,numpy.sum 与内置 Python sum 不同。 import * from numpy 会很糟糕。
  • 你是对的。我用的是闲置。现在可以了!我真的很感激
猜你喜欢
  • 1970-01-01
  • 2013-05-15
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多