git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tkinter.tix "Control" widget don't work with StringVar?


On 2019-06-02 02:28, jfong at ms4.hinet.net wrote:
> Below is a simplified version of the sample script downloaded from its package.
> When run it, the entry field display '0' instead of 'P&W'.
> PS. I am using Python 3.4.4, Windows 32bit
> 
> ----test0.py----
>   1 from tkinter import tix as Tix
>   2
>   3 root = Tix.Tk()
>   4
>   5 demo_maker = Tix.StringVar()
>   6 demo_maker.set('P&W')
>   7
>   8 c = Tix.Control(root, label='Engine Maker: ',
>   9                variable=demo_maker,
> 10                options='entry.width 10 label.width 20 label.anchor e')
> 11 c.pack(side=Tix.TOP, anchor=Tix.W)
> 12
> 13 root.mainloop()
> --------
> 
> I run it under pdb, try to figure out where the problem is:
> 
> D:\Works\Python>py -m pdb test0.py
>> d:\works\python\test0.py(1)<module>()
> -> from tkinter import tix as Tix
> (Pdb) tbreak 13
> Breakpoint 1 at d:\works\python\test0.py:13
> (Pdb) cont
> Deleted breakpoint 1 at d:\works\python\test0.py:13
>> d:\works\python\test0.py(13)<module>()
> -> root.mainloop()
> (Pdb) !c['value']
> '0'
> (Pdb) !demo_maker.get()
> 'P&W'
> (Pdb) quit
> 
> If I change line 8 to "c = Tix.Control(root, label='Engine Maker: ', value='P&W',", the result is very interest:
> 
> D:\Works\Python>py -m pdb test0.py
>> d:\works\python\test0.py(1)<module>()
> -> from tkinter import tix as Tix
> (Pdb) tbreak 13
> Breakpoint 1 at d:\works\python\test0.py:13
> (Pdb) cont
> Deleted breakpoint 1 at d:\works\python\test0.py:13
>> d:\works\python\test0.py(13)<module>()
> -> root.mainloop()
> (Pdb) !c['value']
> '0'
> (Pdb) !demo_maker.get()
> '0'
> (Pdb) quit
> 
> Anyone can help?
> 
 From the documentation:

"""class tkinter.tix.Control
The Control widget is also known as the SpinBox widget. The user can 
adjust the value by pressing the two arrow buttons or by entering the 
value directly into the entry. The new value will be checked against the 
user-defined upper and lower limits."""

In other words, the widget that you're using is for entering a _number_; 
you can click the up and down buttons to change its value. You usually 
use it with tix.IntVar.

You're using it with tix.StringVar, which is for strings, and because 
it's unable to interpret the value as a number, it's pretending that 
it's 0 instead. You can, however, set it to, say, '1', or '01', and the 
widget will then show the number 1.