栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

如何用Tkinter做一个计算器

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何用Tkinter做一个计算器

from tkinter import *
'''
制作一个计算器,先打印3*3的数字按钮,再打印最右边的运算符号按钮,
最后打印底部的0和=,底部左侧是结果的文本框,用类记录每一个按钮代
表的含义。用一个全局变量,来检测是否是第一次输入数字,是的话存入
数字一,不是的话说明已经有数字一,则存入数字二,按=时输出数值的
同时,把结果存在数字一并且初始化数字二,以便于继续运算。
'''
# 计步器
cnt = 1
# 数量
quantity = 4
# 格子边长
wide = 1/quantity

# 创建一个窗口
root= Tk()
# 窗口名字
root.title('计算器')
# 窗口大小
root.geometry('300x300') # 这里的乘号不是 * ,而是小写英文字母 x

k=0

# 运算类
class operation():
    def main(self,value1,value2,operation_symbol):
        True
    def result(self):
        self.value1 = int(self.value1)
        self.value2 = int(self.value2)
        if self.operation_symbol =='+':
            return self.value1 + self.value2
        elif self.operation_symbol =='-':
            return self.value1 - self.value2
        elif self.operation_symbol =='*':
            return self.value1 * self.value2
        elif  self.operation_symbol =='÷':
            return self.value1 / self.value2

# 运算类定义 执行   
last_result = operation()
# 初始化为字符串
last_result.value1 = ''
last_result.value2 = ''


# 输入数字函数
def run2(cnt2):
    #声明全局变量k
    global k
    # 输入第一个数字
    if k==0:
        last_result.value1 = last_result.value1 + str(cnt2)
        lbl.config(text=str(last_result.value1))
    # 输入第二个数字
    if k!=0:
        last_result.value2 = last_result.value2 + str(cnt2)
        lbl.config(text=str(last_result.value2))

# 输入符号函数
def run3(cnt3):
        global k
        k+=1
        last_result.operation_symbol = cnt3
        
# 打印结果至文本框,并记录这一次结果便于直接继续计算
def run4():
        lbl.config(text=str(last_result.result()))
        # 把结果存回第一个数字
        last_result.value1 = last_result.result()
        # 初始化第二个数字
        last_result.value2 = ''
    
# 创建一个自己的数字按钮类    
class myButton():
    def __init__(self,root):
        # 用cnt存储按钮代表的数字
        self.cnt = 1
        self.j = 1
    def button(self):
        btn = Button(root, text='{}'.format(cnt),command = lambda:run2(self.cnt))
        btn.place(relx=j*wide, rely=i*wide, relwidth=wide, relheight=wide)

# 创建符号按钮类
class myButton2():
    def __init__(self,root):
        # 用j2存储符号位置
        self.j2 = 1
    def button(self):
        last_column = ['+','-','*','÷']
        btn = Button(root, text='{}'.format(last_column[self.j2]),command = lambda:run3(last_column[self.j2]))
        btn.place(relx=3*wide, rely=self.j2*wide, relwidth=wide, relheight=wide)


# 先创建一个九宫格
for i in range(quantity-1):
    for j in range(quantity-1):
        # 用自己的类创建数字按钮
        btn = myButton(root)
        btn.cnt = cnt
        btn.j = j
        btn.i = i
        btn.button()
        # 用计步器记录数值
        cnt += 1


# 创建最后一列的按钮
for j in range(quantity): 
    # 用自己的类创建符号按钮
    btn = myButton2(root)
    btn.j2 = j
    btn.button()

# 最后一行的符号
last_line = ['exit','0','=']

# 0按钮
btn_0 = Button(root, text='0',command = lambda:run2(self.value))
btn_0.place(relx=1*wide, rely=3*wide, relwidth=wide, relheight=wide)

# 等号按钮
btn_deng = Button(root, text='{}'.format(last_line[2]),command = lambda:run4())
btn_deng.place(relx=2*wide, rely=3*wide, relwidth=wide, relheight=wide)

# 结果显示文本框
lbl=Label(root,text='结果')
lbl.place(relx=0,rely=3*wide,relwidth=wide,relheight=wide)


# 运行
root.mainloop()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号