猫史档案馆


【Python作品分享】计算机【作品秀】

用户:砹整钬的屑砹整钬的屑查看:0 回复:0 评论:0 创建时间:2022-12-19T11:01:19


【作品展示】

center_image

 

【作品介绍】

哈哈哈哈哈哈哈哈哈哈哈哈哈哈

 

【作品源代码】

from tkinter import *


num1 = 0
num2 = 0
s = ''

temp = 0


def cal():
    root = Tk()
    root.title('计算器')
    root.geometry('340x450+600+100')
    root.resizable(0, 0)

    def fC():
        global temp, num1, num2, s
        temp = 0
        num1 = 0
        num2 = 0
        s = ''
        print(temp)
        t.set(temp)

    def f0():
        global temp
        temp = temp*10+0
        print(temp)
        t.set(temp)

    def f1():
        global temp
        temp = temp*10+1
        print(temp)
        t.set(temp)

    def f2():
        global temp
        temp = temp*10+2
        print(temp)
        t.set(temp)

    def f3():
        global temp
        temp = temp*10+3
        print(temp)
        t.set(temp)

    def f4():
        global temp
        temp = temp*10+4
        print(temp)
        t.set(temp)

    def f5():
        global temp
        temp = temp*10+5
        print(temp)
        t.set(temp)

    def f6():
        global temp
        temp = temp*10+6
        print(temp)
        t.set(temp)

    def f7():
        global temp
        temp = temp*10+7
        print(temp)
        t.set(temp)

    def f8():
        global temp
        temp = temp*10+8
        print(temp)
        t.set(temp)

    def f9():
        global temp
        temp = temp*10+9
        print(temp)
        t.set(temp)

    def plus():
        global num1, temp, s
        num1 = temp
        temp = 0
        s = '+'
        t.set('+')

    def dec():
        global num1, temp, s
        num1 = temp
        temp = 0
        s = '-'
        t.set('-')

    def mul():
        global num1, temp, s
        num1 = temp
        temp = 0
        s = '×'
        t.set('×')

    def div():
        global num1, temp, s
        num1 = temp
        temp = 0
        s = '÷'
        t.set('÷')

    def equ():
        global num1, temp, s, num2
        if len(str(temp)) >= 20:
            temp = 0
            num1 = 0
            num2 = 0
            s = ''
            t.set('错误 ERROR!')
        else:
            num2 = temp
            temp = 0
            if s == '÷':
                try:
                    num1 / num2
                except ZeroDivisionError:
                    t.set('错误 ERROR!')
                    temp = 0
                    num1 = 0
                    num2 = 0
                    s = ''
                else:
                    t.set(num1/num2)
                    if len(str(num1/num2)) >= 20:
                        t.set('错误 ERROR!')
                    temp = 0
                    num1 = 0
                    num2 = 0
                    s = ''
            elif s == '×':
                t.set(num1*num2)
                if len(str(num1*num2)) >= 20:
                    t.set('错误 ERROR!')
                temp = 0
                num1 = 0
                num2 = 0
                s = ''
            elif s == '+':
                t.set(num1+num2)
                if len(str(num1+num2)) >= 20:
                    t.set('错误 ERROR!')
                temp = 0
                num1 = 0
                num2 = 0
                s = ''
            elif s == '-':
                t.set(num1-num2)
                if len(str(num1-num2)) >= 20:
                    t.set('错误 ERROR!')
                temp = 0
                num1 = 0
                num2 = 0
                s = ''
            else:
                t.set('错误 ERROR!')

    c1 = Canvas(root, width=400, height=450, background='deepskyblue')
    c1.pack()

    n7_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='7', command=f7)
    n7_btn.pack()
    c1.create_window(50, 150, window=n7_btn)

    n8_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='8', command=f8)
    n8_btn.pack()
    c1.create_window(130, 150, window=n8_btn)

    n9_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='9', command=f9)
    n9_btn.pack()
    c1.create_window(210, 150, window=n9_btn)

    nch_btn = Button(root, width=7, height=3, font=('楷体', 15),
                     text='÷', command=div)
    nch_btn.pack()
    c1.create_window(290, 150, window=nch_btn)

    n4_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='4', command=f4)
    n4_btn.pack()
    c1.create_window(50, 225, window=n4_btn)

    n5_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='5', command=f5)
    n5_btn.pack()
    c1.create_window(130, 225, window=n5_btn)

    n6_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='6', command=f6)
    n6_btn.pack()
    c1.create_window(210, 225, window=n6_btn)

    nchen_btn = Button(root, width=7, height=3, font=(
        '楷体', 15), text='×', command=mul)
    nchen_btn.pack()
    c1.create_window(290, 225, window=nchen_btn)

    n1_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='1', command=f1)
    n1_btn.pack()
    c1.create_window(50, 300, window=n1_btn)

    n2_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='2', command=f2)
    n2_btn.pack()
    c1.create_window(130, 300, window=n2_btn)

    n3_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='3', command=f3)
    n3_btn.pack()
    c1.create_window(210, 300, window=n3_btn)

    njian_btn = Button(root, width=7, height=3, font=(
        '楷体', 15), text='-', command=dec)
    njian_btn.pack()
    c1.create_window(290, 300, window=njian_btn)

    c_btn = Button(root, width=10, height=5, font=('楷体', 10),
                   text='C/eliminate', command=fC)
    c_btn.pack()
    c1.create_window(50, 375, window=c_btn)

    n0_btn = Button(root, width=7, height=3, font=('楷体', 15),
                    text='0', command=f0)
    n0_btn.pack()
    c1.create_window(130, 375, window=n0_btn)

    equ_btn = Button(root, width=7, height=3, font=('楷体', 15),
                     text='=', command=equ)
    equ_btn.pack()
    c1.create_window(210, 375, window=equ_btn)

    jia_btn = Button(root, width=7, height=3, font=('楷体', 15),
                     text='+', command=plus)
    jia_btn.pack()
    c1.create_window(290, 375, window=jia_btn)

    c1.create_rectangle((20, 20, 315, 100), fill='Blue')

    t = StringVar()
    num_label = Label(root, textvariable=t, font=('楷体', 20), background='Blue')
    num_label.pack()

    c1.create_window(175, 60, window=num_label)
    t.set(temp)
    root.mainloop()


if __name__ == '__main__':
    cal()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页