Tkinter, Toplevel blank image

ouiIRL -  
ouiIRL Posted messages 3 Status Member -

Hello,

I'm trying to program a game. I'm facing a problem; after trying a good ten techniques and looking through many forums, I haven't solved it.

The problem is as follows:

  • I open a window that has a button;
  • this button calls a function that hides the main window and opens another one;
  • but when the second window opens, a blank window appears.

Here is the problematic part of the 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="Attack", bg="red", fg="orange") inv_button = Button(DG, text="Inventory", bg="blue", fg="purple") flee_button = Button(DG, text="Flee", 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=flee_button) win = Tk() win.geometry("1920x1080") choix_on = True width = 1920 height = 1080 right_arrow = PhotoImage(file="flècheDroiteS.png") right_arrow.config(width="174", height="100") left_arrow = PhotoImage(file="flecheGaucheS.png") left_arrow.config(width="174", height="100") choice = PhotoImage(file="choix.png") canva = Canvas(win, width=width, height=height) canva.create_image(width, height, image=choice, anchor="se") label_start = Label(win, text="What do you want to do?", font=("Comic Sans MS", 20, "bold"), bg="#5db6ce", fg="white") canva.create_window(960, 100, window=label_start) canva.pack() RightArrowButton = Button(win,command=open_toplevel) RightArrowButton.config(image=right_arrow) RightArrowButton.pack() canva.create_window(1400, 600, window=RightArrowButton) LeftArrowButton = Button(win,command=open_toplevel) LeftArrowButton.config(image=left_arrow) LeftArrowButton.pack() canva.create_window(740, 500, window=LeftArrowButton) win.mainloop() 

By the way, when I want to set a title or any parameter for my top level window, I get an error message saying that the option doesn't work.

I really need help, I've been stuck on this for two days.

Thanks in advance.

4 answers

jouflu
 

Here is a very basic example of how to switch from one content to another.

import tkinter as tk # Function to switch the contents of the window def frame_switch(): # winfo_ismapped indicates if a widget is "packed" if main_frame.winfo_ismapped(): window.title('Second frame') # Unpacking the frame and packing the other main_frame.pack_forget() second_frame.pack() else: window.title('Main Frame') second_frame.pack_forget() main_frame.pack() window = tk.Tk() window.geometry('400x200') window.title('Main Frame') # Frame visible at launch 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='To blabla', command=frame_switch) switch_button.pack() # Frame invisible at launch and its content 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='Go Back', command=frame_switch) back_button.pack() window.mainloop()

If you understand the principle, you will have no trouble adapting it to your needs.

Good luck.

1
ouiIRL Posted messages 3 Status Member
 

thank you very much ^^

0
jouflu
 

Hello, we need to create an easily reproducible example; we don't have your images, and the window size is quite large.

Also, why use a canvas to place your widgets?

Now, if we comment out all the parts related to the images, it "works".

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="Attack", bg="red", fg="orange") inv_button = Button(DG, text="Inventory", bg="blue", fg="purple") run_button = Button(DG, text="Run", 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=run_button) win = Tk() # win.geometry("1920x1080") choix_on = True width = 800 height = 600 # right = PhotoImage(file="flècheDroiteS.png") # right.config(width="174", height="100") # left = PhotoImage(file="flecheGaucheS.png") # left.config(width="174", height="100") # choice = PhotoImage(file="choix.png") canva = Canvas(win, width=width, height=height) # canva.create_image(width, height, image=choice,anchor="se") label_start = Label(win, text="What would you like to do?", font=("Comic Sans MS", 20, "bold"), bg="#5db6ce", fg="white") canva.create_window(200, 200, window=label_start) canva.pack() RightArrow = Button(win,text='D', command=open_toplevel) # RightArrow.config(image=right) RightArrow.pack() canva.create_window(200, 400, window=RightArrow) LeftArrow = Button(win,text='G', command=open_toplevel) # LeftArrow.config(image=left) LeftArrow.pack() canva.create_window(300, 400, window=LeftArrow) win.mainloop()

What you're trying to do is modify the content of the main window, right?

In that case, you need to assign each content to different frames, and depending on what you want to display, pack and unpack these frames.

0
ouiIRL Posted messages 3 Status Member
 

thank you very much now I just have to watch a good hour of how to use frames????

0
jouflu
 

Frames are just containers for other widgets, they're easy to use, the most complex part is using layout managers like pack or grid.

0
ouiIRL Posted messages 3 Status Member
 

fail the "???"

0