PythonGtk GUI programming in linux, pythongtkgui
Installation:
$ Sudo apt install libgtk3 *
$ Sudo apt install glade
Then open glade for design.
Ui. glade
<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- Generated with glade 3.20.0 -->
<Interface>
<Requires lib = "gtk +" version = "3.0"/>
<Object class = "GtkWindow" id = "window">
<Property name = "can_focus"> False </property>
<Signal name = "delete-event" handler = "onDeleteWindow" swapped = "no"/>
<Child>
<Object class = "GtkButton" id = "button">
<Property name = "label" translatable = "yes"> click me </property>
<Property name = "visible"> True </property>
<Property name = "can_focus"> True </property>
<Property name = "receives_default"> True </property>
</Object>
</Child>
</Object>
</Interface>
App. py
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Import gi
Import time
Gi. require_version ('gtk ', '3. 0 ')
From gi. repository import Gtk
Class Application (Gtk. Window ):
Def onDeleteWindow (self, * args ):
Gtk. main_quit (* args)
Def clicked (self, button ):
Print ("Hello World! ")
Self. window. set_title ("clicked ")
Self. button. set_label ("clicked ")
Def _ init _ (self ):
Builder = Gtk. Builder ()
Builder. add_from_file ("ui. glade ")
Self. window = builder. get_object ("window ")
Self. window. connect ("delete-event", self. onDeleteWindow)
Self. button = builder. get_object ("button ")
Self. button. connect ("clicked", self. clicked)
Def show_all (self ):
Self. window. show_all ()
App = Application ()
App. show_all ()
Gtk. main ()
Running Effect
Effect after clicking
The red part is the key.