Python Forum

Full Version: Dynamic button text
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi I am putting some code together that uses a pickle file that saves my data from previous session. I have a set up page that allows the users to change the text on the buttons but I am having way to much trouble trying to get the buttons to update using a textvariable and been on the google all day almost

Hope some one can point me in the right direction please.

self.MyList = loaddata()
        print(self.MyList)

        self.text1=((self.MyList[4]))
        print(self.text1)


   



        self.button1 = tk.Button(self.frame, textvariable  = self.text1, width = 25, command=self.capture_asset)
        self.button1.grid(row=2, column=3) 
Mylist comes from the pickle file when I print self.text1 is fine and shows the text string I want to put on the button but the button shows a blank text?
If I remember well, the textvariable parameter in tkinter widgets needs a tkinter StringVar value. It should probably be

self.text1 = tk.StringVar()
self.text1.set(self.Mylist[4])
...
Thanks for the suggestion but I tried that one and it doesn't look to like StringVar saying unresolved reference and error is stringvar not defined?
StringVar is in tkinter. If you import tkinter as tk you need a qualified name tk.StringVar()
Brillant well done that man!! Wondered why it never showed up as I was typing it now I know.

Thanks a mill