숫자를 입력하거나 -/+ 단추를 눌러 입력 숫자를 선택합니다!
from gi.repository import Gtk
import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="SpinButton Example", application=app)
self.set_default_size(210, 70)
self.set_border_width(5)
# an adjustment (initial value, min value, max value,
# step increment - press cursor keys or +/- buttons to see!,
# page increment - not used here,
# page size - not used here)
ad = Gtk.Adjustment(0, 0, 100, 1, 0, 0)
# a spin button for integers (digits=0)
self.spin = Gtk.SpinButton(adjustment=ad, climb_rate=1, digits=0)
# as wide as possible
self.spin.set_hexpand(True)
# we connect the signal "value-changed" emitted by the spinbutton with the callback
# function spin_selected
self.spin.connect("value-changed", self.spin_selected)
# a label
self.label = Gtk.Label()
self.label.set_text("Choose a number")
# a grid to attach the widgets
grid = Gtk.Grid()
grid.attach(self.spin, 0, 0, 1, 1)
grid.attach(self.label, 0, 1, 2, 1)
self.add(grid)
# callback function: the signal of the spinbutton is used to change the
# text of the label
def spin_selected(self, event):
self.label.set_text(
"The number you selected is " + str(self.spin.get_value_as_int()) + ".")
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
Gtk.Adjustment는 Gtk.SpinButton을 만들떄 씁니다. 이 객체는 상한값, 하한값, 단계값, 페이지 증가 값, 페이지 크기를 나타내며 Gtk.Adjustment(value, lower, upper, step_increment, page_increment, page_size)
생성자로 만듭니다. 여기서 필드 값은 float
입니다. step_increment
은 커서 키를 사용할 떄 또는 SpinButton의 증감 단추를 사용할 때 가져올 증가 감소 값입니다. 참고로, page_increment
와 page_size
는 이 상황에 활용하지 않으므로 0
값으로 설정해야합니다.
23번째 줄에서 "value-changed"
시그널은 widget.connect(signal, callback function)
함수로 spin_selected()
콜백 함수에 연결했습니다. 더 자세한 설명은 를 참조하십시오.
SpinButton 값이 최대 최소 값을 넘어설 경우 시작 값 내지는 끝 값으로 돌아가게 하려면 set_wrap(True)
함수를 설정하십시오. 이 상황이 일어나면 "wrapped"
시그널을 내보냅니다.
set_digits(digits)
함수는 SpinButton에 나타낼 수 있는 자리수를 최대 20자리까지 설정합니다.
SpinButton에서 정수 값을 받으려면 get_value_as_int()
함수를 사용하십시오.
이 예제는 다음 참고자료가 필요합니다:
GtkSpinButton
GtkAdjustment