Preface:

Python makes a simple calculator. Let’s get started happily

The development tools

Related modules: Python comes with the Tkinter and Math modules. Environment set up

Install Python and add it to the environment variable.

Introduction of the principle

First, use Tkinter to build the basic calculator interface, which will not be explained in detail here. Please refer to the relevant documents, such as:

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

Key function is as follows: 0-9: number key. : decimal point + : addition – : subtraction * : multiplication / : division = : calculation result 1/x: take the reciprocal % : take the remainder del: backspace key CE: empty the current C: empty all +/- : take the reverse SQRT: square root MC: return the current memory number to zero MR: MS: Replace the number in memory directly with the current number, regardless of the number in memory. M+ : Memory the current number M- : Since it calls the Math library and Python’s eval function directly, it’s easy to implement, but I’ll cover a few of the details you need to pay attention to.

(1) Backspace operation

When there is only one number in the display box, use the backspace key to make the display box display the number 0, not empty.

(2) Illegal operation

To prevent the program from throwing an exception, illegal operations such as using 0 as the denominator or taking the square root of a negative number.

(3) Set the maximum length of the current display character

Because the length of the display box is limited, the number of characters that can be displayed is also limited, so the maximum length of the characters currently displayed needs to be set. For example, the user can not enter a series of numbers without limit, the final result can only retain the first number of digits, and so on.

Part of the code

'''Demo''' def Demo(): Root.minSize (320, 420) root.title('Calculator') # Layout # -- TextBox = Tkinter.label (root, TextVariable = currentShow, Bg = 'black', the anchor = 'e', bd = 5, fg = 'white', the font = (' regular script, 20)) label. Place (x = 20, y = 50, width = 280, Height =50) # ----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, The command = lambda: pressOperator (' MR ')) button1_2. Place (= 77.5 x, 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, The command = lambda: pressOperator (' M +)) button1_4. Place (= 192.5 x, 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, Button2_1 = tkinter.Button(text='del', bg='#666', bd=2) command=lambda:delOne()) button2_1.place(x=20, y=155, width=50, # ---- Button2_2 = tkinter.Button(text='CE', bg='#666', bd=2) The command = lambda: clearCurrent button2_2 ()). The place (= 77.5 x, y = 155, width = 50. Height =35) # ---- Button2_3 = tkinter.Button(text='C', bg='#666', bd=2) command=lambda:clearAll()) button2_3.place(x=135, y=155, width=50, Height = # 35), take the button2_4 = tkinter. The Button (text = '+ / -, bg =' # 666, bd = 2, The command = lambda: pressOperator button2_4 (' + / -)). The place (= 192.5 x, y = 155, width = 50. Height =35) # ---- Button2_5 = tkinter.Button(text=' SQRT ', bg='#666', bd=2) command=lambda:pressOperator('sqrt')) button2_5.place(x=250, y=155, width=50, ----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, The command = lambda: pressNumber (' 8 ')) button3_2. Place (= 77.5 x, 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) -- except button3_4 = tkinter. Button (text = '/', bg = '# 708069, bd = 2, The command = lambda: pressOperator button3_4 ('/')). The place (= 192.5 x, y = 200, width = 50. Height =35) # ---- Remain button3_5 = tkinter.Button(text='%', bg='#708069', bd=2, command=lambda:pressOperator('%')) button3_5.place(x=250, y=200, width=50, ----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, The command = lambda: pressNumber (' 5 ')) button4_2. Place (= 77.5 x, 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 = '*', bg = '# 708069, bd = 2, The command = lambda: pressOperator (' * ')) button4_4. Place (= 192.5 x, y = 245, width = 50. 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, ----3 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, The command = lambda: pressNumber button5_2 (' 2 ')). The place (= 77.5 x, 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) -- minus button5_4 = tkinter. Button (text = '-', bg = '# 708069, bd = 2, The command = lambda: pressOperator (' - ')) button5_4. Place (= 192.5 x, y = 290, width = 50. Height =35) # ---- = Button5_5 = Tkinter.Button(text='=', bg='#708069', bd=2, command=lambda:pressOperator('=')) button5_5.place(x=250, y=290, width=50, ----0 Button6_1 = tkinter.Button(text='0', bg='# BBBBBB ', bd=2) The command = lambda: pressNumber (' 0 ')) button6_1. Place (x = 20, y = 335, width = 107.5, Height =35) # ---- Button6_2 = tkinter.Button(text='.', bg='# BBBBBB ', bd=2, command=lambda:pressDP()) button6_2.place(x=135, y=335, width=50, Height = # 35), plus button6_3 = tkinter. The Button (text = '+', bg = '# 708069, bd = 2, The command = lambda: pressOperator (' + ')) button6_3. Place (= 192.5 x, y = 335, width = 50. height=35) root.mainloop() if __name__ == '__main__': Demo()

That concludes the article. Thank you for watching. Follow my daily series on Python widgets

To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.

Dry goods mainly include:

① More than 2000 Python e-books (mainstream and classic books are available)

(2) The Python Standard Library (Chinese version)

③ project source code (40 or 50 interesting and classic hands-on project and source code)

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

⑤Python Learning Roadmap (Goodbye to Slow Learning)

⑥ Two days of Python crawler training camp live access

All done~ complete source code + dry goods see personal profile or private message to obtain the relevant files.