<img height="1" width="1" src="https://www.facebook.com/tr?id=1101141206686180&amp;ev=PageView &amp;noscript=1">


Making a calculator with Pyside

Publish Date 24 Feb 2022 Ashton ScaliseAshton Scalise

Python is not really the first language that comes to mind when thinking about GUI development. However, it is a great tool for automating all types of workflows. There can be certain use cases when a sort of UI integration can really help in automation. Lately, I have been using PySide as my choice of a python UI library. It is built off the Qt framework and is free to use. For today, I thought it would make a fun blog post to create a basic GUI math calculator in PySide for those interested in learning some of the syntax.

Here is the basic Hello World. I created a class that represents the main window, set some parameters like the size and the title, and then added a layout with a label. Think of the layout as a container for all the other components.



Now I need to create some buttons and put them in a QGridLayout. I decided to create two more classes to keep the MainWindow class a bit cleaner. The first class will be what all my buttons will be based off.

The second class just builds off the QGridLayout and my StdButton class to make the grid of buttons like a real calculator. I use a loop to create the numbers but manually add the rest of the buttons. I’m sure this could be refactored to look cleaner.

I created a globally scoped function to test the button clicked signal.




The next step is to make the digital indicator that creates the above the buttons. For this, I am making another class that will simply consist of a styled QLabel. I will add it to the main window class.


I now need a way to update the indicator every time a button is pressed. I will define 3 functions to do this. Do_math will be used to perform the basic arithmetic. The function get_op will be used to parse which option I need to use when calling do_math. The function handle_click will be used to modify the string I show in the indicator. To make things simple, I treat all the operators as an ‘=’ if I already have one in the indicator.



I never added support for the positive/negative button. If you found this interesting and would like to learn more, I challenge whoever read to the end,  to add support for the last button. You can free the entire source on my GitHub page. https://github.com/AshtonScalise/pyside-calculator


Below is the final product of the calculator.



Recent Posts:

How I Learn New Skills for Personal Growth
Publish Date 24 Feb 2022 Bryce UrestiBryce Uresti

Learning new skills can be quite the task especially when there's already so much [..]

Controlling the Supply Chain Dream
Publish Date 24 Feb 2022 Thomas MathewThomas Mathew

You are out with your friends, bird watching, and nothing could be more peaceful. [..]

Aligning Data in WAVE
Publish Date 24 Feb 2022 Rohama KhadijaRohama Khadija

When analyzing data from multiple sources, we often find that the time series data [..]

Predicting a Project – Scheduling
Publish Date 24 Feb 2022 Garrett HallmarkGarrett Hallmark

Making a schedule from a blank set of rows and columns can be tricky. It’s tough to [..]

Introduction to PCB Design and Manufacturing Part 4: PCB Manufacturing 
Publish Date 24 Feb 2022 Nathan SzantoNathan Szanto

So, you’ve created your schematic, you’ve laid out your PCB, you’ve had multiple [..]

How to Build a Better MWD Hand
Publish Date 24 Feb 2022 Jason GalleJason Galle

What skills and experiences should we be looking for in new hires and what kind of [..]

Customer Experience means everything to us, Here's Why...
Publish Date 24 Feb 2022 Casey MayesCasey Mayes

Here at Erdos Miller, we all strive for the same goal, and that is to provide our [..]