python GUI模擬實(shí)現(xiàn)計(jì)算器
python編寫(xiě)計(jì)算器,供大家參考,具體內(nèi)容如下
(1)計(jì)算器界面如下:

(2)基本滿(mǎn)足了計(jì)算器的所有需求,使用時(shí)不可鍵盤(pán)輸入,只能鼠標(biāo)點(diǎn)擊左鍵才可執(zhí)行。初始時(shí)顯示0.0,每次輸入的內(nèi)容存于D:\num.txt(啟動(dòng)程序時(shí)自動(dòng)創(chuàng)建)
(3)" AC " 記錄清零返回初始 0.0;" delete " 刪除上一個(gè)輸入內(nèi)容;" +/- " 將正數(shù)為負(fù)數(shù),負(fù)數(shù)為正數(shù)
(4)對(duì)于不同的進(jìn)制數(shù)值系統(tǒng),小數(shù)的精準(zhǔn)值不同。
因此計(jì)算機(jī)會(huì)出現(xiàn) 0.1+0.2=0.3000000000004 的現(xiàn)象
能對(duì)數(shù)據(jù)進(jìn)行截?cái)嗵幚?,可以解決問(wèn)題,但精度喪失。
(此計(jì)算機(jī)沒(méi)有進(jìn)行截?cái)嗵幚恚?br />
import tkinter,os
from tkinter import *
def temp(string):#空白間隔
temp=tkinter.Frame(string,width=20,height=50)
temp.pack()
flag=0
node=0
def num_work(): #更新顯示框Lable
global flag
global node
with open("D:\\num.txt") as f:
for length in f:
string=length
top_work.configure(text=string.strip('\n')) # 重新設(shè)置標(biāo)簽文本
root.after(500,num_work) # 每隔0.5s調(diào)用函數(shù)num_work自身獲取結(jié)果
def num_math_int(num1,num2):#整數(shù)運(yùn)算
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:])
with open("D:\\num.txt",'a') as f:
f.write('\n'+str(string)+'\n')
except:
with open("D:\\num.txt",'a') as f:
f.write('\n錯(cuò)誤')
def num_math_float(num1,num2):#小數(shù)運(yùn)算
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:
with open("D:\\num.txt",'a') as f:
f.write('\n'+str(string)+'\n')
else:
with open("D:\\num.txt",'a') as f:
f.write('\n'+str(string))
except:
with open("D:\\num.txt",'a') as f:
f.write('\n錯(cuò)誤')
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):#按鍵對(duì)應(yīng)的功能
if string.isnumeric():
with open("D:\\num.txt","a") as file:
file.write(string)
else:
#讀取文件D:\\num.txt所有內(nèi)容
lists=[]
with open("D:\\num.txt","r") as file:
for length in file:
lists.append(length)
if string=='AC':
with open("D:\\num.txt","w") as file:
file.write('0.0\n')
elif string=='=':
num1=lists[-2]
num2=lists[-1]
if num1=='\n':#解決末尾為換行的情況
num1=lists[-3]
#將百分?jǐn)?shù)小數(shù)化
#出現(xiàn)結(jié)果多0.0000000001
num1=decimal(num1)
num2=decimal(num2)
try: #判斷兩個(gè)數(shù)是整數(shù)還是小數(shù)
number=int(num1)
number=int(num2[1:])
num_math_int(num1,num2)#兩個(gè)數(shù)進(jìn)行整數(shù)運(yùn)算
except:
num_math_float(num1,num2)#兩個(gè)數(shù)進(jìn)行小數(shù)運(yùn)算
elif string=='.':
if lists[-1].count('.')==0:#判斷結(jié)尾是否有小數(shù)點(diǎn),沒(méi)有寫(xiě)入否則報(bào)錯(cuò)
with open("D:\\num.txt","a") as file:
file.write(string)
else:
with open("D:\\num.txt","a") as file:
file.write('\n錯(cuò)誤')
elif string=='+/-':
if lists[-1].count('-')==0:#-+為-
if lists[-1].count('+')==1:
lists[-1]=lists[-1].replace('+','')
lists[-1]='-'+lists[-1]
else: #--為+
lists[-1]=lists[-1].replace('-','+')
#更新文件
with open("D:\\num.txt","w") as file:
pass
for length in lists:
with open("D:\\num.txt","a") as file:
file.write(length)
elif string=='delete':
number=lists[-1]
lists[-1]=number[0:(len(number)-1)]#刪除一位
#更新文件
with open("D:\\num.txt","w") as file:
pass
for length in lists:
with open("D:\\num.txt","a") as file:
file.write(length)
elif string=='%':
if lists[-1].endswith("%")==False:
with open("D:\\num.txt","a") as file:
file.write(string)
else:
with open("D:\\num.txt","a") as file:
file.write('\n錯(cuò)誤')
else:
with open("D:\\num.txt","a") as file:
file.write('\n'+string)
def run():#計(jì)算器顯示界面主體
if os.path.exists("D:\\num.txt")==False:
with open("D:\\num.txt",'w') as f:
f.write('0.0\n')
global root#定義全局變量root,方便Label更新
root=tkinter.Tk()
root.title("計(jì)算器")
#x = root.winfo_screenwidth()
#獲取當(dāng)前屏幕的寬
#y = root.winfo_screenheight()
#獲取當(dāng)前屏幕的高
#print(((x-500)//2),((y-600)//2))#為居中提供的參數(shù)
root.geometry('400x500+760+290')#主體長(zhǎng)400,高500,居中
top=tkinter.Frame(root,width=20,height=50)
top.pack()
global top_work#定義全局變量root
temp(top)#空白間隔
#計(jì)算器顯示框
top_work=tkinter.Label(top,text='',justify='left',relief=SUNKEN,bd=10,bg='white',width=40)
top_work.pack(side='bottom')#計(jì)算器顯示框(位置居下)
num_work()
temp(root)#空白間隔
number=tkinter.Frame(root)#成放計(jì)算機(jī)鍵盤(pán)的容器
number.pack()
#所有按鍵,AC鍵為事例
numberAC=tkinter.Button(number,text="AC",width=10,command=lambda : work('AC')).grid(row=0,column=0)
#左鍵點(diǎn)擊,執(zhí)行函數(shù)work
#按鍵位置(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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python數(shù)據(jù)庫(kù)格式化輸出文檔的思路與方法
這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)庫(kù)格式化輸出文檔的思路與方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python爬蟲(chóng)必備技巧詳細(xì)總結(jié)
本篇文章介紹了我在爬蟲(chóng)過(guò)程中總結(jié)的幾個(gè)必備技巧,都是經(jīng)過(guò)實(shí)驗(yàn)的,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-10-10
基于python實(shí)現(xiàn)FTP文件上傳與下載操作(ftp&sftp協(xié)議)
這篇文章主要介紹了基于python實(shí)現(xiàn)FTP文件上傳與下載操作(ftp&sftp協(xié)議),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python如何把十進(jìn)制數(shù)轉(zhuǎn)換成ip地址
這篇文章主要介紹了Python如何把十進(jìn)制數(shù)轉(zhuǎn)換成ip地址,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python對(duì)列表的操作知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理了關(guān)于Python對(duì)列表的操作知識(shí)點(diǎn)總結(jié)以及實(shí)例代碼運(yùn)用,需要的朋友們跟著學(xué)習(xí)下。2019-08-08
詳解如何在Python中有效調(diào)用JavaScript
JavaScript和Python都是極為流行的編程語(yǔ)言,并在前端開(kāi)發(fā)和后端開(kāi)發(fā)領(lǐng)域扮演著重要的角色,那么Python如何更好的契合JavaScript呢,下面就跟隨小編一起學(xué)習(xí)一下吧2024-02-02

