Home > database >  Tkinter window swapping
Tkinter window swapping

Time:01-05

I don't know much about python or Tkinter but my functions aren't firing. No errors but No result either.

from tkinter import *


def root_win():

    root = Tk()
    root.geometry('700x400 100 100')
    root.maxsize(700, 400)
    root.minsize(700, 400)
    root.title("root")

    btn1_root = Button(root, text="ext1", command=lambda: [root.destroy, ext1_win])
    btn1_root.place(x=590, y=360, height=30, width=100)
    btn1_root.configure(bg="DodgerBlue3", fg="black")


def ext1_win():

    ext1 = Tk()
    ext1.geometry('700x400 100 100')
    ext1.maxsize(700, 400)
    ext1.minsize(700, 400)
    ext1.title("1")

    btn1_ext1 = Button(ext1, text="back", command=lambda: [ext1.destroy, root_win])
    btn1_ext1.place(x=590, y=360, height=30, width=100)
    btn1_ext1.configure(bg="DodgerBlue3", fg="black")


root_win()

I'm trying to make it so I can hop between different windows to conserve screen space when I start putting together the rest of my project. Tkinter doesn't seem to like all of def root_win(): being a def.

CodePudding user response:

If you use lambda, you need to call the functions inside the body of the lambda.

btn1_ext1 = Button(ext1, text="back", command=lambda: [root.destroy(), ext1_win()])

You also need to call mainloop. With the above code your functions will be called, but they just run to completion and then exit.

def root_win():

    root = Tk()
    ...
    root.mainloop()

CodePudding user response:

In Line 12, change this:

btn1_root = Button(root, text="ext1", command=lambda: [root.destroy, ext1_win])

to:

btn1_root = Button(root, text="ext1", command=ext1_win)

Output you can see root.title("root"):

enter image description here

Output you can see ext1.title("1":

enter image description here

  • Related