The Link Your Class | https://bbs.csdn.net/forums/ssynkqtd-04 |
---|---|
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/617332156 |
The Aim of This Assignment | A calculator with a visual interface |
MU STU ID and FZU STU ID | MU: 21124337 FZU: 832101212 |
contents
VI. Displaying result functions
I. Introduction
In this blog I will explain how I implemented the visual calculator using python's Tkinter module. And how to realize the basic functions of the calculator such as addition, subtraction, multiplication and division. On this basis, I also added the function of trigonometry and exponential calculation to it.
II. PSP table
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning | ||
• Estimate | 60 | 80 |
Development | ||
• Analysis | 20 | 25 |
• Design Spec | 5 | 5 |
• Design Review | 5 | 10 |
• Coding Standard | 2 | 3 |
• Design | 15 | 12 |
• Coding | 40 | 35 |
• Code Review | 10 | 8 |
• Test | 10 | 10 |
Reporting | ||
• Test Report | 40 | 45 |
• Size Measurement | 5 | 8 |
• Postmortem & Process Improvement Plan | 10 | 15 |
Sum | 222 | 256 |
III. Problem-solving ideas
First of all, we need to make a visual interface, for which I chose the most familiar python Tkinter module to make. But I didn't know how to start an event when I clicked a button in the interface, so I chose to look for a tutorial on Bilibili. Second is the logic writing part, simple operations I choose to use the eval function to solve. The more complicated trigonometric functions and exponential operations I intend to write myself. One of the withdrawal function at first did not think of a solution, and then I wanted to use the string to remove the end of the character to achieve, so I looked for the use of slice function on the Internet to solve the problem.
IV. Design and implementation
V. Code description
1. Enter the numbers and symbols and the code for the operation.
def click_button(a):
result_num.set(result_num.get() + a)
def calculation():
A = result_num.get()
result = eval(A)
result_num.set(str(result))
button_one.config(command=lambda: click_button('1'))
button_two.config(command=lambda: click_button('2'))
button_three.config(command=lambda: click_button('3'))
button_four.config(command=lambda: click_button('4'))
button_five.config(command=lambda: click_button('5'))
button_six.config(command=lambda: click_button('6'))
button_seven.config(command=lambda: click_button('7'))
button_eight.config(command=lambda: click_button('8'))
button_nine.config(command=lambda: click_button('9'))
button_zero.config(command=lambda: click_button('0'))
button_dot.config(command=lambda: click_button('.'))
button_plus.config(command=lambda: click_button('+'))
button_subtraction.config(command=lambda: click_button('-'))
button_multiplication.config(command=lambda: click_button('*'))
button_division.config(command=lambda: click_button('/'))
button_square.config(command=lambda: click_button('**'))
button_equal.config(command=calculation)
2. Code for clearing and reversing operations
def clear():
result_num.set('')
def back():
r = result_num.get()
new_r = r[:-1]
result_num.set(str(new_r))
button_clear.config(command=clear)
button_back.config(command=back)
3. Code for trigonometric operation
def cos():
COS = result_num.get()
new_COS = math.cos((float(COS)*math.pi)/180)
result_num.set(int(new_COS))
def sin():
SIN = result_num.get()
new_SIN = math.sin((float(SIN)*math.pi)/180)
result_num.set(int(new_SIN))
def tan():
TAN = result_num.get()
new_TAN = math.tan((float(TAN)*math.pi)/180)
result_num.set(int(new_TAN))
button_cos.config(command=cos)
button_sin.config(command=sin)
button_tan.config(command=tan)
4. The setting of each button in the calculator interface
button_clear = tk.Button(root, text='C', width=5, font=font_16, relief=tk.FLAT, bg='#B8860B')
button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#B8860B')
button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#B8860B')
button_multiplication = tk.Button(root, text='x', width=5, font=font_16, relief=tk.FLAT, bg='#B8860B')
button_clear.grid(row=2, column=1, padx=4, pady=2)
button_back.grid(row=2, column=2, padx=4, pady=2)
button_division.grid(row=2, column=3, padx=4, pady=2)
button_multiplication.grid(row=2, column=4, padx=4, pady=2)
button_seven = tk.Button(root, text='7', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#B8860B')
button_seven.grid(row=3, column=1, padx=4, pady=2)
button_eight.grid(row=3, column=2, padx=4, pady=2)
button_nine.grid(row=3, column=3, padx=4, pady=2)
button_subtraction.grid(row=3, column=4, padx=4, pady=2)
button_four = tk.Button(root, text='4', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_plus = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#B8860B')
button_four.grid(row=4, column=1, padx=4, pady=2)
button_five.grid(row=4, column=2, padx=4, pady=2)
button_six.grid(row=4, column=3, padx=4, pady=2)
button_plus.grid(row=4, column=4, padx=4, pady=2)
button_one = tk.Button(root, text='1', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_equal = tk.Button(root, text='=', width=5, height=3, font=font_16, relief=tk.FLAT, bg='#B8860B')
button_one.grid(row=5, column=1, padx=4, pady=2)
button_two.grid(row=5, column=2, padx=4, pady=2)
button_three.grid(row=5, column=3, padx=4, pady=2)
button_equal.grid(row=5, column=4, padx=4, pady=2, rowspan=2)
button_zero = tk.Button(root, text='0', width=12, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_dot = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#eacda1')
button_zero.grid(row=6, column=1, padx=4, pady=2, columnspan=2)
button_dot.grid(row=6, column=3, padx=4, pady=2)
VI. Displaying result functions
VII. Summary
In this assignment, I designed a calculator with a visual interface using Python. Here are the steps and procedures for me to complete this assignment:
(1) Import the required libraries: I first imported the Tkinter library, which is the standard library in Python for creating GUI interfaces.
(2) Create the main window: I used the Tkinter library to create a main window and set the title and size of the window.
(3) Create display: I created a text box on the main window to display the calculation result and the expression entered by the user.
(4) Create buttons: I used the Tkinter library's Button class to create a series of buttons for the user to enter numbers and operators.
(5) Add button function: I added corresponding function for each button, such as when clicking the number button, the corresponding number will be displayed on the display screen; When you click the operator button, the corresponding operator is displayed on the display.
(6) Implement the calculation function: I use the eval() function to evaluate the expression entered by the user and display the result on the display screen.
(7) Improve the interface: I made some cosmetic changes to the interface, such as setting the color and size of the buttons and adjusting the font and alignment of the display.
(8) Testing and debugging: I tested the calculator and fixed some possible bugs.
By doing this assignment, I learned how to use Python and Tkinter libraries to design a calculator with a simple visual interface. I learned the basic concepts and techniques of GUI programming and improved my programming skills.