Sidelobe
Sidelobe

Reputation: 440

Using textvariable for a Tkinter (or ttk) LabelFrame

I want to update the label of a Tkinter LabelFrame widget.

For a Label widget, this can be done using the textvariable attribute, to which one can assign a StringVar.

I want to do the same thing, but for a LabelFrame

self.labelText = StringVar()
self.selectionFrame = ttk.LabelFrame(self, textvariable=self.labelText)

(...)

if A:
     self.labelText.set("LabelA")
elif B:
     self.labelText.set("LabelB")

How can I achieve this?

Upvotes: 0

Views: 2663

Answers (3)

Grandya
Grandya

Reputation: 1

I found that there is a problem to set a new label text due to text length. So, I recommend that define a width of labelwidget as mentioned below:

self.labelWidget = Label(self, textvariable=self.labelText, width = 20)

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 386230

You can't. Neither the Tkinter LabelFrame nor the ttk LabelFrame support associating a variable with the widget.

If what you're really asking is how can you change the label, then you can use the configure method:

self.selectionFrame.configure(text="hello")

Upvotes: 2

Sidelobe
Sidelobe

Reputation: 440

I just found some kind of a solution - using the labelwidget attribute to supply a separate Label object which uses the underlying StringVar:

self.labelText = StringVar()
self.labelWidget = Label(self, textvariable=self.labelText)
self.selectionFrame = ttk.LabelFrame(self, labelwidget=self.labelWidget)

This way, I can update the labelText to change the label of the LabelFrame

self.labelText.set("New Label")

Upvotes: 1

Related Questions