Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

python - How to close a single window in Tkinter without closing the main window?

I am making a database application for a school coursework project. I have a login screen which takes the user into the main menu screen. From there, the user can open up a customer form. I want to be able to close the form using a button, however, whenever I try to do this, the entire program stops running. Does anyone have a solution to this problem? I have attached a snippet of the code which includes the quit() function applied to the button, along with images of the form. Please leave a comment if you would like more information.

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    global prog_location
    prog_call = sys.argv[0]
    prog_location = os.path.split(prog_call)[0]
    root = tk.Tk()
    top = CustomerForm (root)
    CustomerForm_support.init(root, top)
    root.mainloop()

w = None
def create_CustomerForm(rt, *args, **kwargs):
    '''Starting point when module is imported by another module.
       Correct form of call: 'create_CustomerForm(root, *args, **kwargs)' .'''
    global w, w_win, root
    global prog_location
    prog_call = sys.argv[0]
    prog_location = os.path.split(prog_call)[0]
    #rt = root
    root = rt
    w = tk.Toplevel (root)
    top = CustomerForm (w)
    CustomerForm_support.init(w, top, *args, **kwargs)
    return (w, top)


class CustomerForm:
    def quit(self):
        root.destroy()

Image of the main menu

Image of the customer form


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As far as I understand (I cannot reproduce your code) what you might want to close is w, not root, as that last one is your main loop.

Replace:

 root.destroy()

For:

 w.destroy()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...