0

I need a widget in TKinter to be a global widget, however, I need the text displayed in it to be different every time. I'm quite new with TKinter and haven't yet successfully managed to edit an option in a widget.

I assume it's something to do with widget.add_option() but the documentation is quite confusing to me and I can't figure out the command.

I specifically just need to edit the text = "" section.

Thanks

EDIT:

gm1_b_current_choice_label = Label(frame_gm1_b, text = "Current input is:\t %s"% str(save_game[6]))

I specifically need to update the save_game[6] (which is a list) in the widget creation, but I assume once the widget is created that's it. I could create the widget every time before I place it but this causes issues with destroying it later.

2 Answers 2

4

You can use the .config method to change options on a Tkinter widget.

To demonstrate, consider this simple script:

from Tkinter import Tk, Button, Label

root = Tk()

label = Label(text="This is some text")
label.grid()

def click():
    label.config(text="This is different text")

Button(text="Change text", command=click).grid()

root.mainloop()

When the button is clicked, the label's text is changed.

Note that you could also do this:

label["text"] = "This is different text"

or this:

label.configure(text="This is different text")

All three solutions ultimately do the same thing, so you can pick whichever you like.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, exactly what I wanted.
3

You can always use the .configure(text = "new text") method, as iCodez suggested.

Alternatively, try using a StringVar as the text_variable parameter:

my_text_var = StringVar(frame_gm1_b)
my_text_var.set("Current input is:\t %s"% str(save_game[6]))
gm1_b_current_choice_label = Label(frame_gm1_b, textvariable = my_text_var)

Then, you can change the text by directly altering my_text_var:

my_text_var.set("Some new text")

This can be linked to a button or another event-based widget, or however else you want to change the text.

1 Comment

That's an interesting way to do it. Will probably end up using it at some point in the future, for now, the simple .config works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.