1

I want to create an app that starts on a main window and by pressing a button it should change to a diferent tkinter frame so the main frame should not be visible. The problem I have is that one frame overlaps another and I can see labels which shouldnt appear (labels of the main frame)

This is what I see on the main window: Main window

and this is what I see when I press the button: I just want to have the "Win 2" label

This is the code I have:

import tkinter


root = tkinter.Tk()
root.title("My Program")
root.geometry("700x350")


def swap_window():
    win2 = tkinter.Frame(root).grid()
    tag2 = tkinter.Label(win2, text="Win 2").grid(row=0, column=0)

main_window = tkinter.Frame(root).grid()
main_tag = tkinter.Label(main_window, text="Main window").grid(row=0, column=0)
btn_swap_window = tkinter.Button(main_window, text="Window 2", command=swap_window).grid(row=1, column=0)


root.grid()


root.mainloop()
DP7
  • 11
  • 2
  • There are several approaches to achieve this. The most stable one is to use pack_forget or `.grid_forget()` on the frame you want to disappear and grid or pack the new frame to appear in the same master. Worth to consider a specific width and height of your window and using appropriate optional argument to fill the space. [You may find this useful](https://stackoverflow.com/a/63536506/13629335) – Thingamabobs Jun 17 '22 at 14:18
  • 2
    `win2` is None - the result of `.grid()`, rather than the actual Frame. Passing None as the parent of `tag2` makes it a child of the root window. – jasonharper Jun 17 '22 at 14:32
  • I have just add "main_window.grid_forget()" at the end of the swap_window function and when I press the button ir raise AttributeError. – DP7 Jun 17 '22 at 14:33
  • 1
    `main_window` is also None. Every variable in your code other than `root` is None. – jasonharper Jun 17 '22 at 14:39

0 Answers0