Python Tkinter TopLevel : too many window

85 Views Asked by At

in my code i use 1 time the function to load window 2 when i click on a buttun but when i do it launch 2 window instead of 1 and only 1 has the color that i want

from tkinter import * 


def load_window1():
    yt_buttun.pack(side=LEFT)
    class_buttun.pack(side=LEFT)
    music_buttun.pack(side=RIGHT)
    label_tittle.pack()
    buttun_frame.pack()
    print("fenetre 1 / page d'acceuil affiché")
    window1.mainloop()


def load_window2():
    # fenetre choisir classe

    window2 = Toplevel()
    window2.config(background="#D13C3C")
    # Composants --> deuxieme page

    label_tittle2 = Label(window2, text="choisi ton programme de révision", font=("Courrier", 40), bg="#41B77F", fg='white')
    buttun_frame2 = Frame(window2, bg='#41B77F', bd=2, relief=SUNKEN)
    buttun5m = Button(buttun_frame2, text="5eme", font=("Courrier", 60), bg="#D13C3C", fg='#41B77F',command=open_HighKhala_channel)
    buttun4m = Button(buttun_frame2, text="4eme", font=("Courrier", 60), bg="#359AF1", fg='white', command=do_music)
    buttun3m = Button(buttun_frame2, text="3eme", font=("Courrier", 60), bg="#9962D8", fg='black', command=do_music)

    label_tittle2.pack()
    buttun_frame2.pack()
    buttun5m.pack()
    buttun4m.pack()
    buttun3m.pack()
    print("fenetre 2 / page choisir classe affiché")

def destroy_window1():
    window1.destroy()
    load_window2()


# fenetre acceuil

window1 = Tk()
window1.attributes('-fullscreen', True)
window1.bind('<Escape>', lambda e: window1.destroy())
window1.config(background='#41B77F')

# Composants --> première page

label_tittle = Label(window1, text="Appli d'Erkan pour le BREVET", font=("Courrier", 40), bg="#41B77F", fg='white')
buttun_frame = Frame(window1, bg='#41B77F', bd=2, relief=SUNKEN)
yt_buttun = Button(buttun_frame, text="Ma Chaine YT", font=("Courrier", 40), bg="#D13C3C", fg='#41B77F', command=open_HighKhala_channel)
class_buttun = Button(buttun_frame, text="Choisir sa classe", font=("Courrier", 40), bg="#359AF1", fg='white', command=destroy_window1)
music_buttun = Button(buttun_frame, text="Un peu de musique", font=("Courrier", 40), bg="#9962D8", fg='black', command=do_music)



load_window1()

thanks for your answers

1

There are 1 best solutions below

7
Robert Salamon On

What you can do is use the widraw() method which simply hides window1:

def destroy_window1():
    window1.withdraw()
    load_window2()

To read more on the .withdraw() method you can read the Tk documentation here: https://tkdocs.com/tutorial/windows.html#:~:text=Iconifying%20and%20Withdrawing