Reputation: 129
I have read a good solution for establishing a default numerical value on a Tkinter Spinbox widget when you use the from_ to options. But I have not seen a single one that can help establish a value (numerical or string) from a tuple.
My code is as follows, and is intended to set a default value in a Tkinter Spinbox widget from a tuple:
from Tkinter import *
root = Tk()
root.geometry('200x200+50+50')
root.title('Prueba')
t = ('One', 'Two', 'Three', 'Four', 'Five')
v = t[3]
var = StringVar()
var.set(v)
sb = Spinbox(root, values=t, textvariable=var, width=10)
sb.place(x=5, y=15)
root.mainloop()
Another variant that I have tried is the following:
from Tkinter import *
root = Tk()
root.geometry('200x200+50+50')
root.title('Prueba')
var = StringVar()
var.set('Four')
sb = Spinbox(root, ('One', 'Two', 'Three', 'Four', 'Five'), textvariable=var, width=10)
sb.place(x=5, y=15)
root.mainloop()
The only way the set method works on Spinbox (and which I took from here) is the following and only works with numbers and within a range established as options in the Spinbox widget:
from Tkinter import *
root = Tk()
root.geometry('200x200+50+50')
root.title('Prueba')
var = StringVar()
var.set(4)
sb = Spinbox(root, from_=1, to=5, textvariable=var, width=10)
sb.place(x=5, y=15)
root.mainloop()
Can please anyone help me to find out how to establish a default value in a Tkinter Spinbox from a Tuple? I will appreciate that greatly!
Upvotes: 3
Views: 5286
Reputation: 385880
If you move the line var.set(v)
to be after creating the widget, the default value will be set.
var = StringVar()
sb = Spinbox(root, values=t, textvariable=var, width=10)
var.set(v)
Upvotes: 4