Tkinter new window contentent when button is clicked.

MRAB python at mrabarnett.plus.com
Wed Feb 24 20:01:35 EST 2021


On 2021-02-24 21:57, Bischoop wrote:
> 
> I'm learning Tkinter now and have upgraded few programs I've made in CLI
> in the past.
> What is bothering me now is what I should look at when I want new
> content in a window when button is 'Next' is clicked. In some programs
> we're clicking button next and new content appears in same window.
> I've used so far Toplevel(root) and new window was appearing on top the
> main one but I'd like to find out about solution without creating new
> windows. Is it a Canvas used for it?
> I'd be glad if you could direct me because I don't know even what to
> look for.
> 
The trick is to put the "pages" on top of each other and then show the 
appropriate one, something like this:
import tkinter as tk

def on_next_page():
     # Brings page 2 to the top.
     frame_2.tkraise()

def on_previous_page():
     # Brings page 1 to the top.
     frame_1.tkraise()

def on_finish():
     # Closes the dialog.
     root.destroy()

root = tk.Tk()

# Page 1.
frame_1 = tk.Frame(root)
tk.Label(frame_1, text='Page 1').pack()
tk.Button(frame_1, text='Next', command=on_next_page).pack()

# Page 2.
frame_2 = tk.Frame()
tk.Label(frame_2, text='Page 2').pack()
tk.Button(frame_2, text='Previous', command=on_previous_page).pack()
tk.Button(frame_2, text='Finish', command=on_finish).pack()

# Put the pages on top of each other.
frame_1.grid(row=0, column=0, sticky='news')
frame_2.grid(row=0, column=0, sticky='news')

# Bring page 1 to the top.
frame_1.tkraise()

tk.mainloop()


More information about the Python-list mailing list