gtk3
파이썬이있는 GTK + 3
수색…
간단한 GTK 창
GTK와 Python을 사용하면 간단하게 창을 표시 할 수 있습니다. 아래 예제는 Python GTK3 튜토리얼을 기반으로합니다. GUI 프로그래밍이나 GTK의 초보자 인 경우 읽어야합니다.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Set up the Gtk window
win = Gtk.Window()
# Tell Gtk what to do when the window is closed (in this case quit the main loop)
win.connect("delete-event", Gtk.main_quit)
# Create a label saying Hello World!
label = Gtk.Label(label="Hello World!")
# Add the label to the window
win.add(label)
# Tell Gtk to show all widgets inside the window
win.show_all()
# Start the Gtk main loop, which returns when Gtk.main_quit is called
Gtk.main()
어느 것 (Windows 10) 결과 :
위젯의 키 누르기 이벤트에 대한 간단한 바인딩
키 누르기에서 호출되는 이벤트 핸들러를 가져 오는 가장 간단한 방법은 핸들러를 key-press-event
신호에 연결하는 것입니다. 이 예에서는 전체 창에 대한 이벤트에 등록하지만 개별 위젯에도 등록 할 수 있습니다.
가장 중요한 부분은 처리기를 이벤트에 연결하는 것입니다.
self.connect("key-press-event",self.on_key_press_event)
이벤트 처리기에서 위젯과 키 누름 이벤트가 매개 변수로 전달됩니다. Ctrl 키와 같은 키 누름 수정자는 event.state
에서 사용할 수 있으며 누른 키는 event.keyval
입니다.
변경 키의 값은 Gdk.ModiferType
에 있으며 CONTROL_MASK
, SHIFT_MASK
및 기타 여러 키가 포함됩니다.
키 값은 KEY_
접두어 로 Gdk
에서 찾을 수 있습니다. 예를 들어 h 키는 Gdk.KEY_h
입니다. Gdk.keyval_name()
사용하여 문자열로 변환 할 수 있습니다.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
class MyWindow(Gtk.Window):
key = Gdk.KEY_h
def __init__(self):
# init the base class (Gtk.Window)
super().__init__()
# state affected by shortcuts
self.shortcut_hits = 0
# Tell Gtk what to do when the window is closed (in this case quit the main loop)
self.connect("delete-event", Gtk.main_quit)
# connect the key-press event - this will call the keypress
# handler when any key is pressed
self.connect("key-press-event",self.on_key_press_event)
# Window content goes in a vertical box
box = Gtk.VBox()
# mapping between Gdk.KEY_h and a string
keyname = Gdk.keyval_name(self.key)
# a helpful label
instruct = Gtk.Label(label="Press Ctrl+%s" % keyname)
box.add(instruct)
# the label that will respond to the event
self.label = Gtk.Label(label="")
self.update_label_text()
# Add the label to the window
box.add(self.label)
self.add(box)
def on_key_press_event(self, widget, event):
print("Key press on widget: ", widget)
print(" Modifiers: ", event.state)
print(" Key val, name: ", event.keyval, Gdk.keyval_name(event.keyval))
# check the event modifiers (can also use SHIFTMASK, etc)
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
# see if we recognise a keypress
if ctrl and event.keyval == Gdk.KEY_h:
self.shortcut_hits += 1
self.update_label_text()
def update_label_text(self):
# Update the label based on the state of the hit variable
self.label.set_text("Shortcut pressed %d times" % self.shortcut_hits)
if __name__ == "__main__":
win = MyWindow()
win.show_all()
# Start the Gtk main loop
Gtk.main()
가속기 그룹 ( Gtk.AccelGroup
)을 사용하여 응용 프로그램 범위의 바로 가기에 대한 고급 동작을 얻을 수 있지만 빠른 키 누르기 처리기는 특정 위젯에 대해 원하는 키보드 이벤트를 캡처하는 데 필요한 모든 것입니다.
파이썬 3의 Gtk 창에 비디오 삽입하기
다음은 간단한 gtk 창에 포함 된 Gstreamer 파이프 라인의 예입니다. 실행하면 작은 창이 다음과 같이 표시됩니다.
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gst', '1.0')
from gi.repository import Gtk, Gst
Gst.init(None)
Gst.init_check(None)
class GstWidget(Gtk.Box):
def __init__(self, pipeline):
super().__init__()
# Only setup the widget after the window is shown.
self.connect('realize', self._on_realize)
# Parse a gstreamer pipeline and create it.
self._bin = Gst.parse_bin_from_description(pipeline, True)
def _on_realize(self, widget):
pipeline = Gst.Pipeline()
factory = pipeline.get_factory()
gtksink = factory.make('gtksink')
pipeline.add(self._bin)
pipeline.add(gtksink)
# Link the pipeline to the sink that will display the video.
self._bin.link(gtksink)
self.pack_start(gtksink.props.widget, True, True, 0)
gtksink.props.widget.show()
# Start the video
pipeline.set_state(Gst.State.PLAYING)
window = Gtk.ApplicationWindow()
# Create a gstreamer pipeline with no sink.
# A sink will be created inside the GstWidget.
widget = GstWidget('videotestsrc')
widget.set_size_request(200, 200)
window.add(widget)
window.show_all()
def on_destroy(win):
Gtk.main_quit()
window.connect('destroy', on_destroy)
Gtk.main()