与哔站教程相对

该代码示例展示了如何利用Python的Tkinter库创建一个基本的图形用户界面(GUI),包含数字和运算符按钮,用于执行简单的加减乘除计算。用户点击按钮输入数字和运算符,程序会根据用户的操作进行相应的计算并显示结果。
摘要由CSDN通过智能技术生成


from tkinter import *
import os
win = Tk()
win.geometry("280x400")
win.resizable(0,0)
sstr1=" "
# flag=False
menubar =Menu(win)
filemenu=Menu(menubar)
win.configure(bg='#69FF69')
# menubar.add_cascade(label='数学', menu=filemenu)
# menubar.add_cascade(label='汇率', menu=filemenu)
#
# hui=Button(text="汇率",command=os.system("python 汇率计算器.py"))
# hui.place(x=100,y=250)
def one1():
    global sstr1
    sstr1=sstr1+"1"
    print(sstr1)

one=Button(width=5,height=5,text="1",command=one1,background="#69FF76", activeforeground='black', activebackground='#69FF76')
one.pack(side=LEFT)
def two2():
    global sstr1
    sstr1=sstr1+"2"
    print(sstr1)
two=Button(width=5,height=5,text="2",command=two2,background="#69FF76", activeforeground='black', activebackground='#69FF76')
two.pack(side=LEFT)
def three3():
    global sstr1
    sstr1=sstr1+"3"
    print(sstr1)
three=Button(text="3",width=5,height=5,command=three3,background="#69FF76", activeforeground='black', activebackground='#69FF76')
three.pack(side=LEFT)
def four4():
    global sstr1
    sstr1=sstr1+"4"
    print(sstr1)
four=Button(text="4",width=5,height=5,command=four4,background="#69FF76", activeforeground='black', activebackground='#69FF76')
four.pack(side=LEFT)
def five5():
    global sstr1
    sstr1=sstr1+"5"
    print(sstr1)
five=Button(text="5",width=5,height=5,command=five5,background="#69FF76", activeforeground='black', activebackground='#69FF76')
five.pack(side=LEFT)
def six6():
    global sstr1
    sstr1=sstr1+"6"
    print(sstr1)
six=Button(text="6",width=5,height=5,command=six6,background="#69FF76", activeforeground='black', activebackground='#69FF76')
six.pack(side=LEFT)
def seven7():
    global sstr1
    sstr1=sstr1+"7"
    print(sstr1)
seven=Button(text="7",width=5,height=5,command=seven7,background="#69FF76", activeforeground='black', activebackground='#69FF76')
seven.place(x=0,y=245)
def eight8():
    global sstr1
    sstr1=sstr1+"8"
    print(sstr1)
eight=Button(text="8",width=5,height=5,command=eight8,background="#69FF76", activeforeground='black', activebackground='#69FF76')
eight.place(x=45,y=245)
def nine9():
    global sstr1
    sstr1=sstr1+"9"
    print(sstr1)
nine=Button(text="9",width=5,height=5,command=nine9,background="#69FF76", activeforeground='black', activebackground='#69FF76')
nine.place(x=90,y=245)
def ten0():
    global sstr1
    sstr1=sstr1+"0"
    print(sstr1)
ten=Button(text="0",width=5,height=5,command=ten0,background="#69FF76", activeforeground='black', activebackground='#69FF76')
ten.place(x=135,y=245)
k=" "
def jia1():
    global sstr1,k
    sstr1=sstr1+"+"
    print(sstr1)
    k="+"
jia=Button(text="+",width=5,height=5,command=jia1,background="#69FF76", activeforeground='black', activebackground='#69FF76')
jia.place(x=180,y=245)
def jian1():
    global sstr1,k
    sstr1=sstr1+"-"
    k="-"
    print(sstr1)
jian=Button(text="-",width=5,height=5,command=jian1,background="#69FF76", activeforeground='black', activebackground='#69FF76')
jian.place(x=225,y=245)
def chu1():
    global sstr1,k
    sstr1=sstr1+"÷"
    k="÷"
    print(sstr1)
chu=Button(text="÷",width=5,height=5,command=chu1,background="#69FF76", activeforeground='black', activebackground='#69FF76')
chu.place(x=0,y=300)

def cheng1():
    global sstr1,k
    sstr1=sstr1+"×"
    k="×"
    print(sstr1)
cheng=Button(text="×",width=5,height=5,command=cheng1,background="#69FF76", activeforeground='black', activebackground='#69FF76')
cheng.place(x=45,y=300)
f=0
def bracket1():
    global sstr1,k,f
    if f==0:
        sstr1=sstr1+"("
        f=1
    elif f==1:
        sstr1 = sstr1 + ")"
        f=0
    print(sstr1)

bracket=Button(text="(",width=5,height=5,command=bracket1,background="#69FF76", activeforeground='black', activebackground='#69FF76')
bracket.place(x=45,y=300)
n=[0,1,2]
def jisuan():
    global sstr1,n,k
    n=sstr1.split("×")
    print(n)
    if len(n)==0:
        n=sstr1.split("÷")
    if len(n)==0:
        n=sstr1.split("+")
    if len(n)==0:
        n=sstr1.split("-")
    one2=int(n[0])
    two1=int(n[1])
    sum=0
    if k=="+":
        sum=one2+two1
        print(sum)
    elif k=="-":
        sum=one2-two1
        print(sum)
    elif k=="×":
        sum=one2*two1
        print(sum)
    elif k=="÷":
        sum=one2/two1
        print(sum)
    text=Label(text=one2+k+two1+str(sum))
    text.pack()
def C1():
    global sstr1
    sstr1=""
# text=Label(text=sstr1,command=jisuan)
# text.pack()
deng=Button(text="=",width=5,height=5,command=jisuan,background="#69FF76", activeforeground='black', activebackground='#69FF76')
deng.place(x=90,y=300)
C=Button(text="C",width=5,height=5,command=C1,background="#69FF76", activeforeground='black', activebackground='#69FF76')
C.place(x=135,y=300)
print(sstr1)
win.mainloop()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值