1

I have been developing a tkinter project and I have run into a problem where I have two windows open when the button widget is clicked instead of just a singular window frame. There isn't any specific syntactic error with my code, I was just hoping someone could point me in the right direction for structuring my small GUI program correctly.

I would be really appreciate any support even if it was just an example code or a link.

Thanks.

Welcome Page

from tkinter import *
from from External_Menu import *

root = Tk()
root.state('zoomed')
root.title("Leisure Centre")
title = Label(root, text="Leisure Centre", font=("", 26))
title.place(relx=0.5, rely=0.0, anchor=N)

welcome = Button(root, text="Welcome!", font=("", 18), command=menu)
welcome.place(relx=0.5, rely=0.5, anchor=CENTER)

root.mainloop()`

External_Menu

from tkinter import *

def menu():
  root = Tk()
  root.state('zoomed')
  external_menu_lbl = Label(root, text="External Menu", font=("", 26))
  external_menu_lbl.pack()

  sign_in_button = Button(root, text="Sign In", command=login_page)
  sign_in_button.pack()

  sign_up_button = Button(root, text="Sign Up", command=sign_up_page)
  sign_up_button.pack()

  root.mainloop()
Notorious
  • 33
  • 2
  • 9
  • use `Toplevel()` to create second window. And don't use second `mainloop()` – furas Dec 02 '17 at 16:38
  • adn what problem do you have ? Describe it. – furas Dec 02 '17 at 16:39
  • Hi Furas, the problem I have is that these two files open two seperate windows when I want it all within the same window, almost like a mainframe. – Notorious Dec 02 '17 at 16:41
  • if you want to have only one window then close previous window - `root.destroy()` - before you create new one - and then you can use `Tk()` and `mainloop()` – furas Dec 02 '17 at 16:41
  • Thanks, I have seen that method before but unfortunately that annoying window opening animation occurs which is what I am trying to prevent, I am trying to develop a window within a window structure if that is possible. – Notorious Dec 02 '17 at 16:44
  • if you what to use one window you can also **replace** elements using `pack_forget()` to remove old elements. And it will easier if you keep elements in frame because you can `forget` frame and it disappire with all widgets inside. – furas Dec 02 '17 at 16:44
  • oh, that sounds like a good idea, I will definitely look into that, thanks for taking the time out of your day man. – Notorious Dec 02 '17 at 16:46
  • Possible duplicate of [Switch between two frames in tkinter](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter) – furas Dec 02 '17 at 16:47
  • 1
    You have two top-level windows when you hit the button because the button handler is creating that second top-level window with another call to `root=Tk()`. If you only want a single top-level window, then use `root` in your button handler to reconfigure the widgets of root. If for some reason you don't have straight access to root in your menu function, you can pass it along as a variable to the menu function by replacing `command=menu` in the Button statement with a lambda, like `command=lambda r=root:menu(r)` – GaryMBloom Dec 02 '17 at 17:12
  • You create two windows, so you get two windows. Every time you do `root=Tk()`, you get a window. – Bryan Oakley Dec 02 '17 at 17:21

0 Answers0