Realize calculator with python

 

Last time I used the python I learned to make a simple calculator, I modified the calculator, changed it and optimized it, and it became a real calculator

Implementation process

1. Computer Layout

2. Computer execution

First import the module:

As one of the Python GUI development tools, Tkinter has the necessary common functions of the GUI software package. For example, it provides more than ten different types of window controls, window layout managers, event handling mechanisms, etc., in addition to its high development efficiency, concise and easy-to-read code

import tkinter as tk
#Python3标准安装包中自带tkinter,即不用安装,导入即可使用。
#可以从命令行运行python -m tkinter会打开一个窗口
#演示一个简单的Tk接口,证明tkinter已正确安装在系统上.
#python -m tkinter

calculator layout

1. First complete the layout of the entire computer and a column of other output results

After completing the overall layout, refine the buttons in the calculator

1. The first four lines of code define the symbol, length, width, font, and color of the button

2. The last four lines are the distance between the buttons

root = tk.Tk()
root.title('计算器')
root.geometry('295x280+100+100') #边距
font = ('宋体', 20)
font_16 = ('宋体', 16)
root.attributes("-alpha", 0.9) #设置透明度

result_num = tk.StringVar() #赋予可变变量
result_num.set('')

tk.Label(root,
         textvariable=result_num, font=font, height=2,
         width=20, justify=tk.LEFT, anchor=tk.SE
         ).grid(row=1, column=1, columnspan=4) #显示结果的布局


button_clear = tk.Button(root, text='C', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定义按钮的符号、长宽、字体、颜色
button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定义按钮的符号、长宽、字体、颜色
button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定义按钮的符号、长宽、字体、颜色
button_multiplication = tk.Button(root, text='×', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2') #定义按钮的符号、长宽、字体、颜色
button_clear.grid(row=2, column=1, padx=4, pady=2)
button_back.grid(row=2, column=2, padx=4, pady=2)
button_division.grid(row=2, column=3, padx=4, pady=2)
button_multiplication.grid(row=2, column=4, padx=4, pady=2) #定义按钮位置

#依葫芦画瓢
button_seven = tk.Button(root, text='7', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_seven.grid(row=3, column=1, padx=4, pady=2)
button_eight.grid(row=3, column=2, padx=4, pady=2)
button_nine.grid(row=3, column=3, padx=4, pady=2)
button_subtraction.grid(row=3, column=4, padx=4, pady=2)

#依葫芦画瓢
button_four = tk.Button(root, text='4', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_addition = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_four.grid(row=4, column=1, padx=4, pady=2)
button_five.grid(row=4, column=2, padx=4, pady=2)
button_six.grid(row=4, column=3, padx=4, pady=2)
button_addition.grid(row=4, column=4, padx=4, pady=2)

#依葫芦画瓢
button_one = tk.Button(root, text='1', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal = tk.Button(root, text='=', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_one.grid(row=5, column=1, padx=4, pady=2)
button_two.grid(row=5, column=2, padx=4, pady=2)
button_three.grid(row=5, column=3, padx=4, pady=2)
button_equal.grid(row=5, column=4, padx=4, pady=2)


button_zero1 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_zero = tk.Button(root, text='0', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_dot = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal2 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_zero1.grid(row=6, column=1, padx=4, pady=2)
button_zero.grid(row=6, column=2, padx=4, pady=2)
button_dot.grid(row=6, column=3, padx=4, pady=2)
button_equal2.grid(row=6, column=4, padx=4, pady=2)

computer execution

After the calculator layout is complete (master and detail) give the calculator the ability to calculate

1. Click the button to realize the output bar of the result and the output bar of the calculator

2. Empower events

def click_button(x):
    print('X:\t', x)
    result_num.set(result_num.get() + x)

Click the button to input the corresponding result and call the lambda function to calculate the input result and give feedback

The ambda function is anonymous: The so-called anonymous function, in layman's terms, is a function without a name. Lambda functions don't have names. The lambda function has input and output: the input is the value passed into the parameter list argument_list, and the output is the value calculated according to the expression expression.

button_one.config(command=lambda: click_button('1'))
button_two.config(command=lambda: click_button('2'))
button_three.config(command=lambda: click_button('3'))
button_four.config(command=lambda: click_button('4'))
button_five.config(command=lambda: click_button('5'))
button_six.config(command=lambda: click_button('6'))
button_seven.config(command=lambda: click_button('7'))
button_eight.config(command=lambda: click_button('8'))
button_nine.config(command=lambda: click_button('9'))
button_zero.config(command=lambda: click_button('0'))
button_dot.config(command=lambda: click_button('.'))
button_addition.config(command=lambda: click_button('+'))
button_subtraction.config(command=lambda: click_button('-'))
button_multiplication.config(command=lambda: click_button('*'))
button_division.config(command=lambda: click_button('/'))
button_clear.config(command=lambda: btnClearDisplay())
button_equal.config(command=calculation)

Calling calculation time is used to calculate the input result, where str is used to convert the string.

Call btnClearDisplay to clear the input result

def calculation():
    opt_str = result_num.get()
    result = eval(opt_str)
    result_num.set(str(result))


def btnClearDisplay():
    opt_str = result_num.get()
    result = eval(str(opt_str))
    result_num.set("")

The last line of code does not need to trigger the lambda because there is no need for anonymous functions and no passing parameters     

button_clear.config(command=lambda: btnClearDisplay())
button_equal.config(command=calculation)

code display

import tkinter as tk

root = tk.Tk()
root.title('计算器')
root.geometry('295x280+100+100')
font = ('宋体', 20)
font_16 = ('宋体', 16)
root.attributes("-alpha", 0.9)

result_num = tk.StringVar()
result_num.set('')

tk.Label(root,
         textvariable=result_num, font=font, height=2,
         width=20, justify=tk.LEFT, anchor=tk.SE
         ).grid(row=1, column=1, columnspan=4)


button_clear = tk.Button(root, text='C', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_multiplication = tk.Button(root, text='×', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_clear.grid(row=2, column=1, padx=4, pady=2)
button_back.grid(row=2, column=2, padx=4, pady=2)
button_division.grid(row=2, column=3, padx=4, pady=2)
button_multiplication.grid(row=2, column=4, padx=4, pady=2)


button_seven = tk.Button(root, text='7', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_seven.grid(row=3, column=1, padx=4, pady=2)
button_eight.grid(row=3, column=2, padx=4, pady=2)
button_nine.grid(row=3, column=3, padx=4, pady=2)
button_subtraction.grid(row=3, column=4, padx=4, pady=2)

button_four = tk.Button(root, text='4', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_addition = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_four.grid(row=4, column=1, padx=4, pady=2)
button_five.grid(row=4, column=2, padx=4, pady=2)
button_six.grid(row=4, column=3, padx=4, pady=2)
button_addition.grid(row=4, column=4, padx=4, pady=2)


button_one = tk.Button(root, text='1', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal = tk.Button(root, text='=', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_one.grid(row=5, column=1, padx=4, pady=2)
button_two.grid(row=5, column=2, padx=4, pady=2)
button_three.grid(row=5, column=3, padx=4, pady=2)
button_equal.grid(row=5, column=4, padx=4, pady=2)


button_zero1 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_zero = tk.Button(root, text='0', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_dot = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal2 = tk.Button(root, text=' ', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_zero1.grid(row=6, column=1, padx=4, pady=2)
button_zero.grid(row=6, column=2, padx=4, pady=2)
button_dot.grid(row=6, column=3, padx=4, pady=2)
button_equal2.grid(row=6, column=4, padx=4, pady=2)


def click_button(x):
    print('X:\t', x)
    result_num.set(result_num.get() + x)


def calculation():
    opt_str = result_num.get()
    result = eval(opt_str)
    result_num.set(str(result))


def btnClearDisplay():
    opt_str = result_num.get()
    result = eval(str(opt_str))
    result_num.set("")


button_one.config(command=lambda: click_button('1'))
button_two.config(command=lambda: click_button('2'))
button_three.config(command=lambda: click_button('3'))
button_four.config(command=lambda: click_button('4'))
button_five.config(command=lambda: click_button('5'))
button_six.config(command=lambda: click_button('6'))
button_seven.config(command=lambda: click_button('7'))
button_eight.config(command=lambda: click_button('8'))
button_nine.config(command=lambda: click_button('9'))
button_zero.config(command=lambda: click_button('0'))
button_dot.config(command=lambda: click_button('.'))
button_addition.config(command=lambda: click_button('+'))
button_subtraction.config(command=lambda: click_button('-'))
button_multiplication.config(command=lambda: click_button('*'))
button_division.config(command=lambda: click_button('/'))
button_clear.config(command=lambda: btnClearDisplay())
button_equal.config(command=calculation)


root.mainloop()

run show

You can see that the page of the calculator is very beautiful. Follow me to learn python and feel a sense of accomplishment! ! !

 

 

Guess you like

Origin blog.csdn.net/weixin_56043516/article/details/127958188