gtk3
पायथन के साथ GTK + 3
खोज…
एक साधारण GTK विंडो
बस एक विंडो पेश करना जीटीके और पायथन के साथ आसान है। नीचे दिए गए उदाहरण पायथन जीटीके 3 ट्यूटोरियल पर आधारित है, जिसे आपको जीयूआई प्रोग्रामिंग या जीटीके में शुरुआत करने वाले को पढ़ना चाहिए।
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()
जो (विंडोज 10 पर) परिणाम देगा:
एक विजेट की-प्रेस-घटना के लिए सरल बंधन
ईवेंट हैंडलर को कुंजी प्रेस पर लाने का सबसे सरल तरीका है हैंडलर को की key-press-event
सिग्नल से जोड़ना। इस उदाहरण में, हम पूरी विंडो के लिए ईवेंट के लिए पंजीकरण करते हैं, लेकिन आप व्यक्तिगत विजेट के लिए भी पंजीकरण कर सकते हैं।
सबसे महत्वपूर्ण हिस्सा इस घटना के लिए हैंडलर का कनेक्शन है:
self.connect("key-press-event",self.on_key_press_event)
ईवेंट हैंडलर में विजेट और की-प्रेस इवेंट को पैरामीटर के रूप में पास किया जाता है। Ctrl- कुंजी जैसे कुंजी-प्रेस संशोधक event.state
में उपलब्ध हैं और दबाया गया कुंजी event.keyval
।
संशोधक कुंजियों के मान Gdk.ModiferType
में पाए Gdk.ModiferType
और इसमें CONTROL_MASK
, SHIFT_MASK
और कई अन्य शामिल हैं।
कुंजी मूल्यों में पाए जाते हैं Gdk
की उपसर्गों के साथ KEY_
, उदाहरण के लिए, ज कुंजी है 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
) के साथ प्राप्त किया जा सकता है, लेकिन अक्सर एक त्वरित कुंजी-प्रेस हैंडलर आप सभी को एक विशिष्ट विजेट के लिए इच्छित कीबोर्ड घटनाओं पर कब्जा करने की आवश्यकता होती है।
Python3 में एक 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()