Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Tutorial: Using Python/Glade to create a simple GUI application.
New Posts  All Forums:Forum Nav:

Tutorial: Using Python/Glade to create a simple GUI application. - Page 3

post #21 of 28
Thread Starter 
Quote:
Originally Posted by FokkerCharlie View Post
Hi Lattyware et al

I know this is an old thread, but it was one of the first that I came across when searching for help, and probably the best getting-started walkthrough for Glade and python. Anyway, you mention that gtk.Builder is replacing libglade, so I thought I would follow that route (starting to learn a bit of python), and after much huffing and puffing have got your adder program to work with gtkbuilder.

Here's the code that works for me if anyone's interested:

Code:
import sys
try:  
    import pygtk  
    pygtk.require("2.0")  
except:  
    pass  
try:  
    import gtk  
except:  
    print("GTK Not Availible")
    sys.exit(1)

class adder:

    result = 0

    def __init__( self, number1, number2 ):
        self.result = int( number1 ) + int( number2 )
        
    def giveResult( self ):
        return str(self.result)
        
class adderGui:

    wTree = gtk.Builder()

    def __init__( self ):
        self.builder = gtk.Builder()
        self.builder.add_from_file("Adder.glade")
        self.window = self.builder.get_object ("windowMain")
        if self.window:
            self.window.connect("destroy", gtk.main_quit)
        self.entry1 = self.builder.get_object ("entry1")
        self.entry2 = self.builder.get_object ("entry2")
        
        dic = { 
            "on_buttonQuit_clicked" : self.quit,
            "on_buttonAdd_clicked" : self.add,
            "on_windowMain_destroy" : self.quit,
        }
        
        self.builder.connect_signals( dic )

    def add(self, widget):
        entry1 = self.builder.get_object ("entry1")
        entry2 = self.builder.get_object ("entry2")
        try:
            thistime = adder( entry1.get_text(), entry2.get_text() )
        except ValueError:
            self.builder.get_object("hboxWarning").show()
            self.builder.get_object("image1").show()
            self.builder.get_object("entryResult").set_text("ERROR")
            return 0
        self.builder.get_object("hboxWarning").hide()
        self.builder.get_object("image1").hide()
        self.builder.get_object("entryResult").set_text(thistime.giveResult())
  
    def quit(self, widget):
        sys.exit(0)
        
adderGui = adderGui()
adderGui.window.show()
gtk.main()
I daresay that it could be tidier or bettered in many ways (pointers welcome). Any suggestions as to where to go next?

Cheers
Charlie
Good job. As to where to go next, I'd suggest thinking up a small project, something that you want, and create it. There are lots of very small, simple apps out there, just try making something, get into the flow of thinking about how to do something.

And whenever you are writing python, always have the reference open. Python has great docs, and having them open can really help. No one will remember all the features on offer.
Shinobu
(12 items)
 
Nodoka
(16 items)
 
Index
(4 items)
 
CPURAMHard DriveOS
Intel Core i7-3720QM Ivy Bridge 1600MHz DDR3L 512GB SSD Arch Linux 
OSMonitorMonitorMonitor
OS X MBP Retina Display Dell 2407WFP Dell U2410 
KeyboardMouseAudioAudio
Cherry - Cherry Blue Switches (Unlabeled keys) Logitech M570 Trackball Sennheiser HD595 Logitech Z-5500 
CPUMotherboardGraphicsRAM
Intel Core i5 3570K Zotac Z77-ITX WiFi EVGA 680 GTX Samsung 
Hard DriveHard DriveOSMonitor
Samsung 256GB 830 Samsung SpinPoint HD501LJ Windows 7 Dell U2410 
MonitorMonitorKeyboardCase
Dell 2407WFP Dell E248WFP Cherry Black (MX Blue Switches, Blank Keys) Silverstone Sugi SG08B 
MouseAudioAudio
Logitech Trackman Logitech Z-5500 Sennheiser HD595s 
CPUMotherboardRAMHard Drive
AMD Athlon II X2 240e Asus M5A78L-M/USB3 Crucial 8GB (2x4GB) DDR3 1600Mhz Ballistix Sport 1.5TB Hard Drives 
  hide details  
Reply
Shinobu
(12 items)
 
Nodoka
(16 items)
 
Index
(4 items)
 
CPURAMHard DriveOS
Intel Core i7-3720QM Ivy Bridge 1600MHz DDR3L 512GB SSD Arch Linux 
OSMonitorMonitorMonitor
OS X MBP Retina Display Dell 2407WFP Dell U2410 
KeyboardMouseAudioAudio
Cherry - Cherry Blue Switches (Unlabeled keys) Logitech M570 Trackball Sennheiser HD595 Logitech Z-5500 
CPUMotherboardGraphicsRAM
Intel Core i5 3570K Zotac Z77-ITX WiFi EVGA 680 GTX Samsung 
Hard DriveHard DriveOSMonitor
Samsung 256GB 830 Samsung SpinPoint HD501LJ Windows 7 Dell U2410 
MonitorMonitorKeyboardCase
Dell 2407WFP Dell E248WFP Cherry Black (MX Blue Switches, Blank Keys) Silverstone Sugi SG08B 
MouseAudioAudio
Logitech Trackman Logitech Z-5500 Sennheiser HD595s 
CPUMotherboardRAMHard Drive
AMD Athlon II X2 240e Asus M5A78L-M/USB3 Crucial 8GB (2x4GB) DDR3 1600Mhz Ballistix Sport 1.5TB Hard Drives 
  hide details  
Reply
post #22 of 28
Thread Starter 
Update: I recorded a screencast of this tutorial and uploaded to youtube, so if you want an in-depth, complete version of this, then do feel free to watch it.
Shinobu
(12 items)
 
Nodoka
(16 items)
 
Index
(4 items)
 
CPURAMHard DriveOS
Intel Core i7-3720QM Ivy Bridge 1600MHz DDR3L 512GB SSD Arch Linux 
OSMonitorMonitorMonitor
OS X MBP Retina Display Dell 2407WFP Dell U2410 
KeyboardMouseAudioAudio
Cherry - Cherry Blue Switches (Unlabeled keys) Logitech M570 Trackball Sennheiser HD595 Logitech Z-5500 
CPUMotherboardGraphicsRAM
Intel Core i5 3570K Zotac Z77-ITX WiFi EVGA 680 GTX Samsung 
Hard DriveHard DriveOSMonitor
Samsung 256GB 830 Samsung SpinPoint HD501LJ Windows 7 Dell U2410 
MonitorMonitorKeyboardCase
Dell 2407WFP Dell E248WFP Cherry Black (MX Blue Switches, Blank Keys) Silverstone Sugi SG08B 
MouseAudioAudio
Logitech Trackman Logitech Z-5500 Sennheiser HD595s 
CPUMotherboardRAMHard Drive
AMD Athlon II X2 240e Asus M5A78L-M/USB3 Crucial 8GB (2x4GB) DDR3 1600Mhz Ballistix Sport 1.5TB Hard Drives 
  hide details  
Reply
Shinobu
(12 items)
 
Nodoka
(16 items)
 
Index
(4 items)
 
CPURAMHard DriveOS
Intel Core i7-3720QM Ivy Bridge 1600MHz DDR3L 512GB SSD Arch Linux 
OSMonitorMonitorMonitor
OS X MBP Retina Display Dell 2407WFP Dell U2410 
KeyboardMouseAudioAudio
Cherry - Cherry Blue Switches (Unlabeled keys) Logitech M570 Trackball Sennheiser HD595 Logitech Z-5500 
CPUMotherboardGraphicsRAM
Intel Core i5 3570K Zotac Z77-ITX WiFi EVGA 680 GTX Samsung 
Hard DriveHard DriveOSMonitor
Samsung 256GB 830 Samsung SpinPoint HD501LJ Windows 7 Dell U2410 
MonitorMonitorKeyboardCase
Dell 2407WFP Dell E248WFP Cherry Black (MX Blue Switches, Blank Keys) Silverstone Sugi SG08B 
MouseAudioAudio
Logitech Trackman Logitech Z-5500 Sennheiser HD595s 
CPUMotherboardRAMHard Drive
AMD Athlon II X2 240e Asus M5A78L-M/USB3 Crucial 8GB (2x4GB) DDR3 1600Mhz Ballistix Sport 1.5TB Hard Drives 
  hide details  
Reply
post #23 of 28
Cheers Latty, will check this out later - I've just got onto GUI programming in Python using wXwidgets, so this will be really handy! My initial opinions, are that GUI programming in Python is a pain in the ass as it requires a lot of code for not a lot of benefit, and that a decent Python IDE which handled the backend coding of the GUI would be an absolute godsend!!!
 
Black 'n' blue II
(13 items)
 
 
CPUMotherboardGraphicsOptical Drive
Intel Core i5-2515E Acer Aspire 5755G NVIDIA GeForce GT630M  DVD/CD 
OSOSMonitorCase
Linux Mint 14 x64 (Cinnamon) Windows 7 Home Premium 15.6 LED screen Acer Aspire 5755G 
Audio
Intel HD Audio 
CPUMotherboardGraphicsRAM
Core i7 860 @ 1.25V MSI P55-GD65 Xpertvision Radeon HD4850 4GB G.Skill Ripjaw 
Hard DriveOptical DriveOSMonitor
150Gb Velociraptor & 1Tb WD Caviar Black Opticon Lightscribe DVD-RW DL Vista Home Premium x64 Hyundai BlueH H224W 22" LCD 
KeyboardPowerCaseMouse
Saitek Eclipse II Thermaltake Purepower RX 550 Galaxy III Logitech MX518 
  hide details  
Reply
 
Black 'n' blue II
(13 items)
 
 
CPUMotherboardGraphicsOptical Drive
Intel Core i5-2515E Acer Aspire 5755G NVIDIA GeForce GT630M  DVD/CD 
OSOSMonitorCase
Linux Mint 14 x64 (Cinnamon) Windows 7 Home Premium 15.6 LED screen Acer Aspire 5755G 
Audio
Intel HD Audio 
CPUMotherboardGraphicsRAM
Core i7 860 @ 1.25V MSI P55-GD65 Xpertvision Radeon HD4850 4GB G.Skill Ripjaw 
Hard DriveOptical DriveOSMonitor
150Gb Velociraptor & 1Tb WD Caviar Black Opticon Lightscribe DVD-RW DL Vista Home Premium x64 Hyundai BlueH H224W 22" LCD 
KeyboardPowerCaseMouse
Saitek Eclipse II Thermaltake Purepower RX 550 Galaxy III Logitech MX518 
  hide details  
Reply
post #24 of 28
Thread Starter 
Quote:
Originally Posted by chemicalfan View Post
Cheers Latty, will check this out later - I've just got onto GUI programming in Python using wXwidgets, so this will be really handy! My initial opinions, are that GUI programming in Python is a pain in the ass as it requires a lot of code for not a lot of benefit, and that a decent Python IDE which handled the backend coding of the GUI would be an absolute godsend!!!
I find it works out pretty well actually. To reduce code you can skip using a dictionary of signals and associating them, and use the autoconnect function by passing it a class, and it'll automatically add up functions with the name of the signal.
Shinobu
(12 items)
 
Nodoka
(16 items)
 
Index
(4 items)
 
CPURAMHard DriveOS
Intel Core i7-3720QM Ivy Bridge 1600MHz DDR3L 512GB SSD Arch Linux 
OSMonitorMonitorMonitor
OS X MBP Retina Display Dell 2407WFP Dell U2410 
KeyboardMouseAudioAudio
Cherry - Cherry Blue Switches (Unlabeled keys) Logitech M570 Trackball Sennheiser HD595 Logitech Z-5500 
CPUMotherboardGraphicsRAM
Intel Core i5 3570K Zotac Z77-ITX WiFi EVGA 680 GTX Samsung 
Hard DriveHard DriveOSMonitor
Samsung 256GB 830 Samsung SpinPoint HD501LJ Windows 7 Dell U2410 
MonitorMonitorKeyboardCase
Dell 2407WFP Dell E248WFP Cherry Black (MX Blue Switches, Blank Keys) Silverstone Sugi SG08B 
MouseAudioAudio
Logitech Trackman Logitech Z-5500 Sennheiser HD595s 
CPUMotherboardRAMHard Drive
AMD Athlon II X2 240e Asus M5A78L-M/USB3 Crucial 8GB (2x4GB) DDR3 1600Mhz Ballistix Sport 1.5TB Hard Drives 
  hide details  
Reply
Shinobu
(12 items)
 
Nodoka
(16 items)
 
Index
(4 items)
 
CPURAMHard DriveOS
Intel Core i7-3720QM Ivy Bridge 1600MHz DDR3L 512GB SSD Arch Linux 
OSMonitorMonitorMonitor
OS X MBP Retina Display Dell 2407WFP Dell U2410 
KeyboardMouseAudioAudio
Cherry - Cherry Blue Switches (Unlabeled keys) Logitech M570 Trackball Sennheiser HD595 Logitech Z-5500 
CPUMotherboardGraphicsRAM
Intel Core i5 3570K Zotac Z77-ITX WiFi EVGA 680 GTX Samsung 
Hard DriveHard DriveOSMonitor
Samsung 256GB 830 Samsung SpinPoint HD501LJ Windows 7 Dell U2410 
MonitorMonitorKeyboardCase
Dell 2407WFP Dell E248WFP Cherry Black (MX Blue Switches, Blank Keys) Silverstone Sugi SG08B 
MouseAudioAudio
Logitech Trackman Logitech Z-5500 Sennheiser HD595s 
CPUMotherboardRAMHard Drive
AMD Athlon II X2 240e Asus M5A78L-M/USB3 Crucial 8GB (2x4GB) DDR3 1600Mhz Ballistix Sport 1.5TB Hard Drives 
  hide details  
Reply
post #25 of 28
I am new to programming and this was one of the most helpful things that I have come across in months! Actually, in Ubuntu, Quickly does all of this in one program, and will even create files for you to distribute your program. But, I also wanted to do a Windows version of my program that I created in Quickly, found Windows API very difficult, and found that wxPython would not do everything I wanted (or at least easily - it uses separate sizers and I couldn't get it to size bitmap pictures). So, I googled the two main components of Quickly, Python and Gtk, and found Glade and your tutorial! I installed Python, Glade, gtk, and pygtk on my Windows PC and everything works great (except that I am using Eclipse for the Python code and Eclipse doesn't find gtk - although the Python command line finds it just fine - UPDATE: See solution below to get gtk to work in Eclipse). And now I need to figure out how to convert my program from Python and Gtk/Glade to .exe... Anyway, thank you very much for your tutorial!
Edited by mtiddens - 8/3/12 at 3:02pm
post #26 of 28
I would like to clarify a couple things to help others reading/watching this tutorial for the first time:
1. All of the Python code that he shows on the first page will not run if you copy and paste it because it is lacking indentation. However, the newer code posted above and below does work (because it is indented).
2. When you click on the link to the tutorial, don't click Play because that is a different tutorial ("List Comprehension"), but go down to "Tutorial - A GUI application using Python". Also note that this tutorial references the revised GtkBuilder code (initially crafted above by FokkerCharlie, but simplified in the video by deleting some lines - see below).
3. It is a little confusing about "Glade": "Glade" as a GUI HTML-code generating tool has not been depreciated. Only the Glade library has been depreciated, which you only use if you choose "Libglade" as your format when you start a new project in Glade. Now choose "GtkBuilder" format because the Glade Libglade library is now included in gtk.'

So, here is the final code that results from the revisions in the tutorial (he refers to being able to download the code, but I don't see where to click to download it):
Code:

#!/usr/bin/env python

import sys
try:  
    import pygtk
    pygtk.require("2.0")  
except:  
    pass  

try:  
    import gtk
except:  
    print("GTK Not Available")
    sys.exit(1)

class adder:

    result = 0

    def __init__( self, number1, number2 ):
        self.result = int( number1 ) + int( number2 )
        
    def giveResult( self ):
        return str(self.result)
        
class adderGui:

    wTree = gtk.Builder()

    def __init__( self ):
        self.builder = gtk.Builder()
        self.builder.add_from_file("adder.glade")
        
        dic = { 
            "on_buttonQuit_clicked" : self.quit,
            "on_buttonAdd_clicked" : self.add,
            "on_windowMain_destroy" : self.quit,
        }
        
        self.builder.connect_signals( dic )

    def add(self, widget):
        entry1 = self.builder.get_object ("entry1")
        entry2 = self.builder.get_object ("entry2")
        try:
            thistime = adder( entry1.get_text(), entry2.get_text() )
        except ValueError:
            self.builder.get_object("hboxWarning").show()
            self.builder.get_object("entryResult").set_text("ERROR")
            return 0
        self.builder.get_object("hboxWarning").hide()
        self.builder.get_object("entryResult").set_text(thistime.giveResult())
  
    def quit(self, widget):
        sys.exit(0)
        
adderGui = adderGui()
gtk.main()

post #27 of 28
To make gtk work in Windows Eclipse:
1. You need to install gtk, which comes with a whole bunch of dependency packages: Install the "All In One Bundle" version linked to in the middle of the text, down the page at http://www.gtk.org/download/win32.php, and save it to C:/Python27/Lib/site-packages.
2. Download pygtk from http://www.pygtk.org/downloads.html.
3. In Eclipse, you need to reset the PyDev Interpreter by removing and reconfiguring it as follows, which automatically picks up all of the available packages: Go to Project, Properties, "PyDev - Interpreter/Grammar", "Click here to configure an interpreter not listed", highlight & Remove python (C:/Python27\Python.exe), Auto Configure (brings up a new list below that you can review and checkmark as needed), then click on OK.
Edited by mtiddens - 8/3/12 at 3:04pm
post #28 of 28
Interesting.
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II SLI MSI GTX560 Ti TwinFrozr II SLI 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 1600MHz CL9 XMS3 (2 x 4GB) OCZ Vertex 3 SSD Western Digital Caviar Black 1TB 7200RPM 64MB C... Western Digital Caviar Green 1TB ~5900RPM 64MB ... 
Optical DriveCoolingOSMonitor
Lite-On 24x DVD-RW CoolerMaster V8 Windows 7 Professional SP1 3 x Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II SLI MSI GTX560 Ti TwinFrozr II SLI 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 1600MHz CL9 XMS3 (2 x 4GB) OCZ Vertex 3 SSD Western Digital Caviar Black 1TB 7200RPM 64MB C... Western Digital Caviar Green 1TB ~5900RPM 64MB ... 
Optical DriveCoolingOSMonitor
Lite-On 24x DVD-RW CoolerMaster V8 Windows 7 Professional SP1 3 x Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Tutorial: Using Python/Glade to create a simple GUI application.