Python GUI simulation implementation calculator

Python writes a calculator for your reference, the specific content is as follows

(1) The calculator interface is as follows:

(2) It basically meets all the needs of the calculator. Keyboard input is not allowed when in use, and can only be executed by clicking the left button of the mouse. 0.0 is displayed initially, and the content entered each time is stored in D:\num.txt (created automatically when the program is started)

(3) The "AC" record is cleared and returned to the initial 0.0; "delete" deletes the previous input; "+/-" turns a positive number into a negative number, and a negative number is a positive number

(4) For different hexadecimal value systems, the precise value of decimals is different.
Therefore, the computer will appear 0.1+0.2=0.3000000000004
The data can be truncated, which can solve the problem, but the accuracy is lost.
(This computer is not truncated)

import tkinter,os
from tkinter import*
def temp(string):#Blank interval
temp=tkinter.Frame(string,width=20,height=50)
temp.pack()
flag=0
node=0
def num_work():   #Update the display box Lable
global flag
global node
withopen("D:\\num.txt")as f:for length in f:
string=length
top_work.configure(text=string.strip('\n'))  #Reset label text
root.after(500,num_work) #Every 0.5s call function num_work itself gets results
def num_math_int(num1,num2):#Integer operations
try:if num2[0]=='+':
string=int(num1)+int(num2[1:])
elif num2[0]=='-':
string=int(num1)-int(num2[1:])
elif num2[0]=='x':
string=int(num1)*int(num2[1:])
elif num2[0]=='/':
string=int(num1)/int(num2[1:])withopen("D:\\num.txt",'a')as f:
f.write('\n'+str(string)+'\n')
except:withopen("D:\\num.txt",'a')as f:
f.write('\n error')
def num_math_float(num1,num2):#Decimal calculation
try:if num2[0]=='+':
string=float(num1)+float(num2[1:])
elif num2[0]=='-':
string=float(num1)-float(num2[1:])
elif num2[0]=='x':
string=float(num1)*float(num2[1:])
elif num2[0]=='/':
string=float(num1)/float(num2[1:])if flag==0:withopen("D:\\num.txt",'a')as f:
f.write('\n'+str(string)+'\n')else:withopen("D:\\num.txt",'a')as f:
f.write('\n'+str(string))
except:withopen("D:\\num.txt",'a')as f:
f.write('\n error')
def decimal(num):if num.count('%')0:
num=num.replace('%','')
num=num.replace('\n','')if num.isnumeric():
num=str(float(num)/100)else:
num=num[0]+str(float(num[1:])/100)return num
def work(string):#The corresponding function of the button
if string.isnumeric():withopen("D:\\num.txt","a")as file:
file.write(string)else:
# Read file D:\\num.txt all content
lists=[]withopen("D:\\num.txt","r")as file:for length in file:
lists.append(length)if string=='AC':withopen("D:\\num.txt","w")as file:
file.write('0.0\n')
elif string=='=':
num1=lists[-2]
num2=lists[-1]if num1=='\n':#Solve the situation where the end is a newline
num1=lists[-3]
# Decimalize percentages
# 0 more results.0000000001
num1=decimal(num1)
num2=decimal(num2)try:      #Determine whether two numbers are integers or decimals
number=int(num1)
number=int(num2[1:])num_math_int(num1,num2)#Integer operations on two numbers
except:num_math_float(num1,num2)#Two numbers for decimal operations
elif string=='.':if lists[-1].count('.')==0:#Determine whether there is a decimal point at the end, otherwise an error will be reported
withopen("D:\\num.txt","a")as file:
file.write(string)else:withopen("D:\\num.txt","a")as file:
file.write('\n error')
elif string=='+/-':if lists[-1].count('-')==0:#-+for-if lists[-1].count('+')==1:
lists[-1]=lists[-1].replace('+','')
lists[-1]='-'+lists[-1]else:           #--for+
lists[-1]=lists[-1].replace('-','+')
# Update file
withopen("D:\\num.txt","w")as file:
pass
for length in lists:withopen("D:\\num.txt","a")as file:
file.write(length)
elif string=='delete':
number=lists[-1]
lists[-1]=number[0:(len(number)-1)]#Delete one
# Update file
withopen("D:\\num.txt","w")as file:
pass
for length in lists:withopen("D:\\num.txt","a")as file:
file.write(length)
elif string=='%':if lists[-1].endswith("%")==False:withopen("D:\\num.txt","a")as file:
file.write(string)else:withopen("D:\\num.txt","a")as file:
file.write('\n error')else:withopen("D:\\num.txt","a")as file:
file.write('\n'+string)
def run():#Calculator display interface body
if os.path.exists("D:\\num.txt")==False:withopen("D:\\num.txt",'w')as f:
f.write('0.0\n')
global root#Define global variable root,Facilitate Label update
root=tkinter.Tk()
root.title("Calculator")
# x = root.winfo_screenwidth()
# Get the width of the current screen
# y = root.winfo_screenheight()
# Get the height of the current screen
# print(((x-500)//2),((y-600)//2))#Parameters provided for centering
root.geometry('400x500+760+290')#The main body is 400 long, 500 high, centered
top=tkinter.Frame(root,width=20,height=50)
top.pack()
global top_work#Define global variable root
temp(top)#Blank interval
# Calculator display box
top_work=tkinter.Label(top,text='',justify='left',relief=SUNKEN,bd=10,bg='white',width=40)
top_work.pack(side='bottom')#Calculator display frame (position at the bottom)
num_work()temp(root)#Blank interval
number=tkinter.Frame(root)#Into a container for the computer keyboard
number.pack()
# All keys, AC key as an example
numberAC=tkinter.Button(number,text="AC",width=10,command=lambda :work('AC')).grid(row=0,column=0)
# Left click to execute function work
# Button position (0,0)
numberdelete=tkinter.Button(number,text="delete",width=10,command=lambda :work('delete')).grid(row=0,column=1)
numberzhengfu=tkinter.Button(number,text="+/-",width=10,command=lambda :work('+/-')).grid(row=0,column=2)
numberchu=tkinter.Button(number,text="/",width=10,command=lambda :work('/')).grid(row=0,column=3)
tkinter.Button(number,text="7",width=10,command=lambda :work('7')).grid(row=1,column=0)
tkinter.Button(number,text="8",width=10,command=lambda :work('8')).grid(row=1,column=1)
tkinter.Button(number,text="9",width=10,command=lambda :work('9')).grid(row=1,column=2)
tkinter.Button(number,text="x",width=10,command=lambda :work('x')).grid(row=1,column=3)
tkinter.Button(number,text="4",width=10,command=lambda :work('4')).grid(row=2,column=0)
tkinter.Button(number,text="5",width=10,command=lambda :work('5')).grid(row=2,column=1)
tkinter.Button(number,text="6",width=10,command=lambda :work('6')).grid(row=2,column=2)
tkinter.Button(number,text="-",width=10,command=lambda :work('-')).grid(row=2,column=3)
tkinter.Button(number,text="1",width=10,command=lambda :work('1')).grid(row=3,column=0)
tkinter.Button(number,text="2",width=10,command=lambda :work('2')).grid(row=3,column=1)
tkinter.Button(number,text="3",width=10,command=lambda :work('3')).grid(row=3,column=2)
tkinter.Button(number,text="+",width=10,command=lambda :work('+')).grid(row=3,column=3)
tkinter.Button(number,text="%",width=10,command=lambda :work('%')).grid(row=4,column=0)
tkinter.Button(number,text="0",width=10,command=lambda :work('0')).grid(row=4,column=1)
tkinter.Button(number,text=".",width=10,command=lambda :work('.')).grid(row=4,column=2)
tkinter.Button(number,text="=",width=10,command=lambda :work('=')).grid(row=4,column=3)
root.mainloop()if __name__=='__main__':run()

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python GUI simulation implementation calculator
Write gui in python
Python drawing rose implementation code
Python implementation of gomoku program
Python telnet login function implementation code
Detailed implementation of Python plug-in mechanism
Implementation of reverse traversal of python list
Python implementation of IOU calculation case
Python preliminary implementation of word2vec operation
FM algorithm analysis and Python implementation
Implementation of python selenium operation cookie
Python ATM function implementation code example
Python simulation of the landlord deal
Scrapy simulation login of Python crawler
Implementation of python3 registration global hotkey
Implementation of python student management system
Implementation of python gradient descent algorithm