学习编程中的Ben 发表于 2022-12-29 06:57:43

tkinter计算器

本帖最后由 学习编程中的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()


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

tommyyu 发表于 2022-12-29 07:49:50

我的代码:import os
os.system('calc'){:10_256:}{:10_256:}{:10_256:}

liuhongrun2022 发表于 2022-12-29 07:54:59

tommyyu 发表于 2022-12-29 07:49
我的代码:

这个我真没想到
虽然我也用Windows自带的

hveagle 发表于 2022-12-29 08:10:33

from os import system
system('calc')
{:10_256:}

zhangjinxuan 发表于 2022-12-29 08:14:03

{:10_275:}

zhangjinxuan 发表于 2022-12-29 08:14:47

hveagle 发表于 2022-12-29 08:10


假设我用的是centos{:10_282:}

zsy0226 发表于 2022-12-29 08:18:40

import os
os.system('calc')

{:10_256:}{:10_256:}

学习编程中的Ben 发表于 2022-12-29 08:29:38

zhangjinxuan 发表于 2022-12-29 08:14


谢谢鼓励

多胺的烦恼 发表于 2022-12-29 08:42:51

{:5_106:}厉害!学习一下!

学习编程中的Ben 发表于 2022-12-29 08:43:47

多胺的烦恼 发表于 2022-12-29 08:42
厉害!学习一下!

一般啦

AhrimanSefid 发表于 2022-12-29 12:38:29

可以考虑用上TinUI模块{:10_256:}
圆角矩形按钮,绝对更配!!!

学习编程中的Ben 发表于 2022-12-29 12:49:01

AhrimanSefid 发表于 2022-12-29 12:38
可以考虑用上TinUI模块
圆角矩形按钮,绝对更配!!!

哈哈哈

AhrimanSefid 发表于 2022-12-29 13:19:50

学习编程中的Ben 发表于 2022-12-29 12:49
哈哈哈

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

学习编程中的Ben 发表于 2022-12-29 13:27:55

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

好的

liuhongrun2022 发表于 2023-7-24 19:22:19

币币币

binzai_007 发表于 2023-7-25 20:16:34

tommyyu 发表于 2022-12-29 07:49
我的代码:

这个牛X
页: [1]
查看完整版本: tkinter计算器