Python Forum

Full Version: Change label for multiple frames
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using Python 3 & Pycharm IDE.

I have a GUI that I created using tk. It is a notebook with multiple tabs.

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)
        nb.add(tab1, text='Winch Control')

        tab2 = ttk.Frame(nb)
        nb.add(tab2, text='Temp Converter')
On one of the tabs, I am adding multiple frames of the same class to a tab in the main window.

        TopLeft = WinchFrame(tab1, padx=10, pady=20, relief='ridge')
        TopLeft.pack()
        TopRight = WinchFrame(tab1, padx=10, pady=20)
        TopRight.pack()
        BotLeft = WinchFrame(tab1, padx=10, pady=20, relief='ridge')
        BotLeft.pack()
        BotRight = WinchFrame(tab1, padx=10, pady=20)
        BotRight.pack()
I would like to add a title/label to each of the frames in tab1, to differentiate between them. For example, "Top Left Winch", "Bottom Left Winch", etc.

Here is part of the Winch frame class:

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)


I added a label to the class definition (self.winch_lbl), but I can't figure out how to change the text of the label based on the instantiation of it....

        TopRight.config['text'] = "Top Right Winch"
Is is possible to add labels to a frame?
A label is a widget, just like a button or text.
TopRight.config['text'] = "Top Right Winch" will not work

Labels can be added to any other suitable widget, which includes frames.
they can be placed on frames, images, canvas and similar 'container type' widgets

Usage:
labelname.config(text = 'My new text')

the label must first exist.

there is also a special widget w = tk.LabelFrame(parent, option, ...)
which will draw a frame around the widgets it contains, with the label embedded in the frame:
[attachment=847]
Thanks, Larz60+!

I've never seen the labelframe. It looks like what I want.