Tkinter, Toplevel image blanche
ouiIRL Messages postés 3 Statut Membre -
Bonjour,
J'essaye de programmer un jeu. Je suis confronté à un problème, après avoir une bonne dizaine de technique ou après avoir regardé plein de forum, je ne l'ai pas résolu.
Le problème est le suivant :
- j'ouvre une fenêtre qui comporte un bouton ;
- ce bouton appelle une fonction qui cache la fenêtre principale et en ouvre une autre ;
- mais lors de l'ouverture de la deuxième fenêtre, une fenêtre blanche s'affiche.
Voici la partie problématique du code:
from tkinter import *
def open_toplevel():
global choix_on
if choix_on == True:
choix_on = False
else:
choix_on = True
if choix_on == False:
win.withdraw()
DG = Toplevel(win)
width = 1920
height = 1080
canvas = Canvas(DG, width=width, height=height)
canvas.pack()
fight = PhotoImage(file="fight.png")
fight.config(width=width, height=height)
canvas.create_image(0, 0, image=fight, anchor="se")
atk = Button(DG, text="Attaquer", bg="red", fg="orange")
inv_button = Button(DG, text="Inventaire", bg="blue", fg="purple")
fuir_button = Button(DG, text="Fuir", bg="green", fg="yellow")
canvas.create_window(200,400, window=atk)
canvas.create_window(200, 425, window=inv_button)
canvas.create_window(200, 450, window=fuir_button)
win = Tk()
win.geometry("1920x1080")
choix_on = True
width = 1920
height = 1080
droite = PhotoImage(file="flècheDroiteS.png")
droite.config(width="174", height="100")
gauche = PhotoImage(file="flecheGaucheS.png")
gauche.config(width="174", height="100")
choix = PhotoImage(file="choix.png")
canva = Canvas(win, width=width, height=height)
canva.create_image(width, height, image=choix,anchor="se")
label_start = Label(win, text="Que voulez vous faire ?",
font=("Comic Sans MS", 20, "bold"), bg="#5db6ce", fg="white")
canva.create_window(960, 100, window=label_start)
canva.pack()
FlecheDroite = Button(win,command=open_toplevel)
FlecheDroite.config(image=droite)
FlecheDroite.pack()
canva.create_window(1400, 600, window=FlecheDroite)
FlecheGauche = Button(win,command=open_toplevel)
FlecheGauche.config(image=gauche)
FlecheGauche.pack()
canva.create_window(740, 500, window=FlecheGauche)
win.mainloop()
D'ailleurs, lorsque je souhaite mettre un titre ou quelque paramètre que ce soit à ma fenêtre top level, ça m'affiche un msg d'erreur disant que l'option ne marche pas.
J'ai vraiment besoin d'aide, je suis bloqué depuis deux jours dessus.
Merci d'avance.
4 réponses
-
Voici un exemple très basique de comment basculer d'un contenu à un autre.
import tkinter as tk # Fonction d'échange des contenus de la fenêtre def frame_switch(): # winfo_ismapped indique si un widget est « packed » if main_frame.winfo_ismapped(): window.title('Seconde frame') # Unpacking de la frame et packing de l'autre main_frame.pack_forget() second_frame.pack() else: window.title('Frame Principale') second_frame.pack_forget() main_frame.pack() window = tk.Tk() window.geometry('400x200') window.title('Frame Principale') # Frame visible au lancement main_frame = tk.Frame(window) main_frame.pack() info_label = tk.Label(main_frame, text='info blabla', bg='blue', fg='white') info_label.pack() switch_button = tk.Button(main_frame, text='Vers blabla', command=frame_switch) switch_button.pack() # Frame invisible au lancement et son contenu second_frame = tk.Frame(window) blabla_text = tk.Text(second_frame, width=50, height=7) blabla_text.pack() blabla_text.insert(0.0, 'blabla\n' * 5) back_button = tk.Button(second_frame, text='Revenir', command=frame_switch) back_button.pack() window.mainloop()Si tu comprends le principe, tu n'auras aucun mal à l'adapter à ton besoin.
Bonne continuation.
-
Salut, il faudrait faire un exemple facilement reproductible, nous n'avons pas tes images, de plus la taille de la fenêtre est conséquente.
Puis pourquoi utiliser un canvas pour placer tes widgets ?
Maintenant, si on mets en commentaires toutes les parties des images, cela « fonctionne ».
from tkinter import * def open_toplevel(): global choix_on if choix_on == True: choix_on = False else: choix_on = True if choix_on == False: # win.withdraw() DG = Toplevel(win) width = 800 height = 600 canvas = Canvas(DG, width=width, height=height) canvas.pack() # fight = PhotoImage(file="fight.png") # fight.config(width=width, height=height) # canvas.create_image(0, 0, image=fight, anchor="se") atk = Button(DG, text="Attaquer", bg="red", fg="orange") inv_button = Button(DG, text="Inventaire", bg="blue", fg="purple") fuir_button = Button(DG, text="Fuir", bg="green", fg="yellow") canvas.create_window(200,400, window=atk) canvas.create_window(200, 425, window=inv_button) canvas.create_window(200, 450, window=fuir_button) win = Tk() # win.geometry("1920x1080") choix_on = True width = 800 height = 600 # droite = PhotoImage(file="flècheDroiteS.png") # droite.config(width="174", height="100") # gauche = PhotoImage(file="flecheGaucheS.png") # gauche.config(width="174", height="100") # choix = PhotoImage(file="choix.png") canva = Canvas(win, width=width, height=height) # canva.create_image(width, height, image=choix,anchor="se") label_start = Label(win, text="Que voulez vous faire ?", font=("Comic Sans MS", 20, "bold"), bg="#5db6ce", fg="white") canva.create_window(200, 200, window=label_start) canva.pack() FlecheDroite = Button(win,text='D', command=open_toplevel) # FlecheDroite.config(image=droite) FlecheDroite.pack() canva.create_window(200, 400, window=FlecheDroite) FlecheGauche = Button(win,text='G', command=open_toplevel) # FlecheGauche.config(image=gauche) FlecheGauche.pack() canva.create_window(300, 400, window=FlecheGauche) win.mainloop()Ce que tu cherches à faire est de modifier le contenu de la fenêtre principale non ?
En ce cas, il faut affecter chacun des contenus à différentes frames, et selon ce que tu veux qu'il s'affiche, pack et unpack ces frames.
-
merci beaucoup maintenant j'ai juste à regarde une bonne heure de comment utiliser les frames????
-