Python - Tkinter Spinbox: Spinbox小部件是一个标准的Tkinter的Entry小窗口部件的变体,它可以用来选择从一个固定的值.
Spinbox小部件是一个标准的Tkinter的Entry小窗口部件的变体,它可以用来选择从一个固定的值.
语法:
这里是一个简单的语法来创建这个widget:
w = Spinbox( master, option, ... )
参数:
-
master: 这代表了父窗口.
-
options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.
Option | Description |
---|---|
activebackground | The color of the slider and arrowheads when the mouse is over them. |
bg | The color of the slider and arrowheads when the mouse is not over them. |
bd | The 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. |
command | A procedure to be called whenever the scrollbar is moved. |
cursor | The cursor that appears when the mouse is over the scrollbar. |
disabledbackground | The background color to use when the widget is disabled. |
disabledforeground | The text color to use when the widget is disabled. |
fg | Text color. |
font | The font to use in this widget. |
format | Format string. No default value. |
from_ | The minimum value. Used together with to to limit the spinbox range. |
justify | Default is LEFT |
relief | Default is SUNKEN. |
repeatdelay | Together with repeatinterval, this option controls button auto-repeat. Both values are given in milliseconds. |
repeatinterval | See repeatdelay. |
state | One of NORMAL, DISABLED, or "readonly". Default is NORMAL. |
textvariable | No default value. |
to | See from. |
validate | Validation mode. Default is NONE. |
validatecommand | Validation callback. No default value. |
values | A tuple containing valid values for this widget. Overrides from/to/increment. |
vcmd | Same as validatecommand. |
width | Widget width, in character units. Default is 20. |
wrap | If true, the up and down buttons will wrap around. |
xscrollcommand | Used 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. |
例子:
自行尝试下面的例子:
from Tkinter import * master = Tk() w = Spinbox(master, from_=0, to=10) w.pack() mainloop()
这将产生以下结果: