Gtk.SpinButton
Gtk.SpinButton带有加减号的输入框,输入内容必须在给定的范围内
继承关系
Gtk.SpinButton是Gtk.Entry的直接子类
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new (adjustment, climb_rate, digits) |
static | new_with_range (min, max, step) |
configure (adjustment, climb_rate, digits) | |
get_adjustment () | |
get_digits () | |
get_increments () | |
get_numeric () | |
get_range () | |
get_snap_to_ticks () | |
get_update_policy () | |
get_value () | |
get_value_as_int () | |
get_wrap () | |
set_adjustment (adjustment) | |
set_digits (digits) | |
set_increments (step, page) | |
set_numeric (numeric) | |
set_range (min, max) | |
set_snap_to_ticks (snap_to_ticks) | |
set_update_policy (policy) | |
set_value (value) | |
set_wrap (wrap) | |
spin (direction, increment) | |
update () |
Virtual Methods
do_change_value (scroll) |
do_input (new_value) |
do_output () |
do_value_changed () |
do_wrapped () |
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
adjustment | Gtk.Adjustment | r/w | The adjustment that holds the value of the spin button |
climb-rate | float | r/w/en | The acceleration rate when you hold down a button |
digits | int | r/w/en | The number of decimal places to display |
numeric | bool | r/w/en | Whether non-numeric characters should be ignored |
snap-to-ticks | bool | r/w/en | Whether erroneous values are automatically changed to a spin button’s nearest step increment |
update-policy | Gtk.SpinButtonUpdatePolicy | r/w/en | Whether the spin button should update always, or only when the value is legal |
value | float | r/w/en | Reads the current value, or sets a new value |
wrap | bool | r/w/en | Whether a spin button should wrap upon reaching its limits |
Signals
Name | Short Description |
---|---|
change-value | The ::change-value signal is a keybinding signal which gets emitted when the user initiates a value change. |
input | The ::input signal can be used to influence the conversion of the users input into a double value. |
output | The ::output signal can be used to change to formatting of the value that is displayed in the spin buttons entry. |
value-changed | The ::value-changed signal is emitted when the value represented by spinbutton changes. |
wrapped | The ::wrapped signal is emitted right after the spinbutton wraps from its maximum to minimum value or vice-versa. |
例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/14
# section 117
TITLE = "SpinButton"
DESCRIPTION = """
A Gtk.SpinButton is an ideal way to allow the user to set the value of some attribute.
Rather than having to directly type a number into a Gtk.Entry, Gtk.SpinButton allows
the user to click on one of two arrows to increment or decrement the displayed value.
"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class SpinButtonWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="SpinButton Demo")
self.set_border_width(10)
hbox = Gtk.Box(spacing=6)
self.add(hbox)
# lower<value<upper-page_size
# step_increment鼠标左键(或者键盘上下键)按下增加或减少的值.page_increment键盘PageDown或PageUp按下增加或减少的值
# 当鼠标右键按下时,直接显示最大值或者最小值
adjustment = Gtk.Adjustment(value=0, lower=0, upper=20, step_increment=1, page_increment=2, page_size=1)
self.spinbutton = Gtk.SpinButton()
self.spinbutton.set_adjustment(adjustment)
self.spinbutton.connect("value-changed", self.change_value)
hbox.pack_start(self.spinbutton, False, False, 0)
check_numeric = Gtk.CheckButton("Numeric")
check_numeric.connect("toggled", self.on_numeric_toggled)
hbox.pack_start(check_numeric, False, False, 0)
check_ifvalid = Gtk.CheckButton("If Valid")
check_ifvalid.connect("toggled", self.on_ifvalid_toggled)
hbox.pack_start(check_ifvalid, False, False, 0)
# 如果勾选,只能输入数字,其它字符不能输入
def on_numeric_toggled(self, button):
self.spinbutton.set_numeric(button.get_active())
# 如果勾选,只有合法的数值才会更新.输入不合法的数值,还是显示原来的数值.
# 当不勾选时,输入不合法的数值,第一次显示upper-page_size,后面就显示输入的数值
def on_ifvalid_toggled(self, button):
if button.get_active():
policy = Gtk.SpinButtonUpdatePolicy.IF_VALID
else:
policy = Gtk.SpinButtonUpdatePolicy.ALWAYS
self.spinbutton.set_update_policy(policy)
@staticmethod
def change_value(widget):
print("change_value", widget.get_value())
def main():
win = SpinButtonWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
代码下载地址:http://download.youkuaiyun.com/detail/a87b01c14/9594728