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
#1
I am stumped. I can't figure out how to add multiple frames to the main window in a GUI....

I have a system that uses multiple winches to raise/lower things. I want to create a GUI that shows each winch & allows for control of them.

Since the winch frame is the same for each winch (except for the winch name, "Top Right", etc.) I created a class for the winch frame. Then I created a main app class. I can't seem to get the winches added to the main app, though....


from tkinter import  *

class WinchFrame(Frame):
    def __init__(self):
        super().__init__()

        #  CREATE WIDGETS:
        self.winch_lbl = Label(text="Top Right", width=15)
        self.lbl_btn_up = Label(text="UP")
        self.btn_up = Button(text="OFF", width=12, fg='green')
        self.txt = Text(height=1, width=50, bg="light blue")
        self.lbl_btn_down = Label(text="DOWN")
        self.btn_down = Button(text="OFF", width=12, fg='green')
        #  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)


class WinchApp(Tk):
    def __init__(self):

        self.frames = {}
        self.frames["Top Right"] = WinchFrame(parent=container, controller=self)
        self.frames["Top Right"].grid(row=0, column=0, sticky="nsew")

        self.Top_Right = WinchFrame()
        self.Bot_Right = WinchFrame()
        self.Top_Right.grid(row=0, column=0, sticky='nsew')
        self.Bot_Right.grid(row=5, column=0, sticky='nsew')


if __name__ == "__main__":
    app = WinchApp()
    app.mainloop
Any help you can offer will be greatly appreciated!
Reply
#2
create additional instances of WinchFrame
Reply
#3
I thought that was what I did in the WinchApp class:


        self.Top_Right = WinchFrame()
        self.Bot_Right = WinchFrame()
        self.Top_Right.grid(row=0, column=0, sticky='nsew')
        self.Bot_Right.grid(row=5, column=0, sticky='nsew')
Am I missing something?
Reply
#4
on line 27 you're calling WinchFrame with attributes that don't exist.
Reply
#5
Oops! That was where I was trying to add it to a container..... I got it from this post:

https://stackoverflow.com/questions/7546...in-tkinter

That didn't work either. Maybe I didn't use the container right.....
Reply
#6
Well, if you are going to try code that doesn't 'work'
at least show us what (runnable code) didn't work
Reply
#7
I can get the labels & buttons & text entry widgets grouped together in a window....

class WinchFrame(Frame):
    def __init__(self):
        super().__init__()

        #  CREATE WIDGETS:
        self.winch_lbl = Label(text="Top Right", width=15)
        self.lbl_btn_up = Label(text="UP")
        self.btn_up = Button(text="OFF", width=12, fg='green')
        self.txt = Text(height=1, width=50, bg="light blue")
        self.lbl_btn_down = Label(text="DOWN")
        self.btn_down = Button(text="OFF", width=12, fg='green')
        #  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)
But I do not know how to make multiple copies of that class appear in a well-organized appearance in a window.
Reply
#8
When you define the Widgets in the WinchFrame Class, you have to assign them to the Frame. Otherwise, they get assigned to the main window, and put in the grid there. All you have to do is add "self" to those Widgets. I also fixed the other issue that was already mentioned.

from tkinter import  *
 
class WinchFrame(Frame):
    def __init__(self):
        super().__init__()
 
        #  CREATE WIDGETS:
        winch_lbl = Label(self, text="Top Right", width=15)
        lbl_btn_up = Label(self, text="UP")
        btn_up = Button(self, text="OFF", width=12, fg='green')
        txt = Text(self, height=1, width=50, bg="light blue")
        lbl_btn_down = Label(self, text="DOWN")
        btn_down = Button(self, text="OFF", width=12, fg='green')
        #  LAYOUT:
        winch_lbl.grid(row = 1, column = 0, padx=5, pady=5)
        lbl_btn_up.grid(row=1, column=1, padx=5, pady=5)
        btn_up.grid(row=1, column=2, padx=5, pady=5)
        lbl_btn_down.grid(row=3, column=1, padx=5, pady=5)
        btn_down.grid(row=3, column=2, padx=5, pady=5)
        txt.grid(row = 5, column=1, columnspan=2, padx=5, pady=5)

class WinchApp(Tk):
    def __init__(self):
        super().__init__()
 
        self.Top_Right = WinchFrame()
        self.Bot_Right = WinchFrame()
        self.Top_Right.grid(row=0, column=0, sticky='nsew')
        self.Bot_Right.grid(row=5, column=0, sticky='nsew')
 
if __name__ == "__main__":
    app = WinchApp()
    app.mainloop
Reply
#9
Thanks, Riddle.

When I make the changes you suggest & run the code, it does nothing.... The output says "Process finished with exit cod 0"

Do I need to "show" the window somehow?
Reply
#10
I am at a loss. If I run that exact same code, it works with no errors.

Did you try to copy and paste my code into a new file, just to see if it works? Maybe it is a minor difference in code that you are missing.
Also, I doubt that it matters, but I am using Version 3.8.2 of Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Hi, Keep postition of main window after iconify() delphinis 3 333 Jul-12-2020, 06:59 AM
Last Post: DT2000
  [Tkinter] Auto re-fit frames sizes in main window Gilush 2 384 Jun-06-2020, 03:14 AM
Last Post: Gilush
  [Tkinter] Change label for multiple frames Dandy_Don 3 496 Apr-30-2020, 02:22 PM
Last Post: Dandy_Don
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 1,016 Dec-16-2019, 04:47 AM
Last Post: woooee
  tkinter window and turtle window error 1885 3 1,645 Nov-02-2019, 12:18 PM
Last Post: 1885
  “main thread is not in main loop” in Tkinter Long_r 1 7,391 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,787 Sep-20-2018, 01:27 AM
Last Post: eabs86
  pyqt main window refresh poblems duende 0 2,596 Apr-13-2018, 05:05 PM
Last Post: duende
  set default font main window pyside Gigux 0 1,713 Apr-23-2017, 01:13 PM
Last Post: Gigux
  update a variable in parent window after closing its toplevel window gray 5 4,226 Mar-20-2017, 10:35 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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