Skip to content

Commit 8f2d228

Browse files
authored
GPT-4.1 generated
Copilot Copilot navigation Copilot Chat PyGTK4 event controller example code You said: Pygtk4 event controller example Pygtk4 event controller example Copilot said: Here’s a simple example demonstrating the use Here’s a simple example demonstrating the use of an event controller in PyGObject (commonly referred to as PyGTK 4, though the correct name is PyGObject for GTK 4): This example creates a GTK 4 window with a button. When you click the button, it uses Gtk.GestureClick (an event controller) to print a message. pygtk4_event_controller_example.py import gi gi.require_version('Gtk', '4.0') from gi.repository import Gtk, GObject class MainWindow(Gtk.ApplicationWindow): def __init__(self, app): Instructions: Save this as pygtk4_event_controller_example.py. Run it with: Code python pygtk4_event_controller_example.py Click the button and observe the terminal output. Key Points: Gtk.GestureClick is an event controller for click and tap gestures. add_controller() is used to add event controllers to widgets in GTK 4. This is for GTK 4. For GTK 3, event controllers are different and the code will not work as is. Let me know if you want an example for a different event (like motion, etc.) or a more complex scenario! You said: What other event controllers are there? What other event controllers are there? Copilot said: GTK 4 (and thus PyGObject/PyGTK4) provides several GTK 4 (and thus PyGObject/PyGTK4) provides several event controllers for handling different kinds of user input and events. Here’s a list of the main event controllers available in GTK 4: Common GTK 4 Event Controllers Gtk.GestureClick Detects single/multiple mouse button presses, taps, and long presses. Gtk.GestureDrag Detects click-and-drag (drag gestures). Gtk.GestureLongPress Detects long press gestures (press and hold). Gtk.GesturePan Detects panning gestures (e.g., scrolling with touch). Gtk.GestureRotate Detects rotation gestures (e.g., two-finger rotate). Gtk.GestureSwipe Detects swipe gestures (e.g., flicks). Gtk.GestureZoom Detects pinch-to-zoom gestures. Gtk.EventControllerKey Handles keyboard events (key presses and releases). Gtk.EventControllerMotion Handles pointer motion (mouse movement, touch movement). Gtk.EventControllerScroll Handles scroll events (mouse wheel, touchpad, etc.). Gtk.EventControllerFocus Handles focus-in and focus-out events for a widget. Gtk.EventControllerLegacy Provides direct access to raw GDK events (not recommended for new code). Example Usage You add these controllers to widgets using .add_controller(controller) and then connect to their signals. Example: Adding a pan and scroll controller Python pan = Gtk.GesturePan.new(Gtk.Orientation.HORIZONTAL) pan.connect("pan", on_pan) widget.add_controller(pan) scroll = Gtk.EventControllerScroll.new(Gtk.EventControllerScrollFlags.VERTICAL) scroll.connect("scroll", on_scroll) widget.add_controller(scroll) Documentation GTK 4 Event Controllers (C docs, applies to Python too) PyGObject API Reference Let me know if you want a code example for a specific controller!
1 parent 058609e commit 8f2d228

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pygtk4_event_controller_example.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import gi
2+
gi.require_version('Gtk', '4.0')
3+
from gi.repository import Gtk, GObject
4+
5+
class MainWindow(Gtk.ApplicationWindow):
6+
def __init__(self, app):
7+
super().__init__(application=app, title="PyGTK4 Event Controller Example")
8+
self.set_default_size(300, 100)
9+
10+
button = Gtk.Button(label="Click Me")
11+
self.set_child(button)
12+
13+
# Create a GestureClick controller and connect to the "pressed" signal
14+
gesture = Gtk.GestureClick.new()
15+
gesture.connect("pressed", self.on_button_pressed)
16+
button.add_controller(gesture)
17+
18+
def on_button_pressed(self, gesture, n_press, x, y):
19+
print("Button was pressed! (with event controller)")
20+
21+
class MyApp(Gtk.Application):
22+
def __init__(self):
23+
super().__init__(application_id="com.example.PyGTK4EventController")
24+
25+
def do_activate(self):
26+
win = MainWindow(self)
27+
win.present()
28+
29+
if __name__ == "__main__":
30+
app = MyApp()
31+
app.run()

0 commit comments

Comments
 (0)