zoukankan      html  css  js  c++  java
  • 计算器

    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: um.txt") as f:
    for length in f:
    string=length
    top_work.configure(text=string.strip(' ')) # 重新设置标签文本
    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('
    '+str(string)+'
    ')
    

    except:
    with open("D: um.txt",'a') as f:
    f.write(' 错误')
    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: um.txt",'a') as f:
    f.write(' '+str(string)+' ')
    else:
    with open("D: um.txt",'a') as f:
    f.write(' '+str(string))
    except:
    with open("D: um.txt",'a') as f:
    f.write(' 错误')
    def decimal(num):
    if num.count('%')>0:
    num=num.replace('%','')
    num=num.replace(' ','')
    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: um.txt","a") as file:
    file.write(string)
    else:
    #读取文件D: um.txt所有内容
    lists=[]
    with open("D: um.txt","r") as file:
    for length in file:
    lists.append(length)

    if string=='清除':
      with open("D:\num.txt","w") as file:
        file.write('0.0
    ')
         
    elif string=='=':
      num1=lists[-2]
      num2=lists[-1]
      if num1=='
    ':#解决末尾为换行的情况
        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('
    错误')
           
    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=='删除':
      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('
    错误')
       
    else:
      with open("D:\num.txt","a") as file:
        file.write('
    '+string)
    

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

    if os.path.exists("D: um.txt")==False:
    with open("D: um.txt",'w') as f:
    f.write('0.0 ')

    global root#定义全局变量root,方便Label更新
    root=tkinter.Tk()
    root.title("ac计算器")

    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()

    所有按键,清除键为事例

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

    左键点击,执行函数work

    按键位置(0,0)

    numberdelete=tkinter.Button(number,text="删除",width=10,command=lambda : work('删除')).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()
    参考链接https://zhuanlan.zhihu.com/p/273420351
    他的思路是创建文本,用函数调用文本里的数值来实现计算器的功能,跟我想象的直接在.py文件里写好每一个按钮对应的运算符号直接运算的方法不一样= =

  • 相关阅读:
    JVM 常量池、运行时常量池、字符串常量池
    JVM Direct Memory
    JVM 方法区
    JVM GC Roots
    jvm 堆
    jvm slot复用
    JVM 虚拟机栈
    JVM 程序计数器
    java打印树形目录结构
    java 通过反射获取数组
  • 原文地址:https://www.cnblogs.com/acacacaac/p/14103160.html
Copyright © 2011-2022 走看看