Preface:

Python makes a simple calculator. Let’s have a good time

The development tools

**Python version: **3.6.4 Related modules: Tkinter and Math modules come with Python. Environment set up

Install Python and add it to the environment variables.

Introduction of the principle

First use Tkinter to build the basic calculator interface, here is not detailed, please refer to the relevant documents, such as:

https://docs.python.org/zh-cn/3/library/tk.html
Copy the code

The key functions are described as follows: 0-9: number key. : Decimal point + : Addition – : subtraction * : multiplication / : Division = : Calculation result 1/x: reciprocal % : mod del: Backspace key CE: Clear the current C: clear all +/- : reverse SQRT: open the square root MC: Reset the current number to zero MR: MS: Ignore the current number in memory, directly replace the current number in memory M+ : remember the current number M- : Remembering the current number (in negative form) is easy to implement because it calls the Math library and Python’s eval function directly. Here are a few details to consider. If there is only one number in the display box, use the backspace key to make the display box display the number 0, not empty. The use of 0 as the denominator or the square root of a negative number is considered illegal to prevent the program from throwing an exception. Because the display box is limited in length, the number of characters that can be displayed is also limited. Therefore, you need to set the maximum length of the current displayed characters. For example, users can not enter an unlimited number of consecutive numbers, the final result can only be reserved for the first number of digits and so on.

Part of the code

'''Demo'''
def Demo() :
	root.minsize(320.420)
	root.title('Calculator')
	# layout
	# -- text box
	label = tkinter.Label(root, textvariable=CurrentShow, bg='black', anchor='e', bd=5, fg='white', font=('regular script'.20))
	label.place(x=20, y=50, width=280, height=50)
	# -- first line
	# ----Memory clear
	button1_1 = tkinter.Button(text='MC', bg='# 666', bd=2, command=lambda:pressOperator('MC'))
	button1_1.place(x=20, y=110, width=50, height=35)
	# ----Memory read
	button1_2 = tkinter.Button(text='MR', bg='# 666', bd=2, command=lambda:pressOperator('MR'))
	button1_2.place(x=77.5, y=110, width=50, height=35)
	# ----Memory save
	button1_3 = tkinter.Button(text='MS', bg='# 666', bd=2, command=lambda:pressOperator('MS'))
	button1_3.place(x=135, y=110, width=50, height=35)
	# ----Memory +
	button1_4 = tkinter.Button(text='M+', bg='# 666', bd=2, command=lambda:pressOperator('M+'))
	button1_4.place(x=192.5, y=110, width=50, height=35)
	# ----Memory -
	button1_5 = tkinter.Button(text='M-', bg='# 666', bd=2, command=lambda:pressOperator('M-'))
	button1_5.place(x=250, y=110, width=50, height=35)
	# -- second line
	# ---- Delete a single number
	button2_1 = tkinter.Button(text='del', bg='# 666', bd=2, command=lambda:delOne())
	button2_1.place(x=20, y=155, width=50, height=35)
	# ---- Clears all numbers in the current display box
	button2_2 = tkinter.Button(text='CE', bg='# 666', bd=2, command=lambda:clearCurrent())
	button2_2.place(x=77.5, y=155, width=50, height=35)
	# ---- Reset
	button2_3 = tkinter.Button(text='C', bg='# 666', bd=2, command=lambda:clearAll())
	button2_3.place(x=135, y=155, width=50, height=35)
	# - invert
	button2_4 = tkinter.Button(text='+ / -, bg='# 666', bd=2, command=lambda:pressOperator('+ / -))
	button2_4.place(x=192.5, y=155, width=50, height=35)
	# ---- take the square root
	button2_5 = tkinter.Button(text='sqrt', bg='# 666', bd=2, command=lambda:pressOperator('sqrt'))
	button2_5.place(x=250, y=155, width=50, height=35)
	# -- third line
	# 7 -
	button3_1 = tkinter.Button(text='7', bg='#bbbbbb', bd=2, command=lambda:pressNumber('7'))
	button3_1.place(x=20, y=200, width=50, height=35)
	8 # -
	button3_2 = tkinter.Button(text='8', bg='#bbbbbb', bd=2, command=lambda:pressNumber('8'))
	button3_2.place(x=77.5, y=200, width=50, height=35)
	# 9 -
	button3_3 = tkinter.Button(text='9', bg='#bbbbbb', bd=2, command=lambda:pressNumber('9'))
	button3_3.place(x=135, y=200, width=50, height=35)
	# - in addition to
	button3_4 = tkinter.Button(text='/', bg='# 708069', bd=2, command=lambda:pressOperator('/'))
	button3_4.place(x=192.5, y=200, width=50, height=35)
	# - take over
	button3_5 = tkinter.Button(text=The '%', bg='# 708069', bd=2, command=lambda:pressOperator(The '%'))
	button3_5.place(x=250, y=200, width=50, height=35)
	# -- line 4
	# 4 -
	button4_1 = tkinter.Button(text='4', bg='#bbbbbb', bd=2, command=lambda:pressNumber('4'))
	button4_1.place(x=20, y=245, width=50, height=35)
	# - 5
	button4_2 = tkinter.Button(text='5', bg='#bbbbbb', bd=2, command=lambda:pressNumber('5'))
	button4_2.place(x=77.5, y=245, width=50, height=35)
	# 6 -
	button4_3 = tkinter.Button(text='6', bg='#bbbbbb', bd=2, command=lambda:pressNumber('6'))
	button4_3.place(x=135, y=245, width=50, height=35)
	# - by
	button4_4 = tkinter.Button(text=The '*', bg='# 708069', bd=2, command=lambda:pressOperator(The '*'))
	button4_4.place(x=192.5, y=245, width=50, height=35)
	# ---- take the derivative
	button4_5 = tkinter.Button(text='1/x', bg='# 708069', bd=2, command=lambda:pressOperator('1/x'))
	button4_5.place(x=250, y=245, width=50, height=35)
	# -- line 5
	# - three
	button5_1 = tkinter.Button(text='3', bg='#bbbbbb', bd=2, command=lambda:pressNumber('3'))
	button5_1.place(x=20, y=290, width=50, height=35)
	# 2 -
	button5_2 = tkinter.Button(text='2', bg='#bbbbbb', bd=2, command=lambda:pressNumber('2'))
	button5_2.place(x=77.5, y=290, width=50, height=35)
	# 1 -
	button5_3 = tkinter.Button(text='1', bg='#bbbbbb', bd=2, command=lambda:pressNumber('1'))
	button5_3.place(x=135, y=290, width=50, height=35)
	#,
	button5_4 = tkinter.Button(text=The '-', bg='# 708069', bd=2, command=lambda:pressOperator(The '-'))
	button5_4.place(x=192.5, y=290, width=50, height=35)
	# - equivalent to
	button5_5 = tkinter.Button(text='=', bg='# 708069', bd=2, command=lambda:pressOperator('='))
	button5_5.place(x=250, y=290, width=50, height=80)
	# -- line 6
	0 # -
	button6_1 = tkinter.Button(text='0', bg='#bbbbbb', bd=2, command=lambda:pressNumber('0'))
	button6_1.place(x=20, y=335, width=107.5, height=35)
	# ---- decimal point
	button6_2 = tkinter.Button(text='. ', bg='#bbbbbb', bd=2, command=lambda:pressDP())
	button6_2.place(x=135, y=335, width=50, height=35)
	# -
	button6_3 = tkinter.Button(text='+', bg='# 708069', bd=2, command=lambda:pressOperator('+'))
	button6_3.place(x=192.5, y=335, width=50, height=35)
	root.mainloop()


if __name__ == '__main__':
	Demo()
Copy the code

That concludes this article, thank you for watching, and follow me in my daily series of Python gadgets. In my next article, I’ll share the Turtle library for making simple clocks

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

③ project source code (forty or fifty interesting and classic practice projects and source code)

④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)

⑤ A Roadmap for Learning Python

⑥ Two days of Python crawler boot camp live access

All done~ Complete source code + dry + Python novice learning community: 594356095