Python Forum
[Tkinter] Change label for multiple frames
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Change label for multiple frames
#1
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?
Reply
#2
A label is a widget, just like a button or text.
Reply
#3
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:
   
Reply
#4
Thanks, Larz60+!

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  [WxPython] [SOLVED] How to change button label? Winfried 3 2,020 May-31-2022, 06:37 PM
Last Post: Winfried
  [Tkinter] Change Label Every 5 Seconds gw1500se 4 6,749 May-26-2020, 05:32 PM
Last Post: gw1500se
  [Tkinter] Python 3 change label text gw1500se 6 4,604 May-08-2020, 05:47 PM
Last Post: deanhystad
  [Tkinter] How to add multiple frames to main window Dandy_Don 13 7,776 Apr-29-2020, 09:21 PM
Last Post: Dandy_Don
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,657 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [Tkinter] So what do I need to do change the color this label? jpezz 10 8,652 Apr-03-2019, 09:49 PM
Last Post: jpezz
  [Tkinter] Multiple frames with tkinter - How to make them run on fullscreen mode eabs86 3 18,202 Sep-20-2018, 01:27 AM
Last Post: eabs86

Forum Jump:

User Panel Messages

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