quit mainloop in python
Asked Answered
P

2

5

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()
Pastoral answered 12/2, 2013 at 17:51 Comment(1)
you can use root .withdraw()Belated
T
9

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.

Tract answered 12/2, 2013 at 18:0 Comment(4)
Thank you! But after pressing the Button theEnd, program becomes stuck! I am using python 2.7.3! By the way, what is the advantage of defining Tkinter as tk? I saw other people also doing that, but I didnt understand the reason!Pastoral
Are you using an IDE? If so, try running the script from the command line: python script.py. I think it should work fine.Tract
Although some might argue that import * from Tkinter is okay -- indeed, I think the author of Tkinter designed it to be imported that way -- in general one should only use import * ... in interactive sessions, not in scripts. By using the more verbose import Tkinter as tk, you clarify exactly where every object is coming from. That helps when debugging or reading other people's code, and it prevents name collisions when two modules use the same names. For example, numpy.sum is different than the builtin Python sum. import * from numpy would be awful.Tract
You are right. I was using the idle. It works now! I really appreciatePastoral
C
0

you can use root.destroy() i seem it's working on my last programme,i hope it's useful for you

Connett answered 20/4, 2023 at 14:2 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ferrule

© 2022 - 2024 — McMap. All rights reserved.