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
#5
(Apr-25-2021, 12:18 AM)deanhystad Wrote: 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.

Thanks for the pointer, that helps! The master has taught the student. I assume that these little caveats and that bit of knowledge has a very long story attached in how you uncovered that.

Though I Am seeing that some folks are writing companion libraries that address some of these deficiencies and even ttk is an improvement over tk in ease of use and functionality options even if they did change how you have to structure some of the arguments. But I have learned through many pains and struggles, you stick to the standards, you stick to the common things and you will find parts and help waaaaay more plentiful rather than if you are using a one off.

I learned that the hard way with the wiimote library I used in my predator cannon armature lights/sounds/movement script. The library development was basically dead. The new wiimotes would not work as Nintendo changed the protocols. I hated to do it but I had to order units out of china as the newer 2-n-1 units were not compatible. It took me three months just to hash out how on bloody earth to make things work with libraries out there and I then find out later that adafruit in august or there abouts wrote an updated nunchuck library, but even it had the i2c clock stretching issue and the only fix for that is to use alternate pins and use software i2c. Something to do with the BCM2835? chipset and a flaw in the ability to handle stretched clock cycles in the communications protocol. Soo instead of a nunchucky with the raspberry pi I went with a wiimote over Bluetooth and it worked after I trusted the device (not paired or any other option) via the bluetoothctl control interface.
Reply


Messages In This Thread
RE: drawing textvariable from Button inside canvas - by knoxvilles_joker - Apr-25-2021, 02:48 AM

Forum Jump:

User Panel Messages

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