Calculating with Python: GUI Calculator using PySimpleGUI and Texas Instruments DataMath II

Posted by


In this tutorial, we will be creating a Python calculator with a graphical user interface using the PySimpleGUI library. We will also be taking inspiration from the Texas Instruments DataMath II calculator for the design and layout of the GUI.

PySimpleGUI is a simple and easy-to-use Python library that allows you to create GUI applications quickly and easily. It provides a high-level API for creating GUIs and supports different GUI frameworks such as Tkinter, Qt, and WxPython.

The Texas Instruments DataMath II calculator was a popular scientific calculator released in the 1970s. It featured a compact and ergonomic design, with a keypad layout that was intuitive and easy to use. We will be replicating the design and layout of the DataMath II calculator in our Python calculator GUI.

Let’s get started by installing the PySimpleGUI library. You can install PySimpleGUI using pip by running the following command in your terminal:

pip install PySimpleGUI

Once you have installed PySimpleGUI, you can start creating the Python calculator with GUI. Here is the code for the calculator GUI:

import PySimpleGUI as sg

layout = [
    [sg.Input(size=(10, 1), key='-DISPLAY-', justification='right')],
    [sg.Button('7'), sg.Button('8'), sg.Button('9'), sg.Button('/')],
    [sg.Button('4'), sg.Button('5'), sg.Button('6'), sg.Button('*')],
    [sg.Button('1'), sg.Button('2'), sg.Button('3'), sg.Button('-')],
    [sg.Button('0'), sg.Button('.'), sg.Button('C'), sg.Button('+')],
    [sg.Button('Calculate', size=(20, 1))]
]

window = sg.Window('Python Calculator', layout)

equation = ''

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == 'C':
        equation = ''

    elif event == 'Calculate':
        try:
            result = eval(equation)
            window['-DISPLAY-'].update(result)
        except:
            window['-DISPLAY-'].update('Error')
            equation = ''

    else:
        equation += event
        window['-DISPLAY-'].update(equation)

window.close()

In this code, we have defined the layout of the calculator GUI using PySimpleGUI elements such as Input fields and Buttons. We have also implemented the functionality for handling user input, clearing the display, and calculating the result using the eval function.

To run the calculator GUI, simply execute the Python script in your terminal. You should see a window pop up with the calculator interface that resembles the Texas Instruments DataMath II calculator.

You can customize the GUI layout, styling, and functionality by modifying the code according to your requirements. You can add more buttons, implement additional mathematical operations, and incorporate advanced features such as memory functions and scientific calculations.

Overall, creating a Python calculator with GUI using PySimpleGUI is a fun and educational project that can help you improve your GUI programming skills and create useful applications for everyday use.

0 0 votes
Article Rating

Leave a Reply

24 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Mr_Accoustic
2 hours ago

Awesome Tutorial

@JZ1917
2 hours ago

As a mechanical engineer, a lot of concepts like "event loop" are missing a name for me. Until watching this video. This is super high quality stuff. Thank you.

@divyanshu901
2 hours ago

Please say I hindi
Hindi me bol bhai….

@claudiotonci3844
2 hours ago

Thanks for your usefull job, Q: every element has no index, but if i wish change p.ex. the color of a button when pushed, if i have 5 or 10 buttons, how can i address each ones? thanks a lot.

@hchattaway
2 hours ago

this explained a number of PySimpleGUI concepts in 8 minutes that I didn't get anywhere else! Just starting to learn this library and this was an incredibly well thought out approach! Thanks!

@snagjulee2734
2 hours ago

Thank you. I'm looking good.
Is it possible to customize with the round(circle) button in PySimpleGUI?

@ivanfedorov7934
2 hours ago

Hi there, this video is pretty awesome. My English is bad but I almost completely understood, thanks a lot

@JimPageGD
2 hours ago

For those who cant access pip try
py -m pip install

@ali-kadar
2 hours ago

Thanks for explaining the basics!

@cloaker7237
2 hours ago

Just curious; what code editor are you using?

@nipunasudha
2 hours ago

This is what super high quality, well planned teaching looks like. Thanks a bunch!

@kmcg101
2 hours ago

Thanks so much. I'm working on a project and ended the day struggling to find out how to arrange my buttons in rows instead of the default columns. Watched your video and then bam! Rows.

@Artsy__0807
2 hours ago

Hlw sir I want code source of this calculator can u plz share this

@brock2k1
2 hours ago

This is really well done. Hope you make more.

@berkaybakacak
2 hours ago

Is this work with Cython?

@gotas.de.terror
2 hours ago

import PySimpleGUI as sg

sg.popup_ok('Atenção!',

'Não utilizar pontos e vírgulas nos valores recebidos, não deixar valores em branco, pagamento não recebido colocar 0 (zero) !')

sg.theme('DarkAmber') # Add a touch of color

# All the stuff inside your window.

layout = ([sg.Text('Total recebido em Dinheiro:'), sg.Input(size=(7, 1), key=('valor0'))],

[sg.Text('Total recebido no Débito:'), sg.Input(size=(7, 1), key=('valor'))],

[sg.Text('Total recebido no Crédito a vista:'), sg.Input(size=(7, 1), key=('valor1'))],

[sg.Text('Total recebido Crédito até 6x:'), sg.Input(size=(7, 1), key=('valor2'))],

[sg.Text('Total recebido Crédito de 7x até 12x:'), sg.Input(size=(7, 1), key=('valor3'))],

[sg.Text('VALORES CARTÃO ELO', background_color='Black', justification='center4')],

[sg.Text('Total recebido Débito Elo:'), sg.Input(size=(7, 1), key=('valor4'))],

[sg.Text('Total recebido Crédito a vista Elo:'), sg.Input(size=(7, 1), key=('valor5'))],

[sg.Text('Total recebido Crédito Elo até 6x:'), sg.Input(size=(7, 1), key=('valor6'))],

[sg.Text('Total recebido Crédito Elo de 7x até 12x:'), sg.Input(size=(7, 1), key=('valor7'))],

[sg.Output(size=(80, 20))],

[sg.Button('Calcular'), sg.Button('Cancel')])

# Create the Window

window = sg.Window('Calculador porcentagem do Dentista', layout)

# Event Loop to process "events" and get the "values" of the inputs

while True:

event, values = window.read()

if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel

break

# Aqui é feito o cálculo da porcentagem da maquininha a ser descontada do valor total

if event == 'Calcular':

values['valor0'] = float

values['valor'] = float

values['valor1'] = float

values['valor2'] = float

values['valor3'] = float

values['valor4'] = float

values['valor5'] = float

values['valor6'] = float

values['valor7'] = float

porcent = values['valor'] * 0.0178

porcent1 = values['valor1'] * 0.0294

porcent2 = values['valor2'] * 0.0350

porcent3 = values['valor3'] * 0.0370

porcent4 = values['valor4'] * 0.0304

porcent5 = values['valor5'] * 0.0409

porcent6 = values['valor6'] * 0.0462

porcent7 = values['valor7'] * 0.0509

desco = values['valor'] – porcent # Aqui é feito o desconto da taxa da maquininha do valor total

desco1 = values['valor1'] – porcent1

desco2 = values['valor2'] – porcent2

desco3 = values['valor3'] – porcent3

desco4 = values['valor4'] – porcent4

desco5 = values['valor5'] – porcent5

desco6 = values['valor6'] – porcent6

desco7 = values['valor7'] – porcent7

liq0 = values['valor0'] * 0.35 # Aqui é feito os 35% a ser repassado para o dentista do valor com a taxa da maquininha descontada

liq = desco * 0.35

liq1 = desco1 * 0.35

liq2 = desco2 * 0.35

liq3 = desco3 * 0.35

liq4 = desco4 * 0.35

liq5 = desco5 * 0.35

liq6 = desco6 * 0.35

liq7 = desco7 * 0.35

print('Valor liquido a receber é', liq0 + liq + liq1 + liq2 + liq3 + liq4 + liq5 + liq6 + liq7) # Aqui imprimi na tela o valor total dos 35% a ser repassado ao dentista de todos os pagamentos

window.close()

#Not working… it didn't do the calculation, what is wrong with it please?

@worldmusiclyrics939
2 hours ago

Thank you so much… Keep doing please!!!!

@joshuakim1843
2 hours ago

Hey bro, your videos are such high quality and so well thought out. Your quality is definitely noticeable, and I have no doubt it would have taken a lot of effort to do all this. It's a bummer to see that you stopped making videos. Overall, thank you so much for all the videos you've made 🙂

@drainis
2 hours ago

That was so pleasing to listen to and look at. It was short and sweet, and it really laid it all out there for me to go in and tinker/replicate it myself

@7evenpm
2 hours ago

G U I

NOT GOOEY

24
0
Would love your thoughts, please comment.x
()
x