1.5.12. fejezet, GTK+

Modulok telepítése

sudo apt install python3-gi
sudo apt install libgirepository1.0-dev
sudo apt install pipenv
pip3 install pygobject
pip3 install schedule

Egy egyszerű mintakód: main.py

import gi
import schedule
 
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
 
 
class Handler:
    @staticmethod
    def onDestroy(*args):
        Gtk.main_quit()
 
    @staticmethod
    def onButtonClicked(button):
        print("Hello World!")
 
 
def job():
    print("It works...")
 
 
def run_scheduler():
    schedule.run_pending()
    GLib.timeout_add(1, run_scheduler)
 
 
schedule.every(10).seconds.do(job)
GLib.timeout_add(1, run_scheduler)
 
builder = Gtk.Builder()
builder.add_from_file("example.glade")
builder.connect_signals(Handler())
 
window = builder.get_object("window1")
window.show_all()
 
Gtk.main()

Az example.glade tartalma:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<interface>
  <requires lib="gtk+" version="3.0"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <signal name="destroy" handler="onDestroy" swapped="no"/>
    <child type="titlebar">
      <placeholder/>
    </child>
    <child>
      <object class="GtkButton" id="button1">
        <property name="label" translatable="yes">button</property>
        <property name="use_action_appearance">False</property>
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="receives_default">True</property>
        <signal name="clicked" handler="onButtonClicked" swapped="no"/>
      </object>
    </child>
  </object>
</interface>

Futtatás:

python3 main.py

Forrás: python-gtk-3-tutorial.readthedocs.io