PyGobject(七十三)Gtk.Widget之Gtk.SpinButton

本文详细介绍了Gtk.SpinButton的使用方法,包括其继承关系、方法、属性和信号等内容,并提供了一个具体的Python示例,展示了如何创建和配置SpinButton。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gtk.SpinButton

Gtk.SpinButton带有加减号的输入框,输入内容必须在给定的范围内

继承关系

Gtk.SpinButton是Gtk.Entry的直接子类
这里写图片描述

Methods

方法修饰词方法名及参数
staticnew (adjustment, climb_rate, digits)
staticnew_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

NameTypeFlagsShort Description
adjustmentGtk.Adjustmentr/wThe adjustment that holds the value of the spin button
climb-ratefloatr/w/enThe acceleration rate when you hold down a button
digitsintr/w/enThe number of decimal places to display
numericboolr/w/enWhether non-numeric characters should be ignored
snap-to-ticksboolr/w/enWhether erroneous values are automatically changed to a spin button’s nearest step increment
update-policyGtk.SpinButtonUpdatePolicyr/w/enWhether the spin button should update always, or only when the value is legal
valuefloatr/w/enReads the current value, or sets a new value
wrapboolr/w/enWhether a spin button should wrap upon reaching its limits

Signals

NameShort Description
change-valueThe ::change-value signal is a keybinding signal which gets emitted when the user initiates a value change.
inputThe ::input signal can be used to influence the conversion of the users input into a double value.
outputThe ::output signal can be used to change to formatting of the value that is displayed in the spin buttons entry.
value-changedThe ::value-changed signal is emitted when the value represented by spinbutton changes.
wrappedThe ::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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sanxiaochengyu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值