Python Forum
drawing textvariable from Button inside canvas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
drawing textvariable from Button inside canvas
#4
This is a short example that demonstrates why you cannot do what you are trying to do.
import tkinter as tk

root = tk.Tk()
stringvar = tk.StringVar()
a = 'AAAAA'
stringvar.set(a)
button = tk.Button(root, textvariable=stringvar)
print(a, stringvar.get())
print(id(a), id(stringvar.get()))
Output:
AAAAA AAAAA 1925086754992 1925089589232
Even though "a" and "stringvar.get()" both reference a str "AAAAA", the two are different objects. "a" references some str object 1925086754992 while "stringvar.get() references a completely different str object 192508958923. So even if you could change the letters in str object 1925086754992 those changes would not be seen by stringvar and would not change the button text.

But the point is moot because str is an immutable type. You cannot change a str. I can make "a" reference a different string object, but "stringvar" does not know about that string, nor does it know about "a", so any change to "a" is not going to change the text in my button.

If you want to change the text in the button you have to do one of these:
stringvar.set('new string')
button['text'] = 'new string'
button.configure(text='new string')
That's it. There are no other ways.
Reply


Messages In This Thread
RE: drawing textvariable from Button inside canvas - by deanhystad - Apr-25-2021, 12:18 AM

Forum Jump:

User Panel Messages

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