Home > Software engineering >  Tkinter window not displaying widgets
Tkinter window not displaying widgets

Time:07-05

  1. This code does not show any widgets in the main window. Any solutions?

  2. Is there a way to create multiple toplevel windows in one single module? Currently, I am creating new python module for each toplevel window.

  3. Also, after creating a new top level window, do we use mainloop or update or update_ideltasks?

Thank you!

    class MainWindowWidgets(ttk.Frame):
        def __int__(self, container):
           super().__init__(container)
    
           create_frame = ttk.LabelFrame(container, text='New Frame')
           create_frame.grid(row=0, column=0)
    
           create_btn = ttk.Button(create_frame, text='New Button')
           create_btn.grid(row=0, column=0)
    
    
    class App(tk.Tk):
        def __init__(self):
            super().__init__()
            self.title('My App')
            self.geometry('1000x500')
            self.resizable(False, False)
            main_window_widgets = MainWindowWidgets(self)
            main_window_widgets.pack()
    
    
    # This function is called from login window if login is successful and login window is destroyed

    def open_main_window():
        app = App()
        app.mainloop()

CodePudding user response:

There are two issues in your MainWindowWidgets class:

  • __int__(...) should be __init__(...) instead
  • self should be used instead of container as the parent of create_frame
class MainWindowWidgets(ttk.Frame):
    # __int__ should be __init__ instead
    #def __int__(self, container):
    def __init__(self, container):
       super().__init__(container)

       # use self instead of container as the parent
       #create_frame = ttk.LabelFrame(container, text='New Frame')
       create_frame = ttk.LabelFrame(self, text='New Frame')
       create_frame.grid(row=0, column=0)

       create_btn = ttk.Button(create_frame, text='New Button')
       create_btn.grid(row=0, column=0)
  • Related