鱼C论坛

 找回密码
 立即注册
查看: 2599|回复: 15

[作品展示] tkinter计算器

[复制链接]
发表于 2022-12-29 06:57:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 学习编程中的Ben 于 2023-7-24 19:22 编辑

萌新制作,不满勿喷

效果图在代码下方

代码:
from tkinter import *

root = Tk()
root.wm_minsize(400, 160)  # 设置窗口大小
ShowNumEntry = Entry(root, width=8, justify="right", font=12)
ShowNumEntry.grid(row=0, column=5)


def getInputValue(ShowNumEntry, Value):
    ShowNumEntry.insert(END, Value)


def GetResult():
    result = eval(ShowNumEntry.get())
    getClean()
    ShowNumEntry.insert(0, str(result))


def num_choice(num):
    if num == 1:
        getInputValue(ShowNumEntry,"1")
    elif num==2:
        getInputValue(ShowNumEntry, "2")
    elif num==3:
        getInputValue(ShowNumEntry, "3")
    elif num==4:
        getInputValue(ShowNumEntry, "4")
    elif num==5:
        getInputValue(ShowNumEntry, "5")
    elif num==6:
        getInputValue(ShowNumEntry, "6")
    elif num==7:
        getInputValue(ShowNumEntry, "7")
    elif num==8:
        getInputValue(ShowNumEntry, "8")
    elif num==9:
        getInputValue(ShowNumEntry, "9")
    elif num==0:
        getInputValue(ShowNumEntry, "0")


def operator_choice(oper):
    if oper == "+":
        getInputValue(ShowNumEntry,"+")
    elif oper=="-":
        getInputValue(ShowNumEntry, "-")
    elif oper=="*":
        getInputValue(ShowNumEntry, "*")
    elif oper=="/":
        getInputValue(ShowNumEntry, "/")
    elif oper == ".":
        getInputValue(ShowNumEntry, ".")

### 清空
def getClean():
    ShowNumEntry.delete(0,END)
# def GetOne():
#     GetInputValue(ShowNumEntry,"1")
#
#
# def GetTwo():
#     GetInputValue(ShowNumEntry,"2")
#
#
# def GetThree():
#     GetInputValue(ShowNumEntry,"3")
#
#
# def GetFour():
#     GetInputValue(ShowNumEntry,"4")
#
#
# def GetFive():
#     GetInputValue(ShowNumEntry,"5")
#
#
# def GetSix():
#     GetInputValue(ShowNumEntry,"6")
#
#
# def GetSeven():
#     GetInputValue(ShowNumEntry,"7")
#
#
# def GetEight():
#     GetInputValue(ShowNumEntry,"8")
#
#
# def GetNine():
#     GetInputValue(ShowNumEntry,"9")
#
#
# def GetZero():
#     GetInputValue(ShowNumEntry,"0")
#
#
# # 加减乘除
# def GetPlus():
#     GetInputValue(ShowNumEntry,"+")
#
#
# def GetMinus():
#     GetInputValue(ShowNumEntry,"-")
#
#
# def GetMultiply():
#     GetInputValue(ShowNumEntry,"*")
#
#
# def GetDivision():
#     GetInputValue(ShowNumEntry,"/")
#
#
# def GetDot():
#     GetInputValue(ShowNumEntry,".")

# def GetClean():
#     ShowNumEntry.delete(0, END)


# 行一
NumOneBtn = Button(root,text="1",width=8,height=2,command=lambda:num_choice(1) )
NumOneBtn.grid(row=1,column=0)
NumTwoBtn = Button(root,text="2",width=8,height=2,command=lambda:num_choice(2))
NumTwoBtn.grid(row=1,column=1)
NumThreeBtn = Button(root,text="3",width=8,height=2,command=lambda:num_choice(3))
NumThreeBtn.grid(row=1,column=2)
PlusBtn = Button(root,text="+",width=8,height=2,command=lambda:operator_choice("+"))
PlusBtn.grid(row=1,column=3)
MinusBtn = Button(root,text="-",width=8,height=2,command=lambda:operator_choice("-"))
MinusBtn.grid(row=1,column=4)
CleanBtn = Button(root,text="Clean",width=8,height=2,command=getClean)
CleanBtn.grid(row=1,column=5)

#第三行
NumFourBtn = Button(root,text="4",width=8,height=2,command=lambda:num_choice(4))
NumFourBtn.grid(row=2,column=0)
NumFiveBtn = Button(root,text="5",width=8,height=2,command=lambda:num_choice(5))
NumFiveBtn.grid(row=2,column=1)
NumSixBtn = Button(root,text="6",width=8,height=2,command=lambda:num_choice(6))
NumSixBtn.grid(row=2,column=2)
DotBtn = Button(root,text=".",width=8,height=2,command=lambda:operator_choice("."))
DotBtn.grid(row=2,column=3)
MultiplyBtn = Button(root,text="*",width=8,height=2,command=lambda:operator_choice("*"))
MultiplyBtn.grid(row=2,column=4)
ResultBtn = Button(root,text="=",width=8,height=2,background='green',command=GetResult)
ResultBtn.grid(row=2,column=5)

#第四行
NumSevenBtn = Button(root,text="7",width=8,height=2,command=lambda:num_choice(7))
NumSevenBtn.grid(row=3,column=0)
NumEightBtn = Button(root,text="8",width=8,height=2,command=lambda:num_choice(8))
NumEightBtn.grid(row=3,column=1)
NumNineBtn = Button(root,text="9",width=8,height=2,command=lambda:num_choice(9))
NumNineBtn.grid(row=3,column=2)
NumZeroBtn = Button(root,text="0",width=8,height=2,command=lambda:num_choice(0))
NumZeroBtn.grid(row=3,column=3)
DivisionBtn = Button(root,text="/",width=8,height=2,command=lambda:operator_choice("/"))
DivisionBtn.grid(row=3,column=4)

# # 行二
# NumFourBtn = Button(root, text="4", width=8, height=2)
# NumFourBtn.grid(row=2, column=0)
#
# NumFiveBtn = Button(root, text="5", width=8, height=2)
# NumFiveBtn.grid(row=2, column=1)
#
# NumSixBtn = Button(root, text="3", width=8, height=2)
# NumSixBtn.grid(row=2, column=2)
#
# DotBtn = Button(root, text=".", width=8, height=2)
# DotBtn.grid(row=2, column=3)
#
# MultiplyBtn = Button(root, text="X", width=8, height=2)
# MultiplyBtn.grid(row=2, column=4)
# EqualBtn = Button(root, text="=", width=8, height=2)
# EqualBtn.grid(row=2, column=5)
#
# # 行三
# NumSevenBtn = Button(root, text="7", width=8, height=2)
# NumSevenBtn.grid(row=3, column=0)
#
# NumEightBtn = Button(root, text="8", width=8, height=2)
# NumEightBtn.grid(row=3, column=1)
#
# NumNineBtn = Button(root, text="9", width=8, height=2)
# NumNineBtn.grid(row=3, column=2)
#
# ZeroBtn = Button(root, text="0", width=8, height=2)
# ZeroBtn.grid(row=3, column=3)
#
# DivBtn = Button(root, text="÷", width=8, height=2)
# DivBtn.grid(row=3, column=4)



root.mainloop()

各位鱼油若觉得满意就评分吧!!!

效果图

效果图

评分

参与人数 1荣誉 +1 鱼币 +2 收起 理由
元豪 + 1 + 2 没额度了

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-29 07:49:50 | 显示全部楼层
我的代码:
import os
os.system('calc')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 07:54:59 From FishC Mobile | 显示全部楼层
tommyyu 发表于 2022-12-29 07:49
我的代码:

这个我真没想到
虽然我也用Windows自带的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 08:10:33 | 显示全部楼层
from os import system
system('calc')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 08:14:03 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-29 08:14:47 | 显示全部楼层

假设我用的是centos
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 08:18:40 | 显示全部楼层
import os
os.system('calc')

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-29 08:29:38 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 08:42:51 | 显示全部楼层
厉害!学习一下!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-29 08:43:47 | 显示全部楼层

一般啦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-12-29 12:38:29 | 显示全部楼层
可以考虑用上TinUI模块
圆角矩形按钮,绝对更配!!!

评分

参与人数 1贡献 +3 收起 理由
asky533 + 3 无条件支持楼主!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-29 12:49:01 | 显示全部楼层
AhrimanSefid 发表于 2022-12-29 12:38
可以考虑用上TinUI模块
圆角矩形按钮,绝对更配!!!

哈哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 13:19:50 | 显示全部楼层

没开玩笑,我真的是建议
官方文档:https://tinui.smart-space.com.cn/
下载途径:pip install TinUI

评分

参与人数 1贡献 +3 收起 理由
asky533 + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-29 13:27:55 | 显示全部楼层
AhrimanSefid 发表于 2022-12-29 13:19
没开玩笑,我真的是建议
官方文档:https://tinui.smart-space.com.cn/
下载途径:pip insta ...

好的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-24 19:22:19 | 显示全部楼层

回帖奖励 +40 鱼币

币币币
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-25 20:16:34 | 显示全部楼层

这个牛X
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 09:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表