Python Forum
[Tkinter] How to add multiple frames to main window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to add multiple frames to main window
Ok. I'm using Python 3.6 with PyCharm IDE on a MacBook.

I tried using the terminal to run the code & it still does nothing....
Sorry, I am at a loss. Maybe someone else can help.

Personally, when things like this happen, I try to find out where the issue is by adding little things to the code here and there. For example, it the WinchApp class, try adding this line and see if it shows anything:

Label(self, text="Hello").grid(row=10, column=20)
Nope. Nothing.

I can run other tkinter python code that works, but this GUI does not show up.... even after adding your label code to the WinchApp class.
Ok. I got it working.... I used notebook & added the frames to a tab of the notebook:

class MainWindow(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        # create different tabs within the main window:
        nb = ttk.Notebook(root)
        tab1 = ttk.Frame(nb)

        TopLeft = WinchFrame(tab1, padx=10, pady=20, relief='ridge')
        TopRight = WinchFrame(tab1, padx=10, pady=20)
        BotLeft = WinchFrame(tab1, padx=10, pady=20, relief='ridge')
        BotRight = WinchFrame(tab1, padx=10, pady=20)

class WinchFrame(tk.Frame):

    def __init__(self, parent=None, **kw):
        Frame.__init__(self, parent, kw)

        #  CREATE WIDGETS:
        self.winch_lbl = Label(self, width=15)
        self.lbl_btn_up = Label(self, text="UP")
        self.btn_up = Button(self, text="OFF", width=12, fg='green', command=self.toggle_up)
        self.txt = Text(self, height=1, width=50, bg="light blue")
        self.lbl_btn_down = Label(self, text="DOWN")
        self.btn_down = Button(self, text="OFF", width=12, fg='green', command=self.toggle_down)

        #  LAYOUT:
        self.winch_lbl.grid(row=1, column=0, padx=5, pady=5)
        self.lbl_btn_up.grid(row=1, column=1, padx=5, pady=5)
        self.btn_up.grid(row=1, column=2, padx=5, pady=5)
        self.lbl_btn_down.grid(row=3, column=1, padx=5, pady=5)
        self.btn_down.grid(row=3, column=2, padx=5, pady=5)
        self.txt.grid(row=5, column=1, columnspan=2, padx=5, pady=5)

    def toggle_up(self):
        to get the present state of the toggle button
        if self.btn_up.config('text')[-1] == 'ON':
            self.btn_up.config(text='OFF', highlightbackground='gray')
            print('button turned off')
            self.btn_down['state'] = tk.NORMAL
            self.btn_up.config(text='ON', highlightbackground='red')
            print('button turned on')
            self.btn_down['state'] = tk.DISABLED

    def toggle_down(self):
        to get the present state of the toggle button
        if self.btn_down.config('text')[-1] == 'ON':
            self.btn_down.config(text='OFF', highlightbackground='gray')
            print('button turned off')
            self.btn_up['state'] = tk.NORMAL
            self.btn_down.config(text='ON', highlightbackground='red')
            print('button turned on')
            self.btn_up['state'] = tk.DISABLED


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Hi, Keep postition of main window after iconify() delphinis 3 328 Jul-12-2020, 06:59 AM
Last Post: DT2000
  [Tkinter] Auto re-fit frames sizes in main window Gilush 2 379 Jun-06-2020, 03:14 AM
Last Post: Gilush
  [Tkinter] Change label for multiple frames Dandy_Don 3 494 Apr-30-2020, 02:22 PM
Last Post: Dandy_Don
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 999 Dec-16-2019, 04:47 AM
Last Post: woooee
  tkinter window and turtle window error 1885 3 1,630 Nov-02-2019, 12:18 PM
Last Post: 1885
  “main thread is not in main loop” in Tkinter Long_r 1 7,322 Jun-26-2019, 11:00 PM
Last Post: metulburr
  [Tkinter] Multiple frames with tkinter - How to make them run on fullscreen mode eabs86 3 12,732 Sep-20-2018, 01:27 AM
Last Post: eabs86
  pyqt main window refresh poblems duende 0 2,590 Apr-13-2018, 05:05 PM
Last Post: duende
  set default font main window pyside Gigux 0 1,711 Apr-23-2017, 01:13 PM
Last Post: Gigux
  update a variable in parent window after closing its toplevel window gray 5 4,214 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020