Python的GUI编程(七)Spinbox(自设值)

Spinbox小部件是一个标准的Tkinter的 Entry小窗口部件的变体,它可以用来选择一个固定的值.
当用户只需要从极少的数值中进行选择的时候,就可以使用Spinbox取代Entry。

语法:

这里是一个简单的语法来创建这个widget:

w = Spinbox( master, option, ... )

参数:

  • master: 这代表了父窗口.

  • options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.

OptionDescription
activebackground             The color of the slider and arrowheads when the mouse is over them.
bgThe color of the slider and arrowheads when the mouse is not over them.
bdThe width of the 3-d borders around the entire perimeter of the trough, and also the width of the 3-d effects on the arrowheads and slider. Default is no border around the trough, and a 2-pixel border around the arrowheads and slider.
commandA procedure to be called whenever the scrollbar is moved.
cursorThe cursor that appears when the mouse is over the scrollbar.
disabledbackgroundThe background color to use when the widget is disabled.
disabledforegroundThe text color to use when the widget is disabled.
fgText color.
fontThe font to use in this widget.
formatFormat string. No default value.
from_The minimum value. Used together with to to limit the spinbox range.
justifyDefault is LEFT
reliefDefault is SUNKEN.
repeatdelayTogether with repeatinterval, this option controls button auto-repeat. Both values are given in milliseconds.
repeatintervalSee repeatdelay.
stateOne of NORMAL, DISABLED, or "readonly". Default is NORMAL.
textvariableNo default value.
toSee from.
validateValidation mode. Default is NONE.
validatecommandValidation callback. No default value.
valuesA tuple containing valid values for this widget. Overrides from/to/increment.
vcmdSame as validatecommand.
widthWidget width, in character units. Default is 20.
wrapIf true, the up and down buttons will wrap around.
xscrollcommandUsed to connect a spinbox field to a horizontal scrollbar. This option should be set to the set method of the corresponding scrollbar.

方法:

spinbox对象有这些方法:

Methods & Description
delete(startindex [,endindex])
This method deletes a specific character or a range of text.
get(startindex [,endindex])
This method returns a specific character or a range of text.
identify(x, y)
Identifies the widget element at the given location.
index(index)
Returns the absolute value of an index based on the given index.
insert(index [,string]...)
This method inserts strings at the specified index location.
invoke(element)
Invokes a spinbox button.
同ListBox控件中的部分属性一样,有from_,to(表示范围),但有increment表示增量。
from Tkinter import *
root=Tk()

Spinbox(root,from_=-100,to=100,increment=2).pack()
root.mainloop()
 
设置Spinbox的值,设置 属性values,设置此值后,每次更新值将使用values指定的值
from Tkinter import *
root=Tk()

# value是个元组,并且increment无论是多少不会再影响up或down的值只会依次按照元组中的数前后移动
Spinbox(root,value=(2,6,0,-8,9),increment=10).pack()
root.mainloop()

Spinbox 绑定变量
from Tkinter import *
root=Tk()
v=StringVar()
v1=StringVar()
# value是个元组,并且increment无论是多少不会再影响up或down的值
Spinbox(root,value=(2,6,0,-8,9),increment=10,textvariable=v).pack()
v.set(9)
Spinbox(root,value=(2,6,0,-8,9),increment=10,textvariable=v1).pack()
# 设置的值不在value元组中,但是7占据第一个索引位置,up一下显示的是6,但实际上7未在元组中在down下显示的是2
v1.set(7)
root.mainloop()

from Tkinter import *
root=Tk()

v=IntVar()

La=Label(root,width=5,fg='blue')
La.config(textvariable=v,width=3)
Entry(root,textvariable=v,width=3).pack()
La.pack()

Spinbox(root,from_=-100,to=100,increment=2,textvariable=v).pack()

root.mainloop()

知识点:获取部分控件中文本值的方法。

text    要显示的文本

textvariable    变量文本

绑定的变量可以通过textvariable或text属性传递给其他控件作为文本显示,并且是动态的。

如Lable控件中只能是textvariable属性获得动态的文本,Entry可以是text或textvariable属性。

get()可以获得控件中文本值,但不能传递给text或textvariable属性给其他控件???

设置Spinbox的回调函数
from Tkinter import *
root=Tk()

v=IntVar()

def Call():
    print Sp.get()

#使用绑定变量获取
Label(root,textvariable=v,width=3).pack()
#Label(root,text=v,width=6).pack()

Entry(root,textvariable=v,width=3).pack()
Entry(root,text=v,width=3).pack()
Sp=Spinbox(root,from_=-100,to=100,increment=2,textvariable=v,command=Call)
Sp.pack()

#使用对象获取
Label(root,textvariable=Sp.get(),width=3).pack()
# Label(root,text=Sp.get(),width=6).pack()

Entry(root,textvariable=Sp.get(),width=3).pack()
Entry(root,text=Sp.get(),width=3).pack()

root.mainloop()

 
删除Spinbox指定位置的字符
def delete(self, first, last=None):
    

删除spinbox的一个或多个元素。

首先是要删除的第一个字符的索引,最后是字符的索引。

最后一个删除。如果最后没有指定它。

默认值为first +1,即单个字符。

删除。这个命令返回一个空字符串。

return self.tk.call( self._w, 'delete', first, last) 很奇诡的一种方法:
当Spinbox中的初始值是1234,对象delete(0--3)对应着数字1234的索引,若delete(0),up一下,值变为235,delete(1),up一下,值变为135.
若delete(0)两下,则会在原来的数字上增加,不改变位数,即1234,up后1235,但是初始值是34?????
from Tkinter import *

root = Tk()

sb = Spinbox(root,
             from_=1234,  # 最小值
             to=2000,  # 最大值
             increment=1
             )
sb.delete(0)
sb.delete(0)
print sb.get()
sb.pack()
root.mainloop()
使用元组value给定值时:
 
from Tkinter import *
root = Tk()

def Call():
    sb.delete(0)
    print sb.get()

sb = Spinbox(root,
             from_=1234,  # 最小值
             to=2000,  # 最大值
             increment=1,
             value=(232,4345,2126,42357,4323),
             command=Call
             )

sb.pack()
root.mainloop()
输出:
345
126
2357
323
323

在Spinbox指定位置插入文本
 
insert()方法
from Tkinter import *
root = Tk()

def Call():
    sb.insert(END,'.0')
    print sb.get()

sb = Spinbox(root,
             from_=1234,  # 最小值
             to=2000,  # 最大值
             increment=1,

             command=Call
             )

sb.pack()
root.mainloop()
输出:
 
1235.0
1236.0
1237.0
1238.0
1239.0
1240.0  .....

delete与insert是针对每个值中的字符,但是容易出现问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HySmiley

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值