Home > other >  How to switch screens using tkinter?
How to switch screens using tkinter?

Time:11-30

I want to switch the screen using the tkinter module.

Is it possible to switch screens without frame?

This is the code I did.

#frame1.py
from tkinter import *

root=Tk()
root.title('page1')

def goto_p2():
    root.destroy()
    import frame2
Button(root, text="goto page1", command=goto_p2).pack()

mainloop()

.

#frame2.py
from tkinter import *

root=Tk()
root.title('page1')

def goto_p1():
    root.destroy()
    import frame1
Button(root, text="goto page2", command=goto_p1).pack()

mainloop()

CodePudding user response:

Here you go, example code how it can be done.

import tkinter as tk
from tkinter import Button,Label


def clear_frame():
    for widget in window.winfo_children():
        widget.destroy()


def screen_two():
    clear_frame()
    button_2 = Button(window, text="Go to  screen one", command=lambda: screen_one())
    button_2.pack(pady=10)
    label_2 = Label(window, text="Label on window two")
    label_2.pack(pady=10)


def screen_one():
    clear_frame()
    button_1 = Button(window, text="Go to screen two", command=lambda: screen_two())
    button_1.pack(pady=10)
    label_1 = Label(window, text="Label on window one")
    label_1.pack(pady=10)


window = tk.Tk()
window.title("Test")
window.geometry('250x100')

screen_one()
window.mainloop()
  • Related