How to Use Python to write a calculator software with renderings, python Renderings

Source: Internet
Author: User

How to Use Python to write a calculator software with python

This calculator is developed using the Python tkinter module.

Results:

 

1 import tkinter # import tkinter module 2 3 root = tkinter. tk () 4 root. minsize (280,500) 5 root. title ('Lee Long's calculator') 6 7 8 #1. page Layout 9 # display panel 10 result = tkinter. stringVar () 11 result. set (0) # display panel result 1, used to display the default number 0 12 result2 = tkinter. stringVar () # display panel result 2, used to display the computing process 13 result2.set ('') 14 # display version 15 label = tkinter. label (root, font = (' ', 20), bg =' # eee9e9', bd = '9', fg = '#828282 ', anchor = 'se', textvariable = result2) 16 label. place (width = 280, height = 170) 17 label2 = tkinter. label (root, font = (' ', 30), bg =' # eee9e9', bd = '9', fg = 'black', anchor = 'se ', textvariable = result) 18 label2.place (y = 170, width = 280, height = 60) 19 20 21 22 23 # number key button 24 25 btn7 = tkinter. button (root, text = '7', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('7') 26 btn7.place (x = 0, y = 285, width = 70, height = 55) 27 btn8 = tkinter. button (root, text = '8', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('8') 28 btn8.place (x = 70, y = 285, width = 70, height = 55) 29 btn9 = tkinter. button (root, text = '9', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('9') 30 btn9.place (x = 140, y = 285, width = 70, height = 55) 31 32 btn4 = tkinter. button (root, text = '4', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('4') 33 btn4.place (x = 0, y = 340, width = 70, height = 55) 34 btn5 = tkinter. button (root, text = '5', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('5') 35 btn5.place (x = 70, y = 340, width = 70, height = 55) 36 btn6 = tkinter. button (root, text = '6', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('6') 37 btn6.place (x = 140, y = 340, width = 70, height = 55) 38 39 btn1 = tkinter. button (root, text = '1', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('1') 40 btn1.place (x = 0, y = 395, width = 70, height = 55) 41 btn2 = tkinter. button (root, text = '2', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('2') 42 btn2.place (x = 70, y = 395, width = 70, height = 55) 43 btn3 = tkinter. button (root, text = '3', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('3') 44 btn3.place (x = 140, y = 395, width = 70, height = 55) 45 btn0 = tkinter. button (root, text = '0', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('0') 46 btn0.place (x = 70, y = 450, width = 70, height = 55) 47 48 49 # Operator number button 50 btnac = tkinter. button (root, text = 'ac', bd = 0.5, font = ('simhei ', 20), fg = 'Orange', command = lambda: pressCompute ('ac') 51 btnac. place (x = 0, y = 230, width = 70, height = 55) 52 btnback = tkinter. button (root, text = 'hangzhou', font = (' ', 20), fg =' # 4F4F4F ', bd = 0.5, command = lambda: pressCompute ('B') 53 btnback. place (x = 70, y = 230, width = 70, height = 55) 54 btndivi = tkinter. button (root, text = 'hangzhou', font = (' ', 20), fg =' # 4F4F4F ', bd = 0.5, command = lambda: pressCompute ('/') 55 btndivi. place (x = 140, y = 230, width = 70, height = 55) 56 btnmul = tkinter. button (root, text = '×', font = (' ', 20), fg = "# 4F4F4F", bd = 0.5, command = lambda: pressCompute ('*') 57 btnmul. place (x = 210, y = 230, width = 70, height = 55) 58 btnsub = tkinter. button (root, text = '-', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressCompute ('-') 59 btnsub. place (x = 210, y = 285, width = 70, height = 55) 60 btnadd = tkinter. button (root, text = '+', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressCompute ('+') 61 btnadd. place (x = 210, y = 340, width = 70, height = 55) 62 btnequ = tkinter. button (root, text = ', bg = 'Orange', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressEqual () 63 btnequ. place (x = 210, y = 395, width = 70, height = 110) 64 btnper = tkinter. button (root, text = '%', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressCompute ('%') 65 btnper. place (x = 0, y = 450, width = 70, height = 55) 66 btnpoint = tkinter. button (root, text = '. ', font = (' ', 20), fg = ('# 4F4F4F'), bd = 0.5, command = lambda: pressCompute ('. ') 67 btnpoint. place (x = 140, y = 450, width = 70, height = 55) 68 69 70 71 72 # operation function 73 lists = [] # Set a variable to save the list of arithmetic numbers and symbols 74 isPressSign = False # Add a flag to determine whether to press the operator number, assume that the 75 isPressNum = False 76 # numeric function 77 def pressNum (num) is not pressed by default ): # Set a number function to determine whether to press the number and obtain the number to write the number in the display version 78 global lists # compile the lists and the button status isPressSign 79 global isPressSign 80 if isPressSign = False: 81 pass 82 else: # reset the operator number status to No 83 result. set (0) 84 isPressSign = False 85 86 # determine whether the number on the page is 0 87 oldnum = result. get () # Step 88 if oldnum = '0': # obtain the 89 result of the pressed number if the number on the border is 0. set (num) 90 else: # If the number on the interface is not 0, the new number 91 newnum = oldnum + num 92 result is linked. set (newnum) # write the number pressed to the Panel 93 94 95 96 97 98 99 100 101 # calculation function 102 def pressCompute (sign): 103 global lists104 global isPressSign105 num = result. get () # obtain the page number 106 lists. append (num) # Save the number obtained on the page to 107 lists in the list. append (sign) # Save the operator number to the list. 109 isPressSign = True110 111 if sign = 'ac': # if you press 'ac, the list content is cleared. The number key on the lecture screen is set to the default number 0112 lists. clear () 113 result. set (0) 114 if sign = 'B': # if you press return '', select 115 a = num [0: -1] 116 lists. clear () 117 result. set (a) 118 119 120 121 # obtain the calculation result function 122 def pressEqual (): 123 global lists124 global isPressSign125 126 127 curnum = result. get () # set the current numeric variable and obtain the value added to lists. append (curnum) 129 130 computrStr = ''. join (lists) # Use the join command to link the list content to the string 131 endNum = eval (computrStr) # Use the eval command to calculate the content of the string 132 # a = str (endNum) 133 # B = '+ a # Add a' = 'before the calculation result. However, if this is the case, a BUG cannot be solved consecutively. comment here, do not = 134 # c = B [] # obtain 9-digit 135 results for all calculation results. set (endNum) # Show the operation result to screen 1136 result2.set (computrStr) # display the operation process to screen 2137 lists. clear () # clear the list content 138 139 140 141 142 root. mainloop ()

QQ: 353145551: July 145551

You are welcome to comment and guide. If you are interested in Python and want to talk to the blogger about technology, love, ideal, and private life, you can add a blogger account.

Because the number of words is insufficient, 10 thousand words are omitted here and replaced #.

######################################## #######

######################################## ########################

######################################## ######################################## ##

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.