鱼C论坛

 找回密码
 立即注册
查看: 947|回复: 1

学习了一个半月python尝试写一计算器,代码太乱,请老师或师兄师姐们给一个更简洁....

[复制链接]
发表于 2020-4-15 13:18:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
from tkinter import *

str1 = ['+', '-', '*', '/']
num = []
sym = ''


root = Tk()
root.title('计算器')
root.geometry('290x350+550+170')
root.resizable(width = False, height = False)

def count(sym, num):
    num = num
    num[0] = int(num[0])
    num[1] = int(num[1])   
    if sym == '+':
        num[0] += num[1]
        del num[1]
    elif sym == '-':
        num[0] -= num[1]
        del num[1]
    elif sym == '*':
        num[0] *= num[1]
        del num[1]
    elif sym == '/':
        num[0] /= num[1]
        del num[1]
    else:
        pass

    return num[0]


def yi():
   
    E1.delete(0, END)
            
    E1.insert(INSERT, 1)
def er():
    E1.delete(0, END)
            
    E1.insert(INSERT, 2)

def san():
    E1.delete(0, END)
            
    E1.insert(INSERT, 3)

def si():
    E1.delete(0, END)
            
    E1.insert(INSERT, 4)

def wu():
    E1.delete(0, END)
            
    E1.insert(INSERT, 5)

def liu():
    if E1.get() == str(0):
        E1.delete(0, END)
            
    E1.insert(INSERT, 6)

def qi():
    E1.delete(0, END)
            
    E1.insert(INSERT, 7)

def ba():
    E1.delete(0, END)
            
    E1.insert(INSERT, 8)

def jiu():
    E1.delete(0, END)
            
    E1.insert(INSERT, 9)

def ling():
    E1.delete(0, END)
            
    E1.insert(INSERT, 0)

def jia():
   
        
    global sym #变为全局变量
   
   
    if len(num) == 0:
        sym = '+'
        num.append(E1.get())
        
        E2.insert(INSERT, (E1.get() + '+'))
        E1.delete(0, END)

   

    elif len(num) == 1:
        
        if sym in str1:
            
            num.append(E1.get())
            E2.insert(INSERT, (E1.get() + '%s' % sym))
            E1.delete(0, END)
            num[0] = count(sym, num)
            E1.insert(INSERT, num[0])
            sym = '+'
        else:
            sym = '+'
            num.append(E1.get())
            E2.insert(INSERT, (E1.get() + '%s' % sym))
            E1.delete(0, END)
            num[0] = count(sym, num)
            E1.insert(INSERT, num[0])

def jian():
    global sym
   
    if len(num) == 0:
        sym = '-'
        num.append(E1.get())
        
        E2.insert(INSERT, (E1.get() + '-'))
        E1.delete(0, END)
        

    elif len(num) == 1:
        
        if sym in str1:
            
            num.append(E1.get())
            E2.insert(INSERT, (E1.get() + '%s' % sym))
            E1.delete(0, END)
            num[0] = count(sym, num)
            E1.insert(INSERT, num[0])
            sym = '-'
        else:
            sym = '-'
            num.append(E1.get())
            E2.insert(INSERT, (E1.get() + '%s' % sym))
            E1.delete(0, END)
            num[0] = count(sym, num)
            E1.insert(INSERT, num[0])

def cheng():
    global sym
   
    if len(num) == 0:
        sym = '*'
        num.append(E1.get())
        
        E2.insert(INSERT, (E1.get() + '*'))
        E1.delete(0, END)
        

    elif len(num) == 1:
        
        if sym in str1:
            
            num.append(E1.get())
            E2.insert(INSERT, (E1.get() + '%s' % sym))
            E1.delete(0, END)
            num[0] = count(sym, num)
            E1.insert(INSERT, num[0])
            sym = '*'
        else:
            sym = '*'
            num.append(E1.get())
            E2.insert(INSERT, (E1.get() + '%s' % sym))
            E1.delete(0, END)
            num[0] = count(sym, num)
            E1.insert(INSERT, num[0])

def chu():
    global sym
   
    if len(num) == 0:
        sym = '/'
        num.append(E1.get())
        
        E2.insert(INSERT, (E1.get() + '/'))
        E1.delete(0, END)
        

    elif len(num) == 1:
        
        if sym in str1:
            
            num.append(E1.get())
            E2.insert(INSERT, (E1.get() + '%s' % sym))
            E1.delete(0, END)
            num[0] = count(sym, num)
            E1.insert(INSERT, num[0])
            sym = '/'
        else:
            sym = '/'
            num.append(E1.get())
            E2.insert(INSERT, (E1.get() + '%s' % sym))
            E1.delete(0, END)
            num[0] = count(sym, num)
            E1.insert(INSERT, num[0])
   

def deng():
    num.append(E1.get())
    E2.insert(INSERT, (E1.get() + '%s' % sym))
    E1.delete(0, END)
    num[0] = count(sym, num)
    E1.insert(INSERT, num[0])
def c0():
    n = len(E1.get())
    E1.delete(n - 1,INSERT)

def ce():
    if len(num):
        for i in num:
            num.remove(i)
   
        
   
    E1.delete(0,END)
    E1.insert(0, 0)
    E2.delete(0, END)
   
   

# 输入框
E1 = Entry(root,justify = RIGHT, font = ('黑体', 20))
E1.place(x = 10, y = 5, width = 270, height = 60)
E1.insert(0,0)


E2 = Entry(root,justify = RIGHT)
E2.place(x = 10, y = 5, width = 270)

Fa = Frame(root)
Fa.place(x = 5,y = 70, width = 290, height = 590)

# 按钮
B1 = Button(Fa, text = '1', command = yi)
B1.place(x = 5, y = 165, width = 50, height = 50)

B2 = Button(Fa, text = '2', command = er)
B2.place(x = 60, y = 165, width = 50, height = 50)

B3 = Button(Fa, text = '3',command = san)
B3.place(x = 115, y = 165, width = 50, height = 50)


B4 = Button(Fa, text = '4', command = si)
B4.place(x = 5, y = 110, width = 50, height = 50)

B5 = Button(Fa, text = '5', command = wu)
B5.place(x = 60, y = 110, width = 50, height = 50)

B6 = Button(Fa, text = '6', command = liu)
B6.place(x = 115, y = 110, width = 50, height = 50)

B7 = Button(Fa, text = '7', command = qi)
B7.place(x = 5, y = 55, width = 50, height = 50)

B8 = Button(Fa, text = '8', command = ba)
B8.place(x = 60, y = 55, width = 50, height = 50)

B9 = Button(Fa, text = '9', command = jiu)
B9.place(x = 115, y = 55, width = 50, height = 50)

B0 = Button(Fa, text = '0', command = ling)
B0.place(x = 5, y = 220, width = 100, height = 50)

# 加
Ba = Button(Fa, text = '+', command = jia)
Ba.place(x = 170, y = 220, width = 50, height = 50)

# 减
Bb = Button(Fa, text = '-', command = jian)
Bb.place(x = 170, y = 165, width = 50, height = 50)

# 乘
Bc = Button(Fa, text = '*', command = cheng)
Bc.place(x = 170, y = 110, width = 50, height = 50)

# 除
Bd = Button(Fa, text = '/', command = chu)
Bd.place(x = 170, y = 55, width = 50, height = 50)

# 等于
Be = Button(Fa, text = '=', command = deng)
Be.place(x = 225, y = 165, width = 50, height = 105)

# 点
Bdian = Button(Fa, text = '.')
Bdian.place(x = 115, y = 220, width = 50, height = 50)

# C
BC = Button(Fa, text = 'C', command = c0)
BC.place(x = 115, y = 0, width = 50, height = 50)

# CE
BCE = Button(Fa, text = 'CE', command = ce)
BCE.place(x = 60, y = 0, width = 50, height = 50)

# 左箭头
Bzuo = Button(Fa, text = '←')
Bzuo.place(x = 5, y = 0, width = 50, height = 50)

# + -
Bzheng = Button(Fa, text = '±')
Bzheng.place(x = 170, y = 0, width = 50, height = 50)

# 平方根
Bp = Button(Fa, text = '√')
Bp.place(x = 225, y = 0, width = 50, height = 50)

# 百分号

Bbfh = Button(Fa, text = '%')
Bbfh.place(x = 225, y = 55, width = 50, height = 50)

# 1/x
Bpai = Button(Fa, text = '1/x')
Bpai.place(x = 225, y = 110, width = 50, height = 50)

mainloop()

只能按顺序计算,不能先乘除后加减,还有代码太乱,不整洁易懂,请老师们,师兄师姐们 批评指点,拜谢大家~~!!!!

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-15 13:20:24 | 显示全部楼层
一开乱点没事,以后继续加油
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-17 13:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表