간단한 콜백 함수에 연결한 단추 위젯입니다.
from gi.repository import Gtk
import sys
class MyWindow(Gtk.ApplicationWindow):
# a window
def __init__(self, app):
Gtk.Window.__init__(self, title="GNOME Button", application=app)
self.set_default_size(250, 50)
# a button
button = Gtk.Button()
# with a label
button.set_label("Click me")
# connect the signal "clicked" emitted by the button
# to the callback function do_clicked
button.connect("clicked", self.do_clicked)
# add the button to the window
self.add(button)
# callback function connected to the signal "clicked" of the button
def do_clicked(self, button):
print("You clicked me!")
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)
16번째 줄에서, widget.connect(signal, callback function)
코드로 "clicked"
시그널을 do_clicked()
콜백 함수에 연결했습니다. 더 자세한 설명은 를 참조하십시오.
set_relief(Gtk.ReliefStyle.NONE)
함수는 Gtk.Button의 가장자리의 두드러진 모양새를 없음으로 설정합니다. Gtk.ReliefStyle.NORMAL
과는 반대죠.
단추 레이블에 스톡 아이콘이라면, set_use_stock(True)
함수로 스톡 아이콘 이름을 레이블로 설정합니다.
button
단추 레이블에 그림(예: 스톡 그림)을 설정하려면:
image = Gtk.Image()
image.set_from_stock(Gtk.STOCK_ABOUT, Gtk.IconSize.BUTTON)
button.set_image(image)
이 코드 다음에 단추에 레이블 문자열을 설정하면, 그림 대신 텍스트 레이블이 나타납니다.
set_focus_on_click(False)
함수를 사용하면 단추를 마우스로 눌렀을 때 포커스를 주지 않습니다. 도구 모음 같은 곳에 쓸만해서 키보드 포커스를 프로그램의 주 영역에서 없애지 않을 때 씁니다.
이 예제는 다음 참고자료가 필요합니다:
GtkButton
GtkWindow