How to Create GUI Applications Using PyGObject in Linux
Creation of GUI Applications Using PyGObject Under Linux Desktop
Different ways to create GUI applications using PyGObject in Linux are discussed in this article.
Creation of GUI Applications Under Linux
Following are the ways to create the applications using PyGObject.
Using Code-Only
Create functional GUI' s for our programs. Here is an example,
[root@linuxhelp Desktop]# nano sample.py
GNU nano 2.3.1 File: sample.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from gi.repository import Gtk
class ourwindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title=" My Hello World Program" )
Gtk.Window.set_default_size(self, 400,325)
Gtk.Window.set_position(self, Gtk.WindowPosition.CENTER)
button1 = Gtk.Button(" Hello, World!" )
button1.connect(" clicked" , self.whenbutton1_clicked)
self.add(button1)
def whenbutton1_clicked(self, button):
print " Hello, World!"
window = ourwindow()
window.connect(" delete-event" , Gtk.main_quit)
window.show_all()
Gtk.main()
Paste the above code in “ sample.py” file. Set 755 permission and execute it.
[root@linuxhelp Desktop]# chmod 755 sample.py
[root@linuxhelp Desktop]# ./sample.py
Hello, World!
Hello, World!
Hello, World!
You can view the “ Hello, World!” sentence in the terminal, by clicking the button.
Using Glade Designer
An easy tool for the creation of the GUI interfaces is Glade. Install and execute Glade using the following command.
[root@linuxhelp Desktop]# yum install glade
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package glade.x86_64 0:3.15.0-5.el7 will be installed
.
.
.
Installing : glade-3.15.0-5.el7.x86_64 1/1
Verifying : glade-3.15.0-5.el7.x86_64 1/1
Installed:
glade.x86_64 0:3.15.0-5.el7
Complete!
[root@linuxhelp Desktop]# glade
In the left panel select the window icon, For creation of a new window.
We can Insert several widgets by using control and display palette . For Example select and insert the button to the window by clicking on the empty window.
Right click the “ button1 GTK Button “ from the project view panel. And select “ Edit Separately” .
Button properties window will appears as follows,
Select “ signal” tab, Find the “ clicked” and set “ button1_clicked” in handler option.
And then save the project.
We have successfully created GUI. Save the file in the name “ test.glade” in your Desktop directory. Now create “ testglad.py” file.
[root@linuxhelp Desktop]# nano testglad.py
Insert the following code in the newly created “ testglad.py” file.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from gi.repository import Gtk
class Handler:
def button_1clicked(self, button):
print " Hello, World!"
builder = Gtk.Builder()
builder.add_from_file(" test.glade" )
builder.connect_signals(Handler())
ournewbutton = builder.get_object(" button1" )
ournewbutton.set_label(" Hello, World!" )
window = builder.get_object(" window1" )
window.connect(" delete-event" , Gtk.main_quit)
window.show_all()
Gtk.main()
Save the file and Set the 755 permission for testglad.py file and execute it.
[root@linuxhelp Desktop]# chmod -R 755 testglad.py
[root@linuxhelp Desktop]# ./testglad.py
Comments ( 0 )
No comments available