pythongui做计算器_python GUI模拟实现计算器

python编写计算器,供大家参考,具体内容如下

(1)计算器界面如下:

(2)基本满足了计算器的所有需求,使用时不可键盘输入,只能鼠标点击左键才可执行。初始时显示0.0,每次输入的内容存于D:\num.txt(启动程序时自动创建)

(3)" AC " 记录清零返回初始 0.0;" delete " 删除上一个输入内容;" +/- " 将正数为负数,负数为正数

(4)对于不同的进制数值系统,小数的精准值不同。

因此计算机会出现 0.1+0.2=0.3000000000004 的现象

能对数据进行截断处理,可以解决问题,但精度丧失。

(此计算机没有进行截断处理)

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')) # 重新设置标签文本

root.after(500,num_work) # 每隔0.5s调用函数num_work自身获取结果

def num_math_int(num1,num2):#整数运算

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错误')

def num_math_float(num1,num2):#小数运算

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错误')

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):#按键对应的功能

if string.isnumeric():

with open("D:\\num.txt","a") as file:

file.write(string)

else:

#读取文件D:\\num.txt所有内容

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]

#将百分数小数化

#出现结果多0.0000000001

num1=decimal(num1)

num2=decimal(num2)

try: #判断两个数是整数还是小数

number=int(num1)

number=int(num2[1:])

num_math_int(num1,num2)#两个数进行整数运算

except:

num_math_float(num1,num2)#两个数进行小数运算

elif string=='.':

if lists[-1].count('.')==0:#判断结尾是否有小数点,没有写入否则报错

with open("D:\\num.txt","a") as file:

file.write(string)

else:

with open("D:\\num.txt","a") as file:

file.write('\n错误')

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错误')

else:

with open("D:\\num.txt","a") as file:

file.write('\n'+string)

def run():#计算器显示界面主体

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("计算器")

#x = root.winfo_screenwidth()

#获取当前屏幕的宽

#y = root.winfo_screenheight()

#获取当前屏幕的高

#print(((x-500)//2),((y-600)//2))#为居中提供的参数

root.geometry('400x500+760+290')#主体长400,高500,居中

top=tkinter.Frame(root,width=20,height=50)

top.pack()

global top_work#定义全局变量root

temp(top)#空白间隔

#计算器显示框

top_work=tkinter.Label(top,text='',justify='left',relief=SUNKEN,bd=10,bg='white',width=40)

top_work.pack(side='bottom')#计算器显示框(位置居下)

num_work()

temp(root)#空白间隔

number=tkinter.Frame(root)#成放计算机键盘的容器

number.pack()

#所有按键,AC键为事例

numberAC=tkinter.Button(number,text="AC",width=10,command=lambda : work('AC')).grid(row=0,column=0)

#左键点击,执行函数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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Python 的 Tkinter 模块来编写 GUI 计算器应用程序。以下是一个简单的例子: ```python from tkinter import * # 创建主窗口 root = Tk() root.title("计算器") # 创建显示屏 display = Entry(root, width=35, borderwidth=5) display.grid(row=0, column=0, columnspan=4, padx=10, pady=10) # 定义按钮点击事件 def button_click(number): current = display.get() display.delete(0, END) display.insert(0, str(current) + str(number)) def button_clear(): display.delete(0, END) def button_add(): first_number = display.get() global f_num global math math = "addition" f_num = int(first_number) display.delete(0, END) def button_subtract(): first_number = display.get() global f_num global math math = "subtraction" f_num = int(first_number) display.delete(0, END) def button_multiply(): first_number = display.get() global f_num global math math = "multiplication" f_num = int(first_number) display.delete(0, END) def button_divide(): first_number = display.get() global f_num global math math = "division" f_num = int(first_number) display.delete(0, END) def button_equal(): second_number = display.get() display.delete(0, END) if math == "addition": display.insert(0, f_num + int(second_number)) elif math == "subtraction": display.insert(0, f_num - int(second_number)) elif math == "multiplication": display.insert(0, f_num * int(second_number)) elif math == "division": display.insert(0, f_num / int(second_number)) # 创建按钮 button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1)) button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2)) button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3)) button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4)) button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5)) button_6 = Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6)) button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7)) button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8)) button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9)) button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0)) button_add = Button(root, text="+", padx=39, pady=20, command=button_add) button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract) button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply) button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide) button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal) button_clear = Button(root, text="Clear", padx=79, pady=20, command=button_clear) # 将按钮放置到主窗口中 button_1.grid(row=3, column=0) button_2.grid(row=3, column=1) button_3.grid(row=3, column=2) button_4.grid(row=2, column=0) button_5.grid(row=2, column=1) button_6.grid(row=2, column=2) button_7.grid(row=1, column=0) button_8.grid(row=1, column=1) button_9.grid(row=1, column=2) button_0.grid(row=4, column=0) button_clear.grid(row=4, column=1, columnspan=2) button_add.grid(row=5, column=0) button_subtract.grid(row=6, column=0) button_multiply.grid(row=6, column=1) button_divide.grid(row=6, column=2) button_equal.grid(row=5, column=1, columnspan=2) # 运行主窗口 root.mainloop() ``` 运行程序后,会出现一个简单的 GUI 计算器应用程序,可以进行加、减、乘、除等基本算术运算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值