0
votes

I am having some trouble in tkinter Python. I have a value from a stringvar that I need updated when pressing a button. It is working when I only use the StringVar as seen below textvariable=newstring, however I also want some text behind the StringVar text="I have %s items" % (newstring), but if I do this the result when I run is: "I have PY_VAR22 items" but when I use newstring on its own it gives me the value and even updates when I press on Update. What am I doing wrong? Please help. Thanks

newstring = StringVar()
value1 = (value2 - 3) * 0.333  
newstring.set(round(value1,2))

ref2 = Label (f1, textvariable=newstring, bg='white',fg='black',font='none 12 italic')
ref2.grid(row=1, column=4)

def update_value():
    value1 = (495 - 3) * 0.333
    newstring.set(value1)

Button(Bottom1, text="Update", width=10, command=update_value).grid(row=0, column=5)

ncica answer works but I still get ValueError: could not convert string to float: 'I have 32.26 items', its because I am using the 32.26 to calculate something in another field.

btnTotal=Button(f1,padx=13,pady=2,bd=16,fg='black',font=('Helvetica',14,'bold'), width=10, text='Calculate',bg='blue',command=Ref). grid(row=10,column=3) 

def Ref(): 
   Gold333 = float(First.get())
   Costof333 = Gold333 * float(newstring.get())
   fif = str("Total: €%s" % round(Costof333,2))
   Fifth.set(fif)

The only solution I have is make a separate label for the text I want to add and place it before the variable text, but I really dont want to do it like this.

1

1 Answers

0
votes

instead of:

newstring.set("I have %s items" % (newstring))

use:

newstring.set("I have %s items" % (value1))

code:

newstring = StringVar()
value1 = (value2 - 3) * 0.333
newstring.set("I have %s items" % (round(value1,2)))

ref2 = Label (f1, textvariable=newstring, bg='white',fg='black',font='none 12 italic')
ref2.grid(row=1, column=4)

def update_value():
    value1 = (495 - 3) * 0.333
    newstring.set("I have %s items" % (value1))

Button(f1, text="Update", width=10, command=update_value).grid(row=0, column=5)