mirror of
https://github.com/Taiko2k/GTK4PythonTutorial.git
synced 2026-03-14 20:10:08 +01:00
32 lines
987 B
Python
32 lines
987 B
Python
import sys
|
|
from pathlib import Path
|
|
import gi
|
|
gi.require_version('Gtk', '4.0')
|
|
gi.require_version('Adw', '1')
|
|
from gi.repository import Gtk, Adw
|
|
|
|
class MyApp(Adw.Application):
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
self.connect('activate', self.on_activate)
|
|
|
|
def on_activate(self, app):
|
|
# Create a Builder
|
|
builder = Gtk.Builder()
|
|
builder.add_from_file(str(Path(__file__).with_name("test.ui")))
|
|
|
|
# Obtain the button widget and connect it to a function
|
|
button = builder.get_object("button1")
|
|
button.connect("clicked", self.hello)
|
|
|
|
# Obtain and show the main window
|
|
self.win = builder.get_object("main_window")
|
|
self.win.set_application(self) # Application will close once it no longer has active windows attached to it
|
|
self.win.present()
|
|
|
|
def hello(self, button):
|
|
print("Hello")
|
|
|
|
app = MyApp(application_id="com.example.GtkApplication")
|
|
app.run(sys.argv)
|