Local visual calculator program based on Python

① Project introduction

This is a blog about developing a program that can meet the requirement of EE301
Further, I developed a calculator using Python language that can implement addition, subtraction, multiplication and division operations and can be opened directly locally with concrete views.
The address of downloading the program

② Assignment Table

InformationDetail
Class Linkhttps://bbs.csdn.net/forums/ssynkqtd-04
Project requirementhttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentDevelop a visual calculator
MU STU ID and FZU STU ID21124507(MU)/832101327(FZU)

③PSP Table

PSP form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning
• Estimate2020
Development
• Analysis1515
• Design Spec1510
• Design Review2530
• Coding Standard1515
• Design3040
• Coding120100
• Code Review3020
• Test1010
Reporting
• Test Repor12070
• Size Measurement55
• Postmortem & Process Improvement Plan1515
Sum400330

④Task objectives and realization ideas

④.1 Task objectives

1.Realize visual calculator, and have basic functions
2.Do not need other files, click to run, to achieve similar exe file functions

④.2 Realization ideas

Based on Python’s tkinter library development, first set the format and layout of specific visual buttons, and then set the respective functions of the buttons and the display of the result.
The buttons include number 0 to 9,noting that the button for the number 0 needs to cross columns; The sign buttons include screen-cleaning C, backward “←”, resulting-pushing =, addition +, subtraction -, multiplication X and division /.Note that = should cross lines.

⑤ Code Description

import tkinter as tk  #引用tkinter库以进行GUI编辑

window = tk.Tk()
window.title("Your calculator")
window.geometry("270x250+100+100")  #Create a 270x250+100+100, titled "Your calculator", which is defined as window in this program


window.attributes("-alpha", 0.85)
window["background"] = "pink"
#Set the background, color and transparency of the window

font1 = ("楷书", 17)
font2 = ("楷书", 15)
#Define two font formats that will be used later, a result line font font1 and a key font font2

res = tk.StringVar()
res.set("")
#Creates the variable res so that the final result row changes as the key is entered

label = tk.Label(window, textvariable=res, font=font1, height=2, bg="pink", width=22, justify=tk.LEFT, anchor=tk.SE).grid(row=1, column=1, columnspan=4)
#Initialize the result line, defined as label, its display value varies with res, font is font1, height is 2, width is 22
#And the background is the same as the window background; The results are displayed on the right; Placed in the first row, first column and across four columns

button_c = tk.Button(window, text="C", width=5, font=font2, relief=tk.FLAT, bg="white")
button_back = tk.Button(window, text="←", width=5, font=font2, relief=tk.FLAT, bg="white")
button_div = tk.Button(window, text="/", width=5, font=font2, relief=tk.FLAT, bg="white")
button_mul = tk.Button(window, text="X", width=5, font=font2, relief=tk.FLAT, bg="white")
button_c.grid(row=2, column=1, padx=3, pady=1)
button_back.grid(row=2, column=2, padx=3, pady=2)
button_div.grid(row=2, column=3, padx=3, pady=2)
button_mul.grid(row=2, column=4, padx=3, pady=2)
#Create the key in the first row of the calculator in the window
# From the left (row=2; column=1) To the right (row=2, column=4) are empty keys, delete keys, divide, and multiply

button_7=tk.Button(window,text="7", width=5,font=font2,relief=tk.FLAT,bg="orange")
button_8=tk.Button(window,text="8", width=5,font=font2,relief=tk.FLAT,bg="orange")
button_9=tk.Button(window,text="9", width=5,font=font2,relief=tk.FLAT,bg="orange")
button_min=tk.Button(window,text="-", width=5,font=font2,relief=tk.FLAT,bg="white")
button_7.grid(row=3,column=1,padx=3,pady=1)
button_8.grid(row=3,column=2,padx=3,pady=1)
button_9.grid(row=3,column=3,padx=3,pady=1)
button_min.grid(row=3,column=4,padx=3,pady=1)
#Create the keys in the second row of the calculator in the window, from the left (row=3; column=1) To the right (row=3, column=4) are the numbers 7, 8, 9, subtraction

button_4=tk.Button(window,text="4",width=5,font=font2,relief=tk.FLAT,bg="orange")
button_5=tk.Button(window,text="5",width=5,font=font2,relief=tk.FLAT,bg="orange")
button_6=tk.Button(window,text="6",width=5,font=font2,relief=tk.FLAT,bg="orange")
button_add=tk.Button(window,text="+",width=5,font=font2,relief=tk.FLAT,bg="white")
button_4.grid(row=4,column=1,padx=3,pady=1)
button_5.grid(row=4,column=2,padx=3,pady=1)
button_6.grid(row=4,column=3,padx=3,pady=1)
button_add.grid(row=4,column=4,padx=3,pady=1)
#Create the keys in the third row of the calculator in the window, from the left (row=4; column=1) To the right (row=4, column=4) are the numbers 4, 5, 6, and adding

button_1=tk.Button(window,text="1",width=5,font=font2,relief=tk.FLAT,bg="orange")
button_2=tk.Button(window,text="2",width=5,font=font2,relief=tk.FLAT,bg="orange")
button_3=tk.Button(window,text="3",width=5,font=font2,relief=tk.FLAT,bg="orange")
button_equ=tk.Button(window,text="=",height=3,width=5,font=font2,relief=tk.FLAT,bg="white")
button_1.grid(row=5,column=1,padx=3,pady=1)
button_2.grid(row=5,column=2,padx=3,pady=1)
button_3.grid(row=5,column=3,padx=3,pady=1)
button_equ.grid(row=5,column=4,padx=3,pady=1,rowspan=2)
#Create the keys in the fourth row of the calculator in the window, from the left (row=5; column=1) To the right (row=5, column=4) are the numbers 1, 2, 3, and the end operation (=), where the end operation key spans two rows

button_0=tk.Button(window,text="0",width=12,font=font2,relief=tk.FLAT,bg="orange")
button_dot=tk.Button(window,text=".",width=5,font=font2,relief=tk.FLAT,bg="orange")
button_0.grid(row=6,column=1,padx=3,pady=1,columnspan=2)
button_dot.grid(row=6,column=3,padx=3,pady=1)
#Create the keys in the fifth row of the calculator in the window, from the left (row=6; column=1) 
#To the right (row=6, column=3) is the number 0, the decimal point, where the number 0 spans two columns

def buttonclick(x):
    res.set(res.get()+x)
# Define a display function so that the number or operation entered in the result line can be displayed continuously

def calculation():
    opt_str = res.get()
    tmp = eval(opt_str)
    res.set(str(tmp))
# Define the evaluation function, after pressing the end operation key, the eval function will evaluate the previously entered data and display the result

def clear():
    res.set("")
# Define a clear function to clear the resulting row when the clear key is pressed

def back():
    a=res.get()
    res.set(a[0:-1])
# Define the Delete key, after pressing the delete key, remove the first number or symbol from right to left, and display the remaining characters


button_1.config(command=lambda: buttonclick("1"))
button_2.config(command=lambda: buttonclick("2"))
button_3.config(command=lambda: buttonclick("3"))
button_4.config(command=lambda: buttonclick("4"))
button_5.config(command=lambda: buttonclick("5"))
button_6.config(command=lambda: buttonclick("6"))
button_7.config(command=lambda: buttonclick("7"))
button_8.config(command=lambda: buttonclick("8"))
button_9.config(command=lambda: buttonclick("9"))
button_0.config(command=lambda: buttonclick("0"))
button_add.config(command=lambda: buttonclick("+"))
button_min.config(command=lambda: buttonclick("-"))
button_mul.config(command=lambda: buttonclick("*"))
button_div.config(command=lambda: buttonclick("/"))
button_dot.config(command=lambda: buttonclick("."))
button_equ.config(command=lambda: calculation())
button_c.config(command=lambda: clear())
button_back.config(command=lambda: back())
# Bind the corresponding key to the corresponding function function to determine the function achieved after pressing the key

window.mainloop()

I’ve written all the comments about the implementation directly into the code, so I think that should be clear enough

⑥ Specific demonstration

在这里插入图片描述

⑦ Summary and possible future improvements

⑦.1 Summary

In this project, some of the features are implemented that I didn’t know before, such as python’s tkinter library. I spent a lot of time searching for relevant information on the Internet (this part of time I calculated into coding). If I still want to use python to develop some small programs similar to calculators in the future, I should have a further understanding of such functional libraries with great practical significance as tkinter, and enhance the search ability to reduce the search time for unknown knowledge

⑦.2 Future improvements

1.Add trigonometric functions
2.Realize online, do not need to download to the local to run

In the last

Thanks for reading my first blog using markdown style, it is quite difficult to use at first. I am trying to adapt using it, so maybe there are some mistakes, hoping you can forget it. XD
I swear I will learn more about it later.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值