Lizzie
Lizzie

Reputation: 53

How to close a window when you click a button to open another window

I am working on a program that will allow someone to enter details in order to write a CV. I am using the Tkinter module (as extra practice) but am already stuck on the menu!

At the moment I have three different options the user can choose: Write CV, Review CV and Exit. I have created a button for each option and when the user presses the button it'll open, however the menu window remains open (there is a different subroutine for each option).

I understand that you need to do something like window.destroy(), however I'm not sure how to give a button two commands without doing something too fiddly like create more subroutines etc.?

The other option I think I'd prefer is is I could clear the menu screen?

Here is the programming I have at the moment:

def Main_Menu():
    import tkinter
    main_menu = tkinter.Tk()
    main_menu.title("CV Writer")
    main_menu.geometry("300x300")
    main_menu.wm_iconbitmap('cv_icon.ico')
    title = tkinter.Label(main_menu, text = "Main Menu", font=("Helvetica",25))
    title.pack()
    gap = tkinter.Label(main_menu, text = "")
    gap.pack()
    write_cv = tkinter.Button(main_menu, text = "1) Write CV", font=("Helvetica"), command=Write_CV)
    write_cv.pack()
    review_cv = tkinter.Button(main_menu, text = "2) Review CV", font=("Helvetica"), command=Review_CV)
    review_cv.pack()
    leave = tkinter.Button(main_menu, text = "3) Exit", font=("Helvetica"), command=Exit)
    leave.pack()
    main_menu.mainloop()

def Write_CV():
    import tkinter
    write_cv = tkinter.Tk()
    write_cv.geometry("300x300")
    write_cv.title("Write CV")

def Review_CV():
    import tkinter
    review_cv = tkinter.Tk()
    review_cv.geometry("300x300")
    review_cv.title("Review CV")

def Exit():
    import tkinter
    leave = tkinter.Tk()
    leave.geometry("300x300")
    leave.title("Exit")

Main_Menu()

Running the program should help make this question make more sense!

I am so sorry for the wordy question, but any kind of help would be appreciated! Please bear in mind I am only a GCSE student so simple language would also be so nice! Thank you!

Upvotes: 0

Views: 1161

Answers (1)

nbro
nbro

Reputation: 15837

I don't know why are you importing tkinter under each method, it's completely useless. Simply import it once at the beginning of your file with a syntax like this:

import tkinter as tk

So that you can refer to the widgets simply with the duo tk:

btn = tk.Button(None, text='I can simply refer to a widget with tk')

Apart from this, the structure of your program is really bad. In my opinion, you should not instantiate Tk inside your function Main_Menu, because it will only be visible inside it. If you want to refer to the master or root or whatever you want to call the instance of Tk, you can't, because it's a local instance, as I said above.

I usually instantiate Tk in the main function of my program, or in the following if __name__ == '__main__': construct:

if __name__ == '__main__':
    master = tk.Tk()  # note I am using "tk"
    # create your objects or call your functions here
    master.mainloop()

Your are creating an instance of Tkin each of your function, that is really a bad practice, never do that. You should only create one instance of Tk for each Tkinter application.

You should use the object-oriented paradigm or make all your widgets global to structure your application.

Except these details, you can simply call master.destroy() when you want to destroy your main window and all its children widgets, where master is the Tk instance.

In general, you have a lot of errors and bad practices. My advice is:

Read a tutorial on Python first and then on Tkinter, before proceeding.

Upvotes: 1

Related Questions