Python Forum
Dynamic button text - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Dynamic button text (/thread-7379.html)



Dynamic button text - Philbot - Jan-07-2018

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?


RE: Dynamic button text - Gribouillis - Jan-07-2018

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])
...



RE: Dynamic button text - Philbot - Jan-07-2018

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?


RE: Dynamic button text - Gribouillis - Jan-07-2018

StringVar is in tkinter. If you import tkinter as tk you need a qualified name tk.StringVar()


RE: Dynamic button text - Philbot - Jan-08-2018

Brillant well done that man!! Wondered why it never showed up as I was typing it now I know.

Thanks a mill