Python easy calculator making

832101222赵浩楠 2023-10-09 21:46:26
The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentA calculator with a visual interface
MU STU ID and FZU STU IDMU: 21125147   FZU: 832101222

 

目录

目录

I. Introduction 

II. PSP table

III. Problem-solving ideas

IV. Design and implementation

V. Code description

VII. Summary



I. Introduction 

In this blog, I will explain how to implement a visual calculator using python. And how to implement the basic functions of calculator like addition, subtraction, multiplication and division. On top of that, I have also added trigonometric functions.

 

II. PSP table

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning2020
• Estimate2020
Development150150
• Analysis1010
• Design Spec2020
• Design Review2020
• Coding Standard2020
• Design2020
• Coding3030
• Code Review1010
• Test2020
Reporting4040
• Test Repor1010
• Size Measurement1010
• Postmortem & Process Improvement Plan2020
Sum210210

 

 

III. Problem-solving ideas

First of all, we need to make a visual interface, and for that I chose to make it in the python language that I learned in class. But when I clicked a button on the interface I didn't know how to fire an event, so I chose to look for tutorials on Bilibili. Next is the logic writing part, for simple arithmetic I chose to use eval functions. The more complex trigonometric functions I wrote myself, in addition to converting the angle and radian systems.

 

IV. Design and implementation

V. Code description


import tkinter as tk
import math
 
root = tk.Tk()
 
root.title('Simple Calculator')
root.geometry("295x310+150+150")
 
root.attributes("-alpha",0.9)
root["background"] = "#ffffff"
font = ('宋体', 20)
font_16 = ('宋体', 16)
result_num = tk.StringVar()
result_num.set('')
 
tk.Label(root,
          textvariable=result_num, font=font, height=2,
         width=20, justify=tk.LEFT, anchor=tk.SE
         ).grid(row=1, column=1, columnspan=4)
 
#buttons
button_clear = tk.Button(root, text='AC', width=5, font=font_16, relief=tk.FLAT, bg='#6495ed')
button_back = tk.Button(root, text='←', width=5, font=font_16, relief=tk.FLAT, bg='#6495ed')
button_division = tk.Button(root, text='÷', width=5, font=font_16, relief=tk.FLAT, bg='#c8c7c5')
button_multiplication = tk.Button(root, text='×', width=5, font=font_16, relief=tk.FLAT, bg='#c8c7c5')
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='#b1b2b2')
button_eight = tk.Button(root, text='8', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_nine = tk.Button(root, text='9', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_subtraction = tk.Button(root, text='-', width=5, font=font_16, relief=tk.FLAT, bg='#c8c7c5')
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='#b1b2b2')
button_five = tk.Button(root, text='5', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_six = tk.Button(root, text='6', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_addition = tk.Button(root, text='+', width=5, font=font_16, relief=tk.FLAT, bg='#c8c7c5')
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_addition.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='#b1b2b2')
button_two = tk.Button(root, text='2', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_three = tk.Button(root, text='3', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_equal = tk.Button(root, text='=', width=5, height = 5, font=font_16, relief=tk.FLAT, bg='#8fd1e1')
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=3)
 
button_zero = tk.Button(root, text='0', width=12, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_point = tk.Button(root, text='.', width=5, font=font_16, relief=tk.FLAT, bg='#b1b2b2')
button_zero.grid(row=6, column=1, padx=4, pady=2, columnspan=2)
button_point.grid(row=6, column=3, padx=4, pady=2)
 
button_sin = tk.Button(root, text='sin', width=5, font=font_16, relief=tk.FLAT, bg='#c8c7c5')
button_cos = tk.Button(root, text='cos', width=5, font=font_16, relief=tk.FLAT, bg='#c8c7c5')
button_tan = tk.Button(root, text='tan', width=5, font=font_16, relief=tk.FLAT, bg='#c8c7c5')
button_sin.grid(row=7, column=1, padx=4, pady=2)
button_cos.grid(row=7, column=2, padx=4, pady=2)
button_tan.grid(row=7, column=3, padx=4, pady=2)

def click_button(x):
    print('x:\t',x)
    result_num.set(result_num.get() + x)
 
def calculation():
    opt_str = result_num.get()
    result = eval(opt_str)
    result_num.set(str(result))
 
def back():
    opt_str=result_num.get()
    opt_str=opt_str[:-1]
    result_num.set(opt_str)
 
def clear():
    opt_str = ""
    result_num.set(opt_str)
 
def sin():
    opt_str = result_num.get()
    result = math.sin((float(opt_str)*math.pi)/180)
    result_num.set(str(result))
 
def cos():
    opt_str = result_num.get()
    result = math.cos((float(opt_str)*math.pi)/180)
    result_num.set(str(result))
 
def tan():
    opt_str = result_num.get()
    result = math.tan((float(opt_str)*math.pi)/180)
    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_addition.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_point.config(command=lambda: click_button('.'))
button_back.config(command=lambda: click_button('←'))
button_clear.config(command=lambda: click_button(''))
button_equal.config(command=lambda: click_button('='))
 
button_sin.config(command=lambda: click_button('sin'))
button_cos.config(command=lambda: click_button('cos'))
button_tan.config(command=lambda: click_button('tan'))
 
button_back.config(command=back)
button_clear.config(command=clear)
button_sin.config(command=sin)
button_cos.config(command=cos)
button_tan.config(command=tan)
button_equal.config(command=calculation)
 
root.mainloop()

 

 

VI.Displaying result functions

 

VII. Summary

By doing this programming assignment, I learned how to use Python to create applications with GUIs and implement basic calculator functions. This project helped me improve my event handling and user interface design skills. I also learned how to organize my code so that it is easy to maintain and extend. Overall, this assignment was a valuable learning experience for me.

 

localflvcko/caculator (github.com)

 

...全文
950 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

176

社区成员

发帖
与我相关
我的任务
社区描述
梅努斯软件工程
软件工程 高校 福建省·福州市
社区管理员
  • LinQF39
  • Jcandc
  • chjinhuu
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧