鱼C论坛

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

[作品展示] tkinter计算器

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

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

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

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

萌新制作,不满勿喷

效果图在代码下方

代码:

  1. from tkinter import *

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


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


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


  12. def num_choice(num):
  13.     if num == 1:
  14.         getInputValue(ShowNumEntry,"1")
  15.     elif num==2:
  16.         getInputValue(ShowNumEntry, "2")
  17.     elif num==3:
  18.         getInputValue(ShowNumEntry, "3")
  19.     elif num==4:
  20.         getInputValue(ShowNumEntry, "4")
  21.     elif num==5:
  22.         getInputValue(ShowNumEntry, "5")
  23.     elif num==6:
  24.         getInputValue(ShowNumEntry, "6")
  25.     elif num==7:
  26.         getInputValue(ShowNumEntry, "7")
  27.     elif num==8:
  28.         getInputValue(ShowNumEntry, "8")
  29.     elif num==9:
  30.         getInputValue(ShowNumEntry, "9")
  31.     elif num==0:
  32.         getInputValue(ShowNumEntry, "0")


  33. def operator_choice(oper):
  34.     if oper == "+":
  35.         getInputValue(ShowNumEntry,"+")
  36.     elif oper=="-":
  37.         getInputValue(ShowNumEntry, "-")
  38.     elif oper=="*":
  39.         getInputValue(ShowNumEntry, "*")
  40.     elif oper=="/":
  41.         getInputValue(ShowNumEntry, "/")
  42.     elif oper == ".":
  43.         getInputValue(ShowNumEntry, ".")

  44. ### 清空
  45. def getClean():
  46.     ShowNumEntry.delete(0,END)
  47. # def GetOne():
  48. #     GetInputValue(ShowNumEntry,"1")
  49. #
  50. #
  51. # def GetTwo():
  52. #     GetInputValue(ShowNumEntry,"2")
  53. #
  54. #
  55. # def GetThree():
  56. #     GetInputValue(ShowNumEntry,"3")
  57. #
  58. #
  59. # def GetFour():
  60. #     GetInputValue(ShowNumEntry,"4")
  61. #
  62. #
  63. # def GetFive():
  64. #     GetInputValue(ShowNumEntry,"5")
  65. #
  66. #
  67. # def GetSix():
  68. #     GetInputValue(ShowNumEntry,"6")
  69. #
  70. #
  71. # def GetSeven():
  72. #     GetInputValue(ShowNumEntry,"7")
  73. #
  74. #
  75. # def GetEight():
  76. #     GetInputValue(ShowNumEntry,"8")
  77. #
  78. #
  79. # def GetNine():
  80. #     GetInputValue(ShowNumEntry,"9")
  81. #
  82. #
  83. # def GetZero():
  84. #     GetInputValue(ShowNumEntry,"0")
  85. #
  86. #
  87. # # 加减乘除
  88. # def GetPlus():
  89. #     GetInputValue(ShowNumEntry,"+")
  90. #
  91. #
  92. # def GetMinus():
  93. #     GetInputValue(ShowNumEntry,"-")
  94. #
  95. #
  96. # def GetMultiply():
  97. #     GetInputValue(ShowNumEntry,"*")
  98. #
  99. #
  100. # def GetDivision():
  101. #     GetInputValue(ShowNumEntry,"/")
  102. #
  103. #
  104. # def GetDot():
  105. #     GetInputValue(ShowNumEntry,".")

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


  108. # 行一
  109. NumOneBtn = Button(root,text="1",width=8,height=2,command=lambda:num_choice(1) )
  110. NumOneBtn.grid(row=1,column=0)
  111. NumTwoBtn = Button(root,text="2",width=8,height=2,command=lambda:num_choice(2))
  112. NumTwoBtn.grid(row=1,column=1)
  113. NumThreeBtn = Button(root,text="3",width=8,height=2,command=lambda:num_choice(3))
  114. NumThreeBtn.grid(row=1,column=2)
  115. PlusBtn = Button(root,text="+",width=8,height=2,command=lambda:operator_choice("+"))
  116. PlusBtn.grid(row=1,column=3)
  117. MinusBtn = Button(root,text="-",width=8,height=2,command=lambda:operator_choice("-"))
  118. MinusBtn.grid(row=1,column=4)
  119. CleanBtn = Button(root,text="Clean",width=8,height=2,command=getClean)
  120. CleanBtn.grid(row=1,column=5)

  121. #第三行
  122. NumFourBtn = Button(root,text="4",width=8,height=2,command=lambda:num_choice(4))
  123. NumFourBtn.grid(row=2,column=0)
  124. NumFiveBtn = Button(root,text="5",width=8,height=2,command=lambda:num_choice(5))
  125. NumFiveBtn.grid(row=2,column=1)
  126. NumSixBtn = Button(root,text="6",width=8,height=2,command=lambda:num_choice(6))
  127. NumSixBtn.grid(row=2,column=2)
  128. DotBtn = Button(root,text=".",width=8,height=2,command=lambda:operator_choice("."))
  129. DotBtn.grid(row=2,column=3)
  130. MultiplyBtn = Button(root,text="*",width=8,height=2,command=lambda:operator_choice("*"))
  131. MultiplyBtn.grid(row=2,column=4)
  132. ResultBtn = Button(root,text="=",width=8,height=2,background='green',command=GetResult)
  133. ResultBtn.grid(row=2,column=5)

  134. #第四行
  135. NumSevenBtn = Button(root,text="7",width=8,height=2,command=lambda:num_choice(7))
  136. NumSevenBtn.grid(row=3,column=0)
  137. NumEightBtn = Button(root,text="8",width=8,height=2,command=lambda:num_choice(8))
  138. NumEightBtn.grid(row=3,column=1)
  139. NumNineBtn = Button(root,text="9",width=8,height=2,command=lambda:num_choice(9))
  140. NumNineBtn.grid(row=3,column=2)
  141. NumZeroBtn = Button(root,text="0",width=8,height=2,command=lambda:num_choice(0))
  142. NumZeroBtn.grid(row=3,column=3)
  143. DivisionBtn = Button(root,text="/",width=8,height=2,command=lambda:operator_choice("/"))
  144. DivisionBtn.grid(row=3,column=4)

  145. # # 行二
  146. # NumFourBtn = Button(root, text="4", width=8, height=2)
  147. # NumFourBtn.grid(row=2, column=0)
  148. #
  149. # NumFiveBtn = Button(root, text="5", width=8, height=2)
  150. # NumFiveBtn.grid(row=2, column=1)
  151. #
  152. # NumSixBtn = Button(root, text="3", width=8, height=2)
  153. # NumSixBtn.grid(row=2, column=2)
  154. #
  155. # DotBtn = Button(root, text=".", width=8, height=2)
  156. # DotBtn.grid(row=2, column=3)
  157. #
  158. # MultiplyBtn = Button(root, text="X", width=8, height=2)
  159. # MultiplyBtn.grid(row=2, column=4)
  160. # EqualBtn = Button(root, text="=", width=8, height=2)
  161. # EqualBtn.grid(row=2, column=5)
  162. #
  163. # # 行三
  164. # NumSevenBtn = Button(root, text="7", width=8, height=2)
  165. # NumSevenBtn.grid(row=3, column=0)
  166. #
  167. # NumEightBtn = Button(root, text="8", width=8, height=2)
  168. # NumEightBtn.grid(row=3, column=1)
  169. #
  170. # NumNineBtn = Button(root, text="9", width=8, height=2)
  171. # NumNineBtn.grid(row=3, column=2)
  172. #
  173. # ZeroBtn = Button(root, text="0", width=8, height=2)
  174. # ZeroBtn.grid(row=3, column=3)
  175. #
  176. # DivBtn = Button(root, text="÷", width=8, height=2)
  177. # DivBtn.grid(row=3, column=4)



  178. root.mainloop()
复制代码


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

效果图

效果图

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-29 07:49:50 | 显示全部楼层
我的代码:
  1. import os
  2. os.system('calc')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

这个我真没想到
虽然我也用Windows自带的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 08:10:33 | 显示全部楼层
  1. from os import system
  2. system('calc')
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 08:14:03 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

假设我用的是centos
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-29 08:29:38 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 08:42:51 | 显示全部楼层
厉害!学习一下!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

一般啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

哈哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

好的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

回帖奖励 +40 鱼币

币币币
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

这个牛X
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 06:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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