Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

quit mainloop in python

Tags:

python

tkinter

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()
like image 745
DanielTheRocketMan Avatar asked Feb 12 '13 17:02

DanielTheRocketMan


1 Answers

Call root.quit(), not 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()

Also, there is no need to make a class CloseEnd if all you want to do is call the function root.quit.

like image 142
unutbu Avatar answered Oct 29 '22 20:10

unutbu