Python programming uses the tkinter module to implement the complete code example of the calculator software, pythontkinter

Source: Internet
Author: User

Python programming uses the tkinter module to implement the complete code example of the calculator software, pythontkinter

Python provides libraries for multiple graphic development interfaces. Tkinter is one of them. Tkinter module (Tk Interface) is the interface of Python's standard Tk GUI toolkit. Tk and Tkinter can be used on most Unix platforms, and can also be used in Windows and Macintosh systems. Later versions of Tk8.0 can implement the local window style and run well on most platforms.

This calculator is developed using the Python tkinter module.

Results:

Import tkinter # import tkinter module root = tkinter. tk () root. min size (280,500) root. title ('Lee Long's calculator') #1. interface layout # display panel result = tkinter. stringVar () result. set (0) # result 1 of the display panel, which is used to display the default number 0result2 = tkinter. stringVar () # display panel result 2, used to display the computing process result2.set ('') # display version label = tkinter. label (root, font = (' ', 20), bg =' # eee9e9', bd = '9', fg = '#828282 ', anchor = 'se', textvariable = result2) label. place (width = 280, height = 170) label2 = tkinter. label (root, font = (' ', 30), bg =' # eee9e9', bd = '9', fg = 'black', anchor = 'se ', textvariable = result) label2.place (y = 170, width = 280, height = 60) # number key button btn7 = tkinter. button (root, text = '7', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('7') btn7.place (x = 0, y = 285, width = 70, height = 55) btn8 = tkinter. button (root, text = '8', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('8') btn8.place (x = 70, y = 285, width = 70, height = 55) btn9 = tkinter. button (root, text = '9', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('9') btn9.place (x = 140, y = 285, width = 70, height = 55) btn4 = tkinter. button (root, text = '4', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('4') btn4.place (x = 0, y = 340, width = 70, height = 55) btn5 = tkinter. button (root, text = '5', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('5') btn5.place (x = 70, y = 340, width = 70, height = 55) btn6 = tkinter. button (root, text = '6', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('6') btn6.place (x = 140, y = 340, width = 70, height = 55) btn1 = tkinter. button (root, text = '1', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('1') btn1.place (x = 0, y = 395, width = 70, height = 55) btn2 = tkinter. button (root, text = '2', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('2') btn2.place (x = 70, y = 395, width = 70, height = 55) btn3 = tkinter. button (root, text = '3', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('3') btn3.place (x = 140, y = 395, width = 70, height = 55) btn0 = tkinter. button (root, text = '0', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressNum ('0') btn0.place (x = 70, y = 450, width = 70, height = 55) # Operator number button btnac = tkinter. button (root, text = 'ac', bd = 0.5, font = ('simhei ', 20), fg = 'Orange', command = lambda: pressCompute ('ac') btnac. place (x = 0, y = 230, width = 70, height = 55) btnback = tkinter. button (root, text = 'hangzhou', font = (' ', 20), fg =' # 4F4F4F ', bd = 0.5, command = lambda: pressCompute ('B') btnback. place (x = 70, y = 230, width = 70, height = 55) btndivi = tkinter. button (root, text = 'hangzhou', font = (' ', 20), fg =' # 4F4F4F ', bd = 0.5, command = lambda: pressCompute ('/') btndivi. place (x = 140, y = 230, width = 70, height = 55) btnmul = tkinter. button (root, text = '×', font = (' ', 20), fg = "# 4F4F4F", bd = 0.5, command = lambda: pressCompute ('*') btnmul. place (x = 210, y = 230, width = 70, height = 55) btnsub = tkinter. button (root, text = '-', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressCompute ('-') btnsub. place (x = 210, y = 285, width = 70, height = 55) btnadd = tkinter. button (root, text = '+', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressCompute ('+') btnadd. place (x = 210, y = 340, width = 70, height = 55) btnequ = tkinter. button (root, text = ', bg = 'Orange', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressEqual () btnequ. place (x = 210, y = 395, width = 70, height = 110) btnper = tkinter. button (root, text = '%', font = (' ', 20), fg = (' # 4F4F4F '), bd = 0.5, command = lambda: pressCompute ('%') btnper. place (x = 0, y = 450, width = 70, height = 55) btnpoint = tkinter. button (root, text = '. ', font = (' ', 20), fg = ('# 4F4F4F'), bd = 0.5, command = lambda: pressCompute ('. ') btnpoint. place (x = 140, y = 450, width = 70, height = 55) # operation function lists = [] # Set a variable to save the list of arithmetic numbers and symbols isPressSign = False # Add a flag to determine whether to press the operator number, assume that the isPressNum = False # Number function def pressNum (num) is not pressed by default ): # Set a numeric function to determine whether to press the number and obtain the number to write the number in the global lists on the display version # compile the lists and the button status isPressSign global isPressSign if isPressSign = False: pass else: # Set the operator number status to "no result" again. set (0) isPressSign = False # determine whether the number on the page is 0 oldnum = result. get () # Step 1 if oldnum = '0': # if the number on the border is 0, the result of the pressed number is obtained. set (num) else: # If the number on the interface is not 0, the new number newnum = oldnum + num result is linked. set (newnum) # write the number that is pressed to the Panel # operation function def pressCompute (sign): global lists global isPressSign num = result. get () # obtain the number lists on the page. append (num) # Save the number obtained on the page to lists in the list. append (sign) # Save the operator number to the list. isPressSign = True if sign = 'ac': # if you press 'ac', the list content is cleared, set the number key on the lecture screen to the default number 0 lists. clear () result. set (0) if sign = 'B': # if you press return '', select the first and last digits of the current number a = num [0: -1] lists. clear () result. set (a) # obtain the operation result function def pressEqual (): global lists global isPressSign curnum = result. get () # set the current numeric variable and obtain the value added to the lists list. append (curnum) computrStr = ''. join (lists) # Use the join command to link the list content to the string endNum = eval (computrStr) # Use the eval command to calculate the content of the string # a = str (endNum) # B = '+ a # Add a' = 'before the calculation result. However, if this is the case, a BUG cannot be solved consecutively, do not = # c = B [0: 10] # obtain 9-digit results for all calculation results. set (endNum) # Show the operation result to screen 1 result2.set (computrStr) # display the operation process to screen 2 lists. clear () # clear the list content root. mainloop ()

Summary

The above is all the content about the complete code example of the calculator software using the tkinter module in Python programming. I hope it will be helpful to you. Interested friends can continue to refer to this site:

Python can draw such a beautiful flower (code sharing)

Example of turtle plotting in Python

Python programming implementation Ant Colony Algorithm details

If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

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.