[PYTHON] I tried playing with the calculator on tkinter

First of all

After learning programming with python, my favorite was ** button processing using tkinter **. When I wondered what could do a lot of that button processing, the calculator came out first.


procedure

Window display

final_python.py


from tkinter import *

#Window creation
root = Tk()
#Window size specification
root.geometry("500x500")
#Window title creation
root.title("Four arithmetic operations")
#Cliche
root.mainloop()

C and AC features

final_python.py


#C function
def clear():
    textVal = expression.get()
    
    if len(textVal) > 1:
        expression.set(textVal[:-1])
    
    else:
        expression.set("")

#AC function
def all_clear():
    expression.set("")

--Role of C and AC

C Erase character by character
AC Erase all characters

Button layout

final_python.py



buttons = (
    (("C", clear   ), ("AC", all_clear), ("%", op("%") ), ("+", op("+"))),
    (("7", digit(7)), ("8",  digit(8) ), ("9", digit(9)), ("/", op("/"))),
    (("4", digit(4)), ("5",  digit(5) ), ("6", digit(6)), ("*", op("*"))),
    (("1", digit(1)), ("2",  digit(2) ), ("3", digit(3)), ("-", op("-"))),
    (("0", digit(1)), (None, None     ), ('.', op('.') ), ("=", calculate)),
)

Completed form

ezgif.com-video-to-gif.gif


apology

Since I was announcing the final task this time, I wanted to explain all the functions, but since each person's time is limited to 2 minutes, I narrowed down the points so that it can be kept within 2 minutes. The source code is pasted at the end, so please take a look at it ...

Source code

final_python.py


# coding:utf-8
from tkinter import *

#Window display
root = Tk()
#Window size specification
root.geometry("500x500")
#Window title creation
root.title("Four arithmetic operations")

expression = StringVar()

#C function
def clear():
    textVal = expression.get()
    
    if len(textVal) > 1:
        expression.set(textVal[:-1])
    
    else:
        expression.set("")

#AC function
def all_clear():
    expression.set("")

#Number button
def digit(number):
    def func():
        expression.set(expression.get() + str(number))
    return func

#Calculate button
def op(label):
    def func():
        expression.set(expression.get() + label)
    return func

#=Function of
def calculate():
    try:
        expression.set(eval(expression.get()))
    except SyntaxError:
        expression.set("SyntaxError")
    except ZeroDivisionError:
        expression.set("ZeroDivisionError")
    except NameError:
        expression.set("NameError")

buttons = (
    (("C", clear   ), ("AC", all_clear), ("%", op("%") ), ("+", op("+"))),
    (("7", digit(7)), ("8",  digit(8) ), ("9", digit(9)), ("/", op("/"))),
    (("4", digit(4)), ("5",  digit(5) ), ("6", digit(6)), ("*", op("*"))),
    (("1", digit(1)), ("2",  digit(2) ), ("3", digit(3)), ("-", op("-"))),
    (("0", digit(1)), (None, None     ), ('.', op('.') ), ("=", calculate)),
)

#Display screen
e = Label(root, textvariable=expression, fg="#ffffff", bg="#000000", anchor=E, height=2)
e.grid(row=0, column=0, columnspan=4, sticky="EW")

for row, btns in enumerate(buttons, 1):
    for col, (label, func) in enumerate(btns):
        if label:
            b = Button(root, text=label, command=func, width=10, height=5)
            b.grid(row=row, column=col)

#Cliche
root.mainloop()

Impressions

Almost almost the source code was based on Calculator made with Tkinter, but the width of the window, the size of the calculator itself, and the function of C There were some areas that needed to be improved, so I'm glad that I could do it myself. It was also good to be able to understand the meaning of each command. I want to focus on programming during the summer vacation.

References

--The easiest python introductory class Fumitaka Osawa [Author]

-Calculator made with Tkinter

Recommended Posts

I tried playing with the calculator on tkinter
I tried cross-validation based on the grid search results with scikit-learn
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried to save the data with discord
I tried playing with PartiQL and MongoDB connected
I tried to make a calculator with Tkinter so I will write it
I tried "binarizing" the image with Python + OpenCV
I tried playing mahjong with Python (single mahjong edition)
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
[Introduction to AWS] I tried porting the conversation app and playing with text2speech @ AWS ♪
I tried Python on Mac for the first time.
I tried running the app on the IoT platform "Rimotte"
I tried to implement Minesweeper on terminal with python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried python on heroku for the first time
I tried to solve the problem with Python Vol.1
I tried installing the Linux kernel on virtualbox + vagrant
I tried to notify the honeypot report on LINE
[Introduction to AWS] I tried playing with voice-text conversion ♪
I tried hitting the API with echonest's python client
I tried fp-growth with python
I tried scraping with Python
I tried Learning-to-Rank with Elasticsearch!
I tried clustering with PyCaret
I tried the changefinder library!
I tried gRPC with Python
I tried scraping with python
I tried MLflow on Databricks
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I tried to simulate how the infection spreads with Python
I tried to analyze the whole novel "Weathering with You" ☔️
I tried using the Python library from Ruby with PyCall
I tried to find the average of the sequence with TensorFlow
I tried to notify the train delay information with LINE Notify
I tried running PIFuHD on Windows for the time being
I tried replacing the Windows 10 HDD with a smaller SSD
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried saving the DRF API request history with django-request
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to launch ipython cluster to the minimum on AWS
I tried to divide the file into folders with Python
I displayed the chat of YouTube Live and tried playing
[Shell startup] I tried to display the shell on the TV with a cheap Linux board G-cluster
I tried AdaNet on table data
I tried the TensorFlow tutorial 1st
I tried the Naro novel API 2
I tried trimming efficiently with OpenCV
I tried summarizing sentences with summpy
I tried machine learning with liblinear
I tried web scraping with python.
I tried moving food with SinGAN
I tried the TensorFlow tutorial 2nd
I tried implementing DeepPose with PyTorch
I tried Cython on Ubuntu on VirtualBox
I liked the tweet with python. ..
I tried face detection with MTCNN
I tried the Naruro novel API
I tried running prolog with python 3.8.2.