Python15行代码 tkinter模块 写计算机

之前去备战csp-j了 也有一段时间没更新了

结果白名单没捞着 还差点被我妈打喜

今天闲来无事 写个计算器玩玩

_____________________________________________________________________________

老规矩 先放代码

from tkinter import *;from math import sqrt;a=Tk();a.geometry('600x600');a.title('苦瓜牌计算器');b=Text(a, font=('微软雅黑',15));b.place(x=0,y=0,width=600,height=200);d=''
def bu(a1,a2,x,y):
    def co():global d;b.insert('end',a1);d += a2
    a3=Button(text=a1,font=('微软雅黑',50),command=co);a3.place(x=x,y=y,width=100,height=100)
bu('1','1',0,200);bu('4','4',0,300);bu('7','7',0,400);bu('0','0',100,500);bu('2','2',100,200);bu('3','3',200,200);bu('5','5',100,300);bu('6','6',200,300);bu('8','8',100,400);bu('9','9',200,400);bu('.','.',500,200);bu('+','+',300,200);bu('(','(',400,400);bu(')',')',400,500);bu('^','**',400,300);bu('-','-',300,300);bu('×','*',300,400);bu('÷','/',300,500);bu('√(', 'sqrt(',400,200)
def jc():global d;b.delete('1.0','end');d=''
def jl():global d;z=b.get('1.0','end-1c');d=d[:-1];b.delete('1.0','end');b.insert('end',z[:-1])
def jd():
    global d
    try:b.insert('end','='+str(eval(d)))
    except ZeroDivisionError:b.insert('end','\n"0"不能作除数')
    except SyntaxError:b.insert('end','\n表达式有误 无法计算')
    except ValueError:b.insert('end','\n数字太大 无法计算本式')
    b.insert('end','\n');d=''
jc0=Button(text='c',font=('微软雅黑',50),command=jc);jc0.place(x=200,y=500,width=100,height=100);jd0=Button(text='=',font=('微软雅黑',50),command=jd);jd0.place(x=0,y=500,width=100,height=100);jl0=Button(text='L',font=('微软雅黑',50),command=jl);jl0.place(x=500,y=300,width=100,hei

接下来是制作过程:

_____________________________________________________________________________

第一步:基础设置

from tkinter import *#导入tkinter模块
from math import sqrt#导入math模块但只使用sqrt函数
a=Tk()#建造窗口a
a.geometry('600x600')#把窗口a的长设为600 宽设为600
a.title('苦瓜牌计算器')#把窗口a的标题改为苦瓜牌计算器
b=Text(a,font=('微软雅黑',15))#在窗口a上建造文本框b 并且在文本框b里输入的字体用微软雅黑并且只有15像素大小
b.place(x=0,y=0,width=600,height=200)#放置文本框b 把位置设为0,0 把大小设为600*200
d=''#建立字符串d

_____________________________________________________________________________

第二步:按钮函数设置

def bu(a1,a2,x,y):
    def co():#定义函数co
        global d#把d改为全局变量
        b.insert('end',a1)#在文本框b里输入形参a1
        d+=a2#在字符串d的结尾加上形参a2
    a3=Button(text=a1,font=('微软雅黑',50),command=co)#创建按钮a3
    a3.place(x=x,y=y,width=100,height=100)#放置a3

_____________________________________________________________________________

第三步:按钮设置

bu('1','1',0,200)
bu('4','4',0,300)
bu('7','7',0,400)
bu('0','0',100,500)
bu('2','2',100,200)
bu('3','3',200,200)
bu('5','5',100,300)
bu('6','6',200,300)
bu('8','8',100,400)
bu('9','9',200,400)
bu('.','.',500,200)
bu('+','+',300,200)
bu('(','(',400,400)
bu(')',')',400,500)
bu('^','**',400,300)
bu('-','-',300,300)
bu('×','*',300,400)
bu('÷','/',300,500)
bu('√(','sqrt(',400,200)

_____________________________________________________________________________

第四步:特殊按钮设置

def jc():#定义函数jc
    global d#把d改为全局变量
    b.delete('1.0','end')#删除文本框b的所有内容
    d=''#重置字符串d
def jl():#定义函数jl
    global d#把d改为全局变量
    z=b.get('1.0','end-1c')#获得文本框b的所有内容
    d=d[:-1]#让字符串d只保留最后一位前的字符
    jc()
    b.insert('end',z[:-1])#让文本框b获得z的最后一位字符前所有的字符
def jd():
    global d#把d改为全局变量
    try:b.insert('end','='+str(eval(d)))
    except ZeroDivisionError:b.insert('end','\n"0"不能作除数')
    except SyntaxError:b.insert('end','\n表达式有误 无法计算')
    except ValueError:b.insert('end','\n数字太大 无法计算本式')
    b.insert('end','\n');d=''
jc0=Button(text='c',font=('微软雅黑',50),command=jc)
jc0.place(x=200,y=500,width=100,height=100)
jd0=Button(text='=',font=('微软雅黑',50),command=jd)
jd0.place(x=0,y=500,width=100,height=100)
jl0=Button(text='L',font=('微软雅黑',50),command=jl)
jl0.place(x=500,y=300,width=100,height=300)

_____________________________________________________________________________

MVP结算:

        本期字数:3457

        本期质量分:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值