猫史档案馆


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

用户:一棵小桦树一棵小桦树查看:0 回复:5 评论:0 创建时间:2022-05-14T18:11:20


【作品展示】

center_image

 

【作品介绍】

小桦树制作的初版计算器,很多功能都没有。。。

 

【作品源代码】

import tkinter as tk
import math
window = tk.Tk()
window.title("tk")
window.geometry('120x230')
var = tk.StringVar()
a = ''
flag = 1
var.set(a)
flag_2 = 0

def backspace():
    global a
    a = a[0:len(a) - 1]
    var.set(a)


def ac():
    global a, flag_2
    a = ''
    var.set(a)
    flag = 1
    flag_2 = 0

def c1():
    global a, flag_2
    a = a + '1'
    var.set(a)
    flag_2 = 1


def c2():
    global a, flag_2
    a = a + '2'
    var.set(a)
    flag_2 = 1


def c3():
    global a, flag_2
    a = a + '3'
    var.set(a)
    flag_2 = 1

def c4():
    global a, flag_2
    a = a + '4'
    var.set(a)
    flag_2 = 1


def c5():
    global a, flag_2
    a = a + '5'
    var.set(a)
    flag_2 = 1


def c6():
    global a, flag_2
    a = a + '6'
    var.set(a)
    flag_2 = 1


def c7():
    global a, flag_2
    a = a + '7'
    var.set(a)
    flag_2 = 1


def c8():
    global a, flag_2
    a = a + '8'
    var.set(a)
    flag_2 = 1


def c9():
    global a, flag_2
    a = a + '9'
    var.set(a)
    flag_2 = 1


def c0():
    global a, flag_2
    a = a + '0'
    var.set(a)
    flag_2 = 1


def c_point():
    global flag
    if flag:
        global a
        a = a + '.'
        var.set(a)
        flag = 0
def c_add():
    global flag, a,flag_2
    if flag_2:
        a = a + '+'
        var.set(a)
        flag = 1
        flag_2 = 0


def c_sqrt():
    global flag, a, var_result, flag_2
    if flag_2:
        var_result = str(math.sqrt(eval(a)))
        a = var_result
        var.set(a)
def c_jian():
    global flag, a, flag_2
    if flag_2:
        a = a + '-'
        var.set(a)
        flag = 1
        flag_2 = 0


def c_cheng():
    global flag, a, flag_2
    if flag_2:
        a = a + '*'
        var.set(a)
        flag = 1
        flag_2 = 0


def c_chu():
    global flag, a, flag_2
    if flag_2:
        a = a + '/'
        var.set(a)
        flag = 1
        flag_2 = 0


def c_pingfang():
    global flag, a, flag_2
    if flag_2:
        a = a + ' ** '
        var.set(a)
        flag = 1
        flag_2 = 0


def c_deng():
    global flag, a,flag_2
    if flag_2:
        var_result = str(eval(a))
        flag = 1
        a =var_result
        var.set(a)

def cul():
    l = tk.Label(window, textvariable=var)
    l.place(x=0, y=0, width=120, height=20)
    button1 = tk.Button(window, text='1', command=c1)
    button1.place(x=0, y=50, width=30, height=30)
    button2 = tk.Button(window, text='2', command=c2)
    button2.place(x=30, y=50, width=30, height=30)
    button3 = tk.Button(window, text='3', command=c3)
    button3.place(x=60, y=50, width=30, height=30)
    button4 = tk.Button(window, text='4', command=c4)
    button4.place(x=0, y=80, width=30, height=30)
    button5 = tk.Button(window, text='5', command=c5)
    button5.place(x=30, y=80, width=30, height=30)
    button6 = tk.Button(window, text='6', command=c6)
    button6.place(x=60, y=80, width=30, height=30)
    button7 = tk.Button(window, text='7', command=c7)
    button7.place(x=0, y=110, width=30, height=30)
    button8 = tk.Button(window, text='8', command=c8)
    button8.place(x=30, y=110, width=30, height=30)
    button9 = tk.Button(window, text='9', command=c9)
    button9.place(x=60, y=110, width=30, height=30)
    button0 = tk.Button(window, text='0', command=c0)
    button0.place(x=0, y=140, width=60, height=30)
    button_point = tk.Button(window, text='.', command=c_point)
    button_point.place(x=60, y=140, width=30, height=30)
    button_add = tk.Button(window, text='+', command=c_add)
    button_add.place(x=90, y=50, width=30, height=30)
    button_jian = tk.Button(window, text='-', command=c_jian)
    button_jian.place(x=90, y=80, width=30, height=30)
    button_cheng = tk.Button(window, text='*', command=c_cheng)
    button_cheng.place(x=90, y=110, width=30, height=30)
    button_chu = tk.Button(window, text='/', command=c_chu)
    button_chu.place(x=90, y=140, width=30, height=30)
    button_ac = tk.Button(window, text='AC', command=ac)
    button_ac.place(x=0, y=20, width=30, height=30)
    button_deng = tk.Button(window, text='=', command=c_deng)
    button_deng.place(x=0, y=170, width=120, height=60)
    button_backspace = tk.Button(window, text='←', command=backspace)
    button_backspace.place(x=30, y=20, width=30, height=30)
    button_sqrt = tk.Button(window, text='开方', command=c_sqrt)
    button_sqrt.place(x=60, y=20, width=30, height=30)
    button_fang = tk.Button(window, text='^', command=c_pingfang)
    button_fang.place(x=90, y=20, width=30, height=30)
    window.mainloop()
cul()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
一棵小桦树一棵小桦树

我该怎么做才能让我的代码不那么长()

点赞0


评论


一棵小桦树一棵小桦树

顶一下

点赞0


评论


yh12i31yh12i31

变量命名尽量用英文吧~

点赞0


评论


vtvtvtvtvvtvtvtvtv

这个tkinter

库是干嘛用的?

点赞0


评论


一棵小桦树一棵小桦树

啊这

TK是用来编写界面的

点赞0


评论