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.
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()
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 |
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)),
)
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 ...
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()
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.
--The easiest python introductory class Fumitaka Osawa [Author]
Recommended Posts