【Tkinter Series 10/15】Widget (Spin)

23. WidgetsSpinbox

This widget allows the user to select a value from a given collection. The range of these values ​​can be a number or a fixed set of strings.Spinbox

On the screen, a has an area showing the current value and a pair of arrows.Spinbox

  • The user can click the up arrow to advance the value sequentially to the next higher value. If the value has already reached the maximum value, you can set the widget, if you wish, so that the new value will wrap closer to the minimum value.

  • The user can click the down arrow to advance the value to the next lower value in sequence. This arrow can also be configured to wrap around, so if the current value is the lowest, clicking the down arrow will display the highest value.

  • The user can also enter values ​​directly, placing the widget as if it were one. The user can move focus to the widget (see Section 53, “ Focus: Routing keyboard input” ) by clicking on it or using Tab or Shift -Tab, then edit the displayed value.Entry

Create a new widget as a child window of the root window or frame:Spinboxparent

w = tk.Spinbox(parent, option, ...)

The constructor returns the new widget. Options include:Spinbox

Table 32. 旋转框Widget options

activebackground The cursor is positioned over the control; see Section 5.3 "Colors".
bgorbackground The background color of the widget .
bdorborderwidth The width of the border around the widget; see Section 5.1, “Sizes”. The default is <> pixels.
buttonbackground The background color shown on the arrow. The default value is gray.
buttoncursor Cursor arrow to display on mouseover; see Section 5.8, “Cursors”.
buttondownrelief Downward-pointing relief style arrow; see Section 5.6, “Relief Styles”. This is the default style.tk.RAISED
buttonup An upward-pointing relief style arrow; see Section 5.6, “Relief Styles”. This is the default style.tk.RAISED
command Use this option to specify a function or method to call whenever the user clicks one of the arrows. Note that when the user edits the direct value, it appears as if it were a .Entry
cursor The cursor shown when the mouse is selected is located in the entry part of the widget; see Section 5.8, “Cursors”.
disabledbackground These options select the background and foreground when the widget is a .statetk.DISABLED
disabledforeground
exportselection Typically, text in entry sections can be cut and pasted. To disable this behavior, set this option to .SpinboxexportselectionTrue
font Use this option for entry text; see Section 5.4, “Type Fonts”.
fgorforeground This option selects the arrow used to display the text and color in the entry section of the widget.
format Use this option to control the numeric value in combination with and options. For example, will display the value as a ten-character field with four digits after the decimal point.from_toformat='%10.4f'
from_ Use this option in conjunction with options (described below) to constrain the values ​​of the numeric range. For example, and would only allow values ​​between 1 and 9 (inclusive of <> and <>). See the options below.tofrom_=1to=9increment
highlightbackground Color  Focus Highlights when there is no focus. See Section 53, “Focus: Routing Keyboard Input”.Spinbox
highlightcolor Color  Focus Highlights when there is  focus .Spinbox
highlightthickness The thickness of the focal point is highlighted. The default value is . Set to suppress focus highlighting.10
increment When constraining a value using the and option, you will have the option to specify how much the value increases or decreases when the user clicks the arrow. For example with options , and , the up arrow will step through the values ​​0.0, 0.5, 1.0, 1.5, and 2.0.from_toincrementfrom_=0.0to=2.0increment=0.5
insertbackground Select the color  to display the caret widget in the entry section of the entry.
insertborderwidth This option controls the width of the border around the caret cursor. Normally, the caret will have no border. If this option is set to a non-zero value, the caret cursor will be displayed in embossed style .tk.RAISED
insertofftime These two options control the caret: how long it takes and how long it takes in milliseconds, respectively. For example, with options and, the cursor will blink off for 0.2 seconds, then on for 0.4 seconds.insertofftime=200insertontime=400
insertontime
insertwidth Use this option to specify the caret cursor; see Section 5.1 , “Sizes” for possible values . The default width is <> pixels.
justify This option controls the position of the text in the entry section of the widget. Values ​​may be to left-align text; center; or right-align text.tk.LEFTtk.CENTERRIGHT
readonlybackground This option specifies the background color when the widget is; see Section 5.3, “Colors”.state'readonly'
relief Use this option for controls; see Section 5.6, “Relief Styles”. The default style is .tk.SUNKEN
repeatdelay These options specify mouse clicks on the arrow; values ​​are in milliseconds. A value specifies how long the mouse button must be held down before repeating, and specifies that the function repeats. The default values ​​are 400 and 100 milliseconds respectively.repeatdelayrepeatinterval
repeatinterval
selectbackground The background color used to display selected items .
selectborderwidth The border width item to display around the selection.
selectforeground The foreground color used to display selected items.
state Usually, widgets are created in states. Setting this option makes the widget unresponsive to mouse or keyboard actions. If it is set to , the value in the widget's entry section cannot be modified using keystrokes, but the value can still be copied to the clipboard and the widget still responds to clicks on the arrow.Spinboxtk.NORMALtk.DISABLED'readonly'
takefocus Typically, the entry part of a widget can have focus (see Section 53, “Focus: Routing Keyboard Input” ). Remove widget sequence from focus traversal, set.Spinboxtakefocus=False
textvariable If you want to retrieve the widget, you can use the method below, or you can associate a control variable with the widget passing that control variable as the value of this option. See Section 52, “Control Variables: Values ​​Behind Widgets”..get()
to This option specifies the upper value of the range. See options, above, and options.from_increment
values 有两种方法可以指定可能的值 的小部件。一种方法是提供一个元组 字符串作为选项的值。例如,将只允许这三个 字符串作为值。将小组件配置为 接受一系列数值,请参阅上面的选项。valuesvalues=('red', 'blue', 'green')from_
width 使用此选项可指定字符数 允许在小部件的入口部分。这 默认值为 20。
wrap 通常,当小部件处于其最大值时, 向上箭头不执行任何操作,当小部件 处于其最低值,向下箭头 无。如果选择 , 向上箭头将从最高处前进 值回到最低值,向下箭头 将从最低值前进回 最高。wrap=True
xscrollcommand 使用此选项将滚动条连接到条目 小部件的一部分。有关细节,请参见第 22.2 节 “将滚动条连接到另一个滚动条” 小部件”。

这些方法在小部件上可用:Spinbox

.bbox(index)

此方法返回字符的边界框 在小部件的入口部分中的位置。这 结果是一个元组,其中的值是左上角的和坐标,以及 字符的宽度和高度(以像素为单位),其中 次序。index(xywh)xy

.delete(first, last=None)

此方法从 的条目部分删除字符 这。和 的值是 以 Python 切片的标准方式解释。Spinboxfirstlast

.get()

此方法返回 的值。该值始终作为 字符串,即使小部件设置为包含 数。Spinbox

.icursor(index)

使用此方法将插入光标定位在 由 指定的位置,使用标准 位置的 Python 约定。index

.identify(xy)

给定一个职位 小部件,此方法返回一个字符串,描述什么 在那个位置。值可以是以下任何值:(xy)

  • 'entry'对于入口区域。

  • 'buttonup'对于向上指向 箭头。

  • 'buttondown'对于 向下的箭头。

  • ''(空字符串)如果这些 坐标不在微件内。

.index(i)

此方法返回 指数。 参数可以是以下任何一种:i

  • tk.END获得职位后 条目的最后一个字符。

  • tk.INSERT要获得位置 插入光标。

  • tk.ANCHOR要获得位置 选择锚点。

  • tk.SEL_FIRST'获得位置 所选内容的开始。如果选择是 不在小部件中,此方法会引发异常。tk.TclError

  • tk.SEL_LAST获得职位 刚刚过了选择的结束。如果 选择不在小部件中,此方法 引发异常。tk.TclError

  • 形式为 “” 的字符串表示小部件中的坐标。返回值 是包含该字符的字符的位置 坐标。如果坐标在 小部件,返回值将是 最接近该字符的位置 位置。@xx

.insert(indextext)

此方法插入字符串中的字符 由 指定的位置。为了可能 索引值,请参阅方法 以上。textindex.index()

.invoke(element)

调用此方法以获得与用户相同的效果 单击箭头。该参数适用于向上箭头和向下箭头。element'buttonup''buttondown'

.scan_dragto(x)

This method works the same as the method described in Section 10, "Entry Widget" ..scan_dragto()

.scan_mark(x)

This method works the same as the method described in Section 10, "Entry Widget" ..scan_mark()

.selection('from', index)

Sets the selection anchor in the widget to the position specified by . See method above for possible values. The initial value of the selection anchor is 0.indexindex.index()

.selection('to', index)

Choose Select Anchor and View.index

.selection('range', startend)

Select and index the text between. For allowed index values, see method above.startend.index()

.selection_clear()

Clear selection.

.selection_get()

Returns the selected text. This method throws an exception if there is no current selection.tk.TclError

Guess you like

Origin blog.csdn.net/gongdiwudu/article/details/132602409