Eli Kerrison-Male Eli Kerrison-Male - 13 days ago 7
Python Question

How to make IntVar and StringVar display in the same widget?

So, I am trying make a label that displays both StringVar and an IntVar, though when I try to put them in, like so...

from tkinter import *
from tkinter import ttk

root = Tk()
parent = ttk.Frame(root)
parent.grid()

string = StringVar()
integer = IntVar()

string.set("Blah =")
integer.set(10)

both = str((string.get(), str(integer.get())))
print(both)

both = StringVar()
both.set((string.get(), str(integer.get())))
print(both.get())

label = ttk.Label(parent, textvariable=(both))
label.grid()

root.mainloop()


Now, this is the closest I was able to get, because the other ways didn't even display the textvariables in the widget.
For some reason the code when printed returns: ('Blah =', '10').

Like a list that has been printed. But the label displays: {Blah =} 10

Can someone please tell me why it displays with these instead of just displaying the variables.

Answer

I think you are looking for how to format a string.

both = StringVar()
both.set( "{}{}".format( string.get(), integer.get() ) )
Comments