The case shows
Calculator. GIF
You can learn
-
Input User input
-
The print output
-
Tkinter graphical interface
-
Python operation symbol
Basic knowledge preparation
Sign of operation
Sum, sum we use the plus sign (+) operator, in addition, there is a minus sign (-), multiply (*), division sign (/), flooring (/ /) or (%).
The input input
In Python we use input to get input. By default, input takes text only. To convert to a number, we use int to convert a string to a character
The output
We use the print method in python3
Tkinter graphical interface
Tkinter is Python’s standard GUI library. Python uses Tkinter to create GUI applications quickly. Since Tkinter is built into the Python installation package, the Tkinter library can be imported once Python is installed, and IDLE is written in Tkinter, Tkinter can handle simple graphical interfaces very well.
Basic version
The case shows
Calculator simple version.gif
Num1 = int(input(" input first number: Num2 = int(input(" input second number: "))# print(" input operation: 1, add; 2, subtract; 3. Multiply; Choice = input(" enter your choice (1/2/3/4):") # if choice == '1': Print (num2 num1, "+", "=", num1 + num2) # 2 elif if choice = = '2' : print (num2 num1, "-", "=", num1 - num2) elif choice = = '3' : Print (num2 num1, "x", "=", num1 * num2) elif choice = = '4' : print (num2 num1, "present", "=", num1 / num2) # other else is illegal: print (" illegal input ")Copy the code
Special attention:
-
Input Prompts the user to enter a number on the computer keyboard
-
We convert the input number to a real number (the default is numeral-like characters) by using the int method
-
Output different content according to choice
-
In the program world, division is divided by/and times by *
The minimalist version
The minimalist version
Print (eval(STR)); print(eval(STR));Copy the code
Note:
-
Input Gets the content as text
-
Eval can be used to execute any string as python
If we want our program to run all the time we can just put our code in a while loop
While True: print(eval(STR))Copy the code
Note:
-
The T of True is capitalized
-
The STR and print functions are preceded by four Spaces or a Tab indent
Calculator (with interface)
Calculator. GIF
Window = tkinter.tk ()# Set the title window.title(' calculator ')# Set window size window.geometry('200x200') # Input method def add(n): Insert (0,n1+ STR (n)) # def calc(): Inp. Insert (0, STR (eval(n1))) def clear(): N1 = inp.get() inp.delete(0,len(n1)) N1 = inp.get() inp.delete(len(n1)-1,len(n1)) N1 = inp.get() inp.delete(0,len(n1)) inp.insert(0, STR (eval(n1)*-1)) Grid (row=0,column=0, columnSPAN =5) # create for loop 123 456 789 9 buttons for I in range(0,3): For j in range (1, 4) : n = j+i*3 btn=tkinter.Button(window, text=str(j+i*3),width=5, Command =lambda n=n:add(n)) btn.grid(row= I +2,column= J-1) Grid (1 row,0 column)tkinter.Button(window,width=5, text="C", The command = clear). The grid (row = 1, the column = 0) tkinter. The Button (Windows, width = 5, the text = "please." command=back).grid(row=1,column=1)tkinter.Button(window,width=5, text="+/-", Row =1,column=2). Grid (row=1,column=2) Grid (1 row,4 columns)tkinter.Button(window,width=5, text="+",bg="#f70",fg="#fff",command=lambda:add("+")).grid(row=1,column=4)tkinter.Button(window,width=5, text="-", bg="#f70",fg="#fff",command=lambda:add("-")).grid(row=2,column=4)tkinter.Button(window,width=5, Text = "x", bg = "# f70", fg = "# FFF", the command = lambda: add (" * ")). The grid (row = 3, column = 4) tkinter. The Button (Windows, width = 5, Text = "present", bg = "# f70", fg = "# FFF", the command = lambda: add ("/")). The grid (row = 4, the column = 4) tkinter. The Button (Windows, width = 12, the text = "0", command=lambda:add("0")).grid(row=5,column=0,columnspan=2)tkinter.Button(window,width=5,text="=", bg="#f70",fg="#fff",command=calc).grid(row=5,column=4)tkinter.Button(window,width=5, text=".", Command =lambda:add("."). Grid (row=5,column=2)Copy the code
Pay attention to
-
Eval executes a string as python code
-
The len() method calculates the string length
-
Inp.get () gets the value of the text box
-
Inp. delete(start,end) Deletes characters from the text box from start to end
-
Insert (start, STR) Inserts text box characters from start
Well, that’s all for today’s sharing. If you’re interested in Python, join us.Python Learning communicationSkirt 】, receive free learning materials and source code.
– EOF –