In this article, we share the example of python to achieve a simple calculator specific code for your reference, the specific content is as follows
Today I learned about the interface design part of python, and a few common graphical interface libraries are Jython, wxPython and tkinter.
The main introduction to the tkinter module, the tkinter module (tk interface) is an interface to Python's standard tk GUI toolkit. tk and tkinter can be used under most UNIX platforms, and can be applied equally well to Windows and Macintosh systems. subsequent versions of Tk 8.0 enable native window styles, and run well tk and tkinter can be used on most UNIX platforms as well as Windows and Macintosh.
The following calculator function is completed using the tkinter design.
(1) First present the initial interface of the calculator:
(2) Simple explanation: the basic functions of the calculator have been realized.
(3) Major code descriptions:
①Import Package
import tkinter from tkinter import * import re import
②Interface Layout Settings
# Create the main window root = Tk() # Set window size and position ("--- Calculator---") ("320x210+500+200") # Auto-refresh string variables with set and get methods for passing and retrieving values contentVar = (root,'') # Create single line text boxes contentEntry = (root, textvariable=contentVar) # Set the coordinates and width of the text box (x=20, y=10, width=260, height=30) # Button display content bvalue = ['CLC', '+', '-', '//', '0', '1', '2', '√', '3', '4', '5', '*', '6', '7', '8', '.', '9', '/', '**', '='] index = 0 # 5x4 placement of buttons for row in range(5): for col in range(4): d = bvalue[index] index += 1 btnDigit = (root, text=d, command=lambda x=d:onclick(x)) (x=20 + col * 70, y=50 + row * 30, width=50, height=20) ()
③ Response function for button events (can be exchanged in the comment section)
# Click events def onclick(btn): # Operators operation = ('+', '-', '*', '/', '**', '//') # Get the contents of the text box content = () # If the existing content begins with a decimal point, precede it with 0 if ('.'): content = '0' + content # Strings can be augmented with characters directly with + # Respond differently to different buttons if btn in '0123456789': # Press 0-9 to append to content content += btn elif btn == '.': # Split the content from the +-*/ characters. lastPart = (r'\+|-|\*|/', content)[-1] if '.' in lastPart: # Information Alert dialog box ('Error', 'Recurring decimal points') return else: content += btn elif btn == 'CLC': # Clear the text box content = '' elif btn == '=': try: # Evaluate the input expression content = str(eval(content)) except: ('Error', 'The expression is wrong') return elif btn in operation: if (operation): ('Error', 'Continuous operators are not allowed') return content += btn elif btn == '√': # Split from . Split at n, n is a list n = ('.') # If all the ones in the list are numbers, it's to check if the expression is the right one if all(map(lambda x: (), n)): content = eval(content) ** 0.5 else: ('Error', 'Expression error') return
This is the entire content of this article.