Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,424 members, 7,808,518 topics. Date: Thursday, 25 April 2024 at 12:58 PM

Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland (16383 Views)

Transform Your Web Project In HTML And Php/mysql To A Desktop Software In .exe / How To Make a DESKTOP Executable Software Using PHP,MYSQL,SQLITE / How To Make Money From Developing Software Using Vb Or Java (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 3:32pm On Nov 21, 2014
Following request by some NL users few months back, I create this thread to share the little I know about making desktop applications using PYTHON programming language with its graphical users interface (GUI) library called wxPython.
This tutorial will cover the same procedure I have used to create some open source desktop apps for some guys here such as this:
https://www.nairaland.com/1790669/visitors-customers-data-app-python
http://www.4shared.com/zip/XmLNVQnKba/NLTempCalc_Setup.html
http://www.4shared.com/zip/EShYucoDce/ttp_setup.html
http://www.4shared.com/zip/e1EvFyD_ba/gpa_calc.html

The content will include;-
~Introduction
~Installation Requirements
~Testing our installations
~Developing the console program
~Sketching the App's GUI (Graphical User Interface)
~Creating the GUI
~Binding Events to Methods
~Compiling & Packaging our completed App

Before we start, I want to be sure if someone out there is interested in this?

2 Likes

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by Nobody: 3:49pm On Nov 21, 2014
Go on brother, I've already downloaded the package. We are ready to learn...

1 Like

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by Nobody: 4:16pm On Nov 21, 2014
drives in
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by khaynoni(m): 5:16pm On Nov 21, 2014
ride on bro
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by Topzlincoln(m): 5:47pm On Nov 21, 2014
Front seat thinzz
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 11:05pm On Nov 21, 2014
[size=20pt]Introduction[/size]

This tutorial will show you how to design and build a fully-functional desktop Graphical User Interface (GUI) application for maths Expression Evaluation using a combination of Python 2.x, wxPython, wxFormBuilder, Py2exe and InnoSetup Compiler. The app we will build is going to be a mini calculator that can perform valid Mathematical Expression Operations of addition, subtraction, multiplication, and division.

First of all, I will use python syntax (leveraging on the eval() python function) to make a Console/CLI (Command Line Interface) program, then convert it into a Graphical User Interface (GUI) app so that users have much more control of it.
I will use the CLI program to explain some python basic syntax for those who may be struggling with understanding of the python basics.

If you feel you need to learn/refresh your python knowledge, then I recommend reading this book “A Bytes of Python” by Swaroop C H.

By the way, some popular software vendors using what we are about to learn to power there software include;- Taskcoach.org, Bittorrent.com, Editra.org, and dropbox.com (Source: www.wikipedia.org/wiki/WxPython)

Basic requirements for this class are:-
~ Basic knowledge of Python programming – I will do my best to simplify the process of this tutorial as much as possible, so everyone will be able to follow along.
~ A Windows Operating System (OS) machine (other OS users can follow along as well) - I am using 32-bit OS computer using the Windows-7 operating system.
~ Python2 interpreter, Text editor, wxPython library, wxFormBuilder, Py2Exe, and Inno Setup Compiler – I will show you how to get and install all of these software soon.

Before we start installing and configuring the software we will be using, let me quickly explain what each of them will be used for and where to get/download them;-

Python 2.x interpreter
Python is an easy to learn, dynamic and powerful modern programming language. Two similar but incompatible versions of Python are in widespread use (Python2.x and Python3.x). Pls note that we will use Python2.x for this class, download the version for your OS below.
Download link: www.python.org/downloads/windows/

wxPython
wxPython is a free GUI toolkit which can be used to build Python GUIs. It is a wrapper for the cross-platform C++ GUI API wxWidgets. Some other GUI library/toolkits for Python include; Tkinter, PySide/PyQt, Kivy and PyGTK
Download link: www.wxpython.org/download.php

Py2exe
Py2Exe is a Python Distutils extension which converts python scripts into executable windows programs, able to run without requiring a python installation. Some other Python extension/library which converts python scripts into executable programs are; PyInstaller, Py2app, cx_Freeze, bbFreeze, and vendorID
Download link: www.py2exe.org or http://www.lfd.uci.edu/~gohlke/pythonlibs/#py2exe

wxFormBuilder
wxFormBuilder is a free Python GUI constructor toolkit for wxWidgets GUI design which can be used to build Python GUIs. Similar python GUI constructor toolkits are wxGlade, Boa Constructor, PythonCard and wxDesigner
Download link: www.sourceforge.net/projects/wxformbuilder/

InnoSetup Compiler
Inno Setup is used to create installer for Windows programs. Similar programs are NSIS and WiX
Download link: www.innosetup.com

For sure, you also need a text editor if you don’t have one already, you can get NotePad++ at: www.notepad-plus-plus.org

If for any reason, any one of the links I provided is broken, use Google to search and get the correct link. Also be free to drop you questions/comments.

Hope you enjoyed this class? See you next time!
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by Nobody: 5:15pm On Nov 22, 2014
umaryusuf:
[size=20pt]Introduction[/size]

This tutorial will show you how to design and build a fully-functional desktop Graphical User Interface (GUI) application for maths Expression Evaluation using a combination of Python 2.x, wxPython, wxFormBuilder, Py2exe and InnoSetup Compiler. The app we will build is going to be a mini calculator that can perform valid Mathematical Expression Operations of addition, subtraction, multiplication, and division.

First of all, I will use python syntax (leveraging on the eval() python function) to make a Console/CLI (Command Line Interface) program, then convert it into a Graphical User Interface (GUI) app so that users have much more control of it.
I will use the CLI program to explain some python basic syntax for those who may be struggling with understanding of the python basics.

If you feel you need to learn/refresh your python knowledge, then I recommend reading this book “A Bytes of Python” by Swaroop C H.

By the way, some popular software vendors using what we are about to learn to power there software include;- Taskcoach.org, Bittorrent.com, Editra.org, and dropbox.com (Source: www.wikipedia.org/wiki/WxPython)

Basic requirements for this class are:-
~ Basic knowledge of Python programming – I will do my best to simplify the process of this tutorial as much as possible, so everyone will be able to follow along.
~ A Windows Operating System (OS) machine (other OS users can follow along as well) - I am using 32-bit OS computer using the Windows-7 operating system.
~ Python2 interpreter, Text editor, wxPython library, wxFormBuilder, Py2Exe, and Inno Setup Compiler – I will show you how to get and install all of these software soon.

Before we start installing and configuring the software we will be using, let me quickly explain what each of them will be used for and where to get/download them;-

Python 2.x interpreter
Python is an easy to learn, dynamic and powerful modern programming language. Two similar but incompatible versions of Python are in widespread use (Python2.x and Python3.x). Pls note that we will use Python2.x for this class, download the version for your OS below.
Download link: www.python.org/downloads/windows/

wxPython
wxPython is a free GUI toolkit which can be used to build Python GUIs. It is a wrapper for the cross-platform C++ GUI API wxWidgets. Some other GUI library/toolkits for Python include; Tkinter, PySide/PyQt, Kivy and PyGTK
Download link: www.wxpython.org/download.php

Py2exe
Py2Exe is a Python Distutils extension which converts python scripts into executable windows programs, able to run without requiring a python installation. Some other Python extension/library which converts python scripts into executable programs are; PyInstaller, Py2app, cx_Freeze, bbFreeze, and vendorID
Download link: www.py2exe.org or http://www.lfd.uci.edu/~gohlke/pythonlibs/#py2exe

wxFormBuilder
wxFormBuilder is a free Python GUI constructor toolkit for wxWidgets GUI design which can be used to build Python GUIs. Similar python GUI constructor toolkits are wxGlade, Boa Constructor, PythonCard and wxDesigner
Download link: www.sourceforge.net/projects/wxformbuilder/

InnoSetup Compiler
Inno Setup is used to create installer for Windows programs. Similar programs are NSIS and WiX
Download link: www.innosetup.com

For sure, you also need a text editor if you don’t have one already, you can get NotePad++ at: www.notepad-plus-plus.org

If for any reason, any one of the links I provided is broken, use Google to search and get the correct link. Also be free to drop you questions/comments.

Hope you enjoyed this class? See you next time!

I just want to add that for simplicity, users can download pyscripter, rather than, notepad++.
i am following.....
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by NPComplete: 5:31pm On Nov 22, 2014
basbone:
I just want to add that for simplicity, users can download pyscripter, rather than, notepad++.
i am following.....

Even the IDLE IDE that comes bundled with Python should suffice.
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by NPComplete: 5:35pm On Nov 22, 2014
OP, one thing I am curious about is this: do .exe files created using py2exe or pyinstaller run faster than the .py files? Also do they run as fast as those .exe files created by compile languages like C# and Java for instance. Thanks.

1 Like

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by Nobody: 5:43pm On Nov 22, 2014
NPComplete:


Even the IDLE IDE that comes bundled with Python should suffice.
Yeah....even the python idle will ok but it doesnt support indentation thats why i like pyscripter. It supports python indentation and its also opensourced. However, if i am to go professional with python, then i will stick to the PTVS i use in my VS 2010.
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by Nobody: 5:46pm On Nov 22, 2014
NPComplete:
OP, one thing I am curious about is this: do .exe files created using py2exe or pyinstaller run faster than the .py files? Also do they run as fast as those .exe files created by compile languages like C# and Java for instance. Thanks.
though im not the op, but i know that interpreted languages are slower than compiled languges, no matter how you tweak them.
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by NPComplete: 7:09pm On Nov 22, 2014
basbone:
though im not the op, but i know that interpreted languages are slower than compiled languges, no matter how you tweak them.
OK.
To the first part of my question. Will the .exe be faster than the .py?
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by Nobody: 7:20pm On Nov 22, 2014
NPComplete:

OK.
To the first part of my question. Will the .exe be faster than the .py?
well, it depends. I dont know much abt p2exe, but from the little i know about ironpython, i.e, python made to run on .NET, theyre faster than their .py counterparts cos theyre running on JIT Compiler.

1 Like

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by NPComplete: 8:34pm On Nov 22, 2014
basbone:
well, it depends. I dont know much abt p2exe, but from the little i know about ironpython, i.e, python made to run on .NET, theyre faster than their .py counterparts cos theyre running on JIT Compiler.
OK thanks bro
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by harrysterol(m): 8:58pm On Nov 22, 2014
Make I grab my sit but I b novice o, op b ready for lot of questions grin bt u go bare wit me o
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by pricelessmr: 9:30pm On Nov 22, 2014
You all should stop this busy-body attitude derailing this thread. Let the op lead us in his own way. If una too sabi, abeg open separate thread.

We only need interested students of which I'm one, and not teachers-- Umaryusuf is the only Lecturer in this class. Oga carry on.
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 10:30pm On Nov 22, 2014
^^^^^ Thank u all for contributing. Any good TextEditor/IDE one is comfortable with is just fine to work this tutorial.

@NPComplete, to add to the reply given to u by basbone, the major advantage .exe file has is in terms of distribution. Any computer without python installation can run python scripts that has been compiled to .exe with lots of dependency files (such as .dll etc) based on the complexity of the app in question, so generally speaking .py is faster than .exe
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 10:31pm On Nov 22, 2014
[size=20pt]Installing and testing our installations[/size]

Basically, all the 5 programs we needed for this tutorial (as listed above) have fairly simple installation procedure. Generally all you have to do is download the Windows Binaries respectively and run it to install on your PC.
Note: you most install “Python” first before installing “wxPython” and “Py2exe”, because they are both python’s extensions/libraries.

Now let’s be sure that all we have installed are working perfectly;
For wxFormBuilder and InnoSetup Compiler, you should see their icons on your desktop or windows start pane. Launch the programs to be sure they are working!

For Python 2.x, wxPython, and Py2exe; you will have to follow the procedures below to verify they are installed properly.

~~ Open command prompt and type python and hit enter. If you got error like on image-1 below, then python is not recognized on your PC. Now do any of Option1 or Option2
~~ Option1: assuming you installed python on this path “C:\Python27” run this code below on the command prompt to fix it automatically (note: if your path is different, replace to the correct path).

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User" )

~~ Option2: If Option1 above didn’t work for you, add the path manually as follow; open your computer’s “Control Panel” and follow 1-6 as shown on image-2 below (i.e.: Control Panel > System and Security > System > Advanced system settings > Advance > EnvironmentVariable...).
~~ On the window that opens after clicking “EnvironmentVariable...”, select “Path” then click on “Edit” and add your python path starting with semi-column as shown on image-3. Then click on “Ok” three times to apply the path.
~~ If all went well, retyping python on the command prompt will open a Python Shell as shown in image-4 below.

Still on the Python Shell, to test for wxpython and py2exe installations just type import wx and hit enter then type import py2exe and hit enter. If no any message is displayed as in image-5, then you are good to go your installations are in order.

Thanks for reading!

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by pricelessmr: 5:06am On Nov 23, 2014
Thanks. well explained. I'm following closely.
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by NPComplete: 11:53am On Nov 23, 2014
umaryusuf:
^^^^^ Thank u all for contributing. Any good TextEditor/IDE one is comfortable with is just fine to work this tutorial.

@NPComplete, to add to the reply given to u by basbone, the major advantage .exe file has is in terms of distribution. Any computer without python installation can run python scripts that has been compiled to .exe with lots of dependency files (such as .dll etc) based on the complexity of the app in question, so generally speaking .py is faster than .exe

Thanks for clearing that up.
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 8:17pm On Nov 23, 2014
[size=20pt]Developing the console program >> Part 1[/size]

Welcome back! Hope u still remember the mission app we are going to build throughout this tutorial? If you don’t, kindly refer back to our introductory class where I mentioned that ...the app is going to be a mini calculator that can perform valid Mathematical Expression Operations of addition, subtraction, multiplication, and division.
Ok at this point, I wish to name the application "Expression Evaluator". Hope the name of our app is cool? Be free to suggest a name if you have something better in mind.

Today, I will explain how our app (Expression Evaluator) works on a command prompt (knowledge of Python basics will help). We will take advantage of on a powerful built-in python function used to evaluate mathematical expressions – the eval() function.

The completed code is attached below (image-6), if you understood every bit of it (the code), then go sleep and wait for the next class wink. But if you are struggling to read/understand/interpret the code, kindly be patient and follow along as I will explain every single line of the code soon.

~~ Now head over to your desktop or any location on your PC and create a project folder (this where we will save all our files) name it “EXPRESSION_EVALUATOR” then save a python file inside it from your text editor as “eval.py”. See image-7 below.

~~ Type the attached code (image-6) in your “eval.py” file and save it.

~~ If you run/launch the “eval.py” file by double clicking on it, you should see a command prompt as on image-8 asking you to enter an expression.

~~ Type a valid mathematical expression and press enter to see the result. If you entered an invalid expression, the program will out an error massage.
As shown on image-9 I tried expressing “2+2”, and it gave me “4” excellent!
When I tried expressing “2+a”, the console crashed after showing an error message - try it! This is because “a” isn’t a number and isn’t defined, making the expression invalid (hope you still remember your maths class!).

~~ A better way in my opinion of running the “eval.py” file is by opening the project folder then the file through a command prompt, this way your cmd prompt window will remain open no matter what happened and you can see all errors. Here is what I mean;-
From within the project folder, hold “Shift” button on your keyboard and right-click inside the folder then select “Open command window here” – see image-10. This will open the command prompt directly from the folder.

~~ Type python space then name of the file (that is: python eval.py) and hit enter. You should see a screen similar to the previous one (except for the folder’s path on the first line) asking you to enter an expression. See image-11.

~~ This time even if we entered an invalid expression our program with never close automatically until we decide to close it manually. Or better still uncomment line-14 in “eval.py” file so however we ran our file it will never close until we press enter.

Ok, let’s dive into detail explanation of each line of code in “eval.py” file in the next class.
Till then digest today’s lesson and have a nice day!


[size=15pt]Warning:[/size] Just a word of warning, the python “eval()” function can be dangerous if you do not know what you are doing (it can be use to delete system files). So in the meantime just use it for simple examples such as the ones shown here.

Peace!

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 1:10pm On Nov 25, 2014
[size=20pt]Developing the console program >> Part 2[/size]

Today, let me quickly explain every line of our “eval.py” code file (as seen attached below).
Basically there are less than 15 lines of codes that made-up the console version of the Expression Evaluator app! Starting from;-

Line-1: This line is called “SheBang” or “HashBang”. It tells python shell;-
- That the file is a script
- To use python to execute the script
- The path to the python interpreter.

Line-2: Blank

Line-3: Here we have a print() function that outputs a string onto the console. In this case, the string within the single quote will be written on the console.

Line-4: The same as line-3 above. But here we are telling python to write out the hyphen “-” character 40 times, to create a ruled line beneath the above string. This is mainly for decoration.

Line-5: Blank

Line-6: This is used to handle errors, try...except statement block starts here. We basically put our usual statements within the try-block and put all our error handlers in the except-block.

Line-7: Here the raw_input() function is use to get user input on the command prompt and stored in a variable called “exp” for further processing. This is done within the try...except block, so note the indentation.

Line-8: We then pass the variable “exp” into the eval() function. Then store the result in a new variable called “Answer”.

Line-9: Blank

Line-10: The two variables “exp” and “Answer” are printed on the console with the aid of string formatting function.

Line-11: raw_input() function is used to allow the user to terminate successful expression evaluation.

Line-12: Error handlers are placed in the except-block.

Line-13: In this case the error handler in the except-block is to use print function to outputs the string 'Ooops! ...'.

Line-14: This is optional, if the “#” sign is removed, raw_input() function will allow the user to terminate an unsuccessful expression evaluation.

Line-15: Blank

That is it! Our next task is to convert this "eval.py" code (as seen attached below) into a more user friendly interface application for windows PC, where the user uses elements such as button, text input box to manipulate the app.

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 6:16am On Nov 26, 2014
[size=20pt]Sketching the App's GUI (Graphical User Interface)[/size]

Today’s class is going to be the shortest and simplest class so far! It will not involve any technical idiom what so ever, but please don’t under rate this stage as doing so can lead you to waste vital time during your software design process.
All you will do here is to pick your paper and pencil, then sketch a beautiful interface for your app!

Keeping thing simple, I have brain stormed and came up with a simple user interface for our app! It consists of four major elements (widgets/control as they are being called in wxPython) on a window frame as follow;
From the top I placed a “Static Text Label” widget to display result of an expression. Then just below it I placed a “Text Input Box” widget to collect expression from the user. At the bottom, I placed two “Buttons” – the first-button will evaluate the given expression while the second-button will close the app’s window. See the image-12 below.

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by pricelessmr: 7:20am On Nov 26, 2014
Following, thank you.
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 12:29pm On Nov 26, 2014
pricelessmr:
Following, thank you.

OK. Hope u are not the only one following till this point? I don't want to scare people away!
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by pricelessmr: 5:40am On Nov 27, 2014
umaryusuf:


OK. Hope u are not the only one following till this point? I don't want to scare people away!

Obviously not d only one. 3 of my friends in real life are following keenly too.
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 8:00am On Nov 27, 2014
[size=20pt]Creating the GUI[/size]
This is where the fun begins!
It is in this section we will start to make use of the wxPython GUI library.
There are lots of codes to learn in wxPython library which we cannot cover at once in this tutorial. I will only introduce you to the basics of wxPython code and to a tool (wxFormBuilder) that generates the wxPython GUI codes for us.

wxPython is primarily used for making GUIs, and largely all GUI apps most have a frame. It is the frame that holds all bunch of elements (such as button, text Boxes, labels, menus, status Bar, combo Boxes, images etc) for any app.
So the minimum code needed to create a frame using wxPython just like in any programming language is seen attached below on image-13 or as quoted below.
import wx

app = wx.App()
frame = wx.Frame(None, -1, 'My First Window App')
frame.Show()
app.MainLoop()
Save the code in a python file (I saved mine as frame.py) and run it, you should see a blank frame/window pops up on your screen (image-14).
To add elements such as buttons, text Boxes, labels etc on the frame, we need to write the codes for all of that! But with wxFormBuilder, we don’t have to as it can be use to easily generate most of the python GUI codes needed on the fly with its WYSIWYG interface. It can also be used to generate codes for C++, PHP, XRC and Lua programming languages.

It is worthy to note that generally in any GUI development, coding is divided into two aspects namely; the GUI coding and the logical coding. We will use wxFormBuilder to generate the GUI code while we get our logical code from the console version of the program we developed earlier.

~~ Let’s get started with wxFormBuilder by creating the GUI interface for our Expression Evaluator App! Now let’s launch our wxFormBuilder software and save it as “Eval_Project” in the project’s folder.

~~ On the right hand side, you will see “Object Properties”, under the “Properties” tab set the Project name to “EvalProject”, set the file name to “gui” (this is the name for the generated python file) and set “code generation” to python. See image-15

~~ Under the “Form” tab in the designer view, create a frame and set its size to 250-Width by 300-Height (see image-16). Note the name of the frame is “MyFrame1”. This name will be used for our frame object class (that an OOP thing...).

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 8:30am On Nov 27, 2014
[size=20pt]Creating the GUI Continue...[/size]

~~ Switch to the “Layout” tab and add a “wxBoxSizer”, leave the orient property on wxVERTICAL, see image-17.

Let’s add the remaining widgets to our frame.

~~ Under the “Common” tab, select “wxStaticText” then EXPAND and STRETCH it by clicking on the respective icons on the toolbar (see image-18 below).
On the object property panel to the right-hand side, set the “wxStaticText” as follow;-
- Name to “DisplayResult”
-Style to wxALIGN_CENTRE
-Label to “DisplayResult”
-Font point size to 20

~~ Add the next widgets on the sketch, which is a Text box (“TextCtrl” as its being called in wxPython). Then EXPAND and STRETCH it as we did above see image-19.
On the object property, set the “wxTextCtrl” as follow;-
-Name to “EnterExpression”
-Style to “wxTE_CENTRE”
-Font point size to 20

The last two widgets on our sketch are the two buttons (Evaluate and Close buttons).

~~ Add the first “wxButton” (Evaluate button), then EXPAND and STRETCH it as we did above for “wxTextCtrl”.
On the object property, set the “wxButton” as follow;-
-Name to “Button1”
-Label to “Evaluate”
-Font point size to 20
Repeat this process for the second button (Close button); remember to change the name appropriately. See image-20

~~ Boom! We are done with our Expression Evaluator GUI design, now save you work and generate the python code by clicking “Generate Code” button icon on the tool bar. See image-21.
Check your project folder, you should see a file named “gui.py” that is the file containing wxpython code for our program’s GUI.

Open “gui.py” file in your text editor and type the minimum required wxpython code at the end of the file. See the code below;-
app = wx.App()
frame = MyFrame1(None)
frame.Show()
app.MainLoop()
If you have followed the tutorial correctly to this point, running/launching the “gui.py” file should present something similar to the screen (image-22) below;-

Wow! Our desktop application is working... But at the moment, our Expression Evaluator app is dormant (it doesn’t evaluate anything). This is because we have not binded the widgets events to methods/functions.

We will bind the events to functions next time.

1 Like

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 9:08pm On Nov 28, 2014
[size=20pt]Binding Events to Methods[/size]

All GUI applications are event-driven. An application reacts to different event types which are generated during its life. Events are generated mainly by the user of an application.

Working with events is straightforward in wxPython. There are three steps:
- Identify the event name
- Create an event handler. This is a function/method that is called when an event is generated
- Bind an event to an event handler

There are different types of events to different wxWidgets. For our expression Evaluator App, we will use only one event type called “OnButtonClick”. This event is generated when a user clicks on a button.

Let’s hook our buttons to event handler by following the steps above;-
~~ From our project (Eval_Project) in wxFormBuilder, select button1 (that’s Evaluate button) and the go to “Object Properties” panel on th right-hand side then select “Events” tab, you should see “OnButtonClick” event. See image-23
Type in the name of the event handler, I named mine “EvaluateFunc” (it can be any name meaningful). Do the same for button2 (that’s Close button), name it “CloseFunc”.

~~ Now regenerate the code file “gui.py” and open it in a text editor. Scrolling down (see image-24), you should notice that the file has been modified (wxPython minimum code will not be there) that means the file got replaced.

~~ As can see, the event handler names we defined for our buttons (“EvaluateFunc” and “CloseFunc”) are names for the functions or methods that are called when an “OnButtonClick” event is generated.
To create the “EvaluateFunc” function for the first button, override the event handler functions, by replacing the “event.Skip()” methods with the code that does whatever you want the buttons to do.

~~ We want the first button to evaluate an entered expression in the “EnterExpression” text box, so we will write a code for doing just that within the function.
As for the second button, we want it to close the frame so we will write a code that too inside the function.
Hence, the codes within our (“EvaluateFunc” and “CloseFunc”) function should look like the screen image-25 below;-

~~ Now, our Expression Evaluator App should work as expected if we entered a valid expression and clicked “Evaluate” button image-26. If an invalid expression is entered, error report is printed out.

[size=15pt]Congrats![/size]
That is it; our Expression Evaluator Application is now ready to be package and distributed!

I will talk about COMPILING, PACKAGING AND DISTRIBUTING OUR COMPLETED APP in the next class.

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by pricelessmr: 10:33pm On Nov 28, 2014
I didn't know python can be used this way.
Please, I'm looking forward to advancing on this. Is there any single book u would recommend to immerse? Thanks
Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by umaryusuf(m): 6:15am On Nov 29, 2014
pricelessmr:
I didn't know python can be used this way.
Please, I'm looking forward to advancing on this. Is there any single book u would recommend to immerse? Thanks

Check these books and tutorials:-
1) Advanced wxPython tutorial
2) Wxpython in Action
3) www.zetcode.com/wxpython/
4) wxPython Application Development Cookbook

1 Like

Re: Learn How To Build A Desktop Software Using PYTHON – Tutorial On Nairaland by pricelessmr: 9:01am On Nov 29, 2014
umaryusuf:


Check these books and tutorials:-
1) Advanced wxPython tutorial
2) Wxpython in Action
3) www.zetcode.com/wxpython/
4) wxPython Application Development Cookbook


Thanks a lot. I intend to use the knowledge I will ultimately derive from your tutorial here, to build a device driver application. Eg connect a gadget to pc via usb for control of device. I come from embedded programming background.

Continue sir.... you are great!

(1) (2) (3) (Reply)

What Language Do I Need To Learn For Software Development? / Funny Source Code Comments / Is C# Better Than C/C++?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 81
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.