鱼C论坛

 找回密码
 立即注册
查看: 2211|回复: 7

[已解决]用Python做一个计算器

[复制链接]
发表于 2021-11-24 10:18:56 From FishC Mobile | 显示全部楼层 |阅读模式
15鱼币
代码最好简洁,初学者可以看懂的那种,除了有基本的加减乘除外,最好还能有其他功能
求大佬!!!!
最佳答案
2021-11-24 10:18:57

TkinterDesigner里本身就有这样的实例:

                               
登录/注册后可看大图

WIN64版:链接:https://pan.baidu.com/s/1B2lovLirlyP_Y0cbbgbL0A 提取码:cu9n
WIN32版:链接:https://pan.baidu.com/s/11Cs-lHMmTPPo-HdolvrlGA 提取码:1kk8

最佳答案

查看完整内容

TkinterDesigner里本身就有这样的实例: WIN64版:链接:https://pan.baidu.com/s/1B2lovLirlyP_Y0cbbgbL0A 提取码:cu9n WIN32版:链接:https://pan.baidu.com/s/11Cs-lHMmTPPo-HdolvrlGA 提取码:1kk8
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-24 10:18:57 | 显示全部楼层    本楼为最佳答案   

TkinterDesigner里本身就有这样的实例:

                               
登录/注册后可看大图

WIN64版:链接:https://pan.baidu.com/s/1B2lovLirlyP_Y0cbbgbL0A 提取码:cu9n
WIN32版:链接:https://pan.baidu.com/s/11Cs-lHMmTPPo-HdolvrlGA 提取码:1kk8

评分

参与人数 1鱼币 +5 收起 理由
蜜雪冰城 + 5

查看全部评分

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

使用道具 举报

发表于 2021-11-24 10:32:06 | 显示全部楼层
https://blog.csdn.net/weixin_42795087/article/details/89931944

  1. #  *_* coding:utf8 *_*
  2. import tkinter
  3. from functools import partial

  4. # 按钮输入调用
  5. def get_input(entry, argu):
  6.     # 从entry窗口展示中获取输入的内容
  7.     input_data = entry.get()

  8.     # 合法运算符 : + - * / -- ** // +-
  9.     # ------------ 输入合法性判断的优化 ------------
  10.     # 最后一个字符不是纯数字(已经有算数符号),原窗口值不为空,且输入值为运算符
  11.     # if not input_data[-1:].isdecimal() and (not argu.isdecimal()):
  12.     #     if input_data[-2:] in ["--", "**", "//", "+-"]:
  13.     #         return
  14.     #     if (input_data[-1:] + argu) not in ["--", "**", "//", "+-"]:
  15.     #         return
  16.     # ------------------------------------------------

  17.     # 出现连续+,则第二个+为无效输入,不做任何处理
  18.     if (input_data[-1:] == '+') and (argu == '+'):
  19.         return
  20.     # 出现连续+--,则第三个-为无效输入,不做任何处理
  21.     if (input_data[-2:] == '+-') and (argu == '-'):
  22.         return
  23.     # 窗口已经有--后面字符不能为+或-
  24.     if (input_data[-2:] == '--') and (argu in ['-', '+']):
  25.         return
  26.     # 窗口已经有 ** 后面字符不能为 * 或 /
  27.     if (input_data[-2:] == '**') and (argu in ['*', '/']):
  28.         return

  29.     # 输入合法将字符插入到entry窗口结尾
  30.     entry.insert("end", argu)

  31. # 退格(撤销输入)
  32. def backspace(entry):
  33.     input_len = len(entry.get())
  34.     # 删除entry窗口中最后的字符
  35.     entry.delete(input_len - 1)

  36. # 清空entry内容(清空窗口)
  37. def clear(entry):
  38.     entry.delete(0, "end")

  39. # 计算
  40. def calc(entry):
  41.     input_data = entry.get()
  42.     # 计算前判断输入内容是否为空;首字符不能为*/;*/不能连续出现3次;
  43.     if not input_data:
  44.         return

  45.     clear(entry)

  46.     # 异常捕获,在进行数据运算时如果出现异常进行相应处理
  47.     # noinspection PyBroadException
  48.     try:
  49.         # eval() 函数用来执行一个字符串表达式,并返回表达式的值;并将执行结果转换为字符串
  50.         output_data = str(eval(input_data))
  51.     except Exception:
  52.         # 将提示信息输出到窗口
  53.         entry.insert("end", "Calculation error")
  54.     else:
  55.         # 将计算结果显示在窗口中
  56.         if len(output_data) > 20:
  57.             entry.insert("end", "Value overflow")
  58.         else:
  59.             entry.insert("end", output_data)


  60. if __name__ == '__main__':

  61.     root = tkinter.Tk()
  62.     root.title("Calculator")

  63.     # 框体大小可调性,分别表示x,y方向的可变性;
  64.     root.resizable(0, 0)

  65.     button_bg = 'orange'
  66.     math_sign_bg = 'DarkTurquoise'
  67.     cal_output_bg = 'YellowGreen'
  68.     button_active_bg = 'gray'

  69.     # justify:显示多行文本的时候, 设置不同行之间的对齐方式,可选项包括LEFT, RIGHT, CENTER
  70.     # 文本从窗口左方开始显示,默认可以显示20个字符
  71.     # row:entry组件在网格中的横向位置
  72.     # column:entry组件在网格中的纵向位置
  73.     # columnspan:正常情况下,一个插件只占一个单元;可通过columnspan来合并一行中的多个相邻单元
  74.     entry = tkinter.Entry(root, justify="right", font=1)
  75.     entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

  76.     def place_button(text, func, func_params, bg=button_bg, **place_params):
  77.         # 偏函数partial,可以理解为定义了一个模板,后续的按钮在模板基础上进行修改或添加特性
  78.         # activebackground:按钮按下后显示颜place_params色
  79.         my_button = partial(tkinter.Button, root, bg=button_bg, padx=10, pady=3, activebackground=button_active_bg)
  80.         button = my_button(text=text, bg=bg, command=lambda: func(*func_params))
  81.         button.grid(**place_params)

  82.     # 文本输入类按钮
  83.     place_button('7', get_input, (entry, '7'), row=1, column=0, ipadx=5, pady=5)
  84.     place_button('8', get_input, (entry, '8'), row=1, column=1, ipadx=5, pady=5)
  85.     place_button('9', get_input, (entry, '9'), row=1, column=2, ipadx=5, pady=5)
  86.     place_button('4', get_input, (entry, '4'), row=2, column=0, ipadx=5, pady=5)
  87.     place_button('5', get_input, (entry, '5'), row=2, column=1, ipadx=5, pady=5)
  88.     place_button('6', get_input, (entry, '6'), row=2, column=2, ipadx=5, pady=5)
  89.     place_button('1', get_input, (entry, '1'), row=3, column=0, ipadx=5, pady=5)
  90.     place_button('2', get_input, (entry, '2'), row=3, column=1, ipadx=5, pady=5)
  91.     place_button('3', get_input, (entry, '3'), row=3, column=2, ipadx=5, pady=5)
  92.     place_button('0', get_input, (entry, '0'), row=4, column=0, padx=8, pady=5,
  93.                  columnspan=2, sticky=tkinter.E + tkinter.W + tkinter.N + tkinter.S)
  94.     place_button('.', get_input, (entry, '.'), row=4, column=2, ipadx=7, padx=5, pady=5)

  95.     # 运算输入类按钮(只是背景色不同)
  96.     # 字符大小('+','-'宽度不一样,使用ipadx进行修正)
  97.     place_button('+', get_input, (entry, '+'), bg=math_sign_bg, row=1, column=3, ipadx=5, pady=5)
  98.     place_button('-', get_input, (entry, '-'), bg=math_sign_bg, row=2, column=3, ipadx=5, pady=5)
  99.     place_button('*', get_input, (entry, '*'), bg=math_sign_bg, row=3, column=3, ipadx=5, pady=5)
  100.     place_button('/', get_input, (entry, '/'), bg=math_sign_bg, row=4, column=3, ipadx=5, pady=5)

  101.     # 功能输入类按钮(背景色、触发功能不同)
  102.     place_button('<-', backspace, (entry,), row=5, column=0, ipadx=5, padx=5, pady=5)
  103.     place_button('C', clear, (entry,), row=5, column=1, pady=5, ipadx=5)
  104.     place_button('=', calc, (entry,), bg=cal_output_bg, row=5, column=2, ipadx=5, padx=5, pady=5,
  105.                  columnspan=2, sticky=tkinter.E + tkinter.W + tkinter.N + tkinter.S)

  106.     root.mainloop()


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

使用道具 举报

发表于 2021-11-24 10:36:43 | 显示全部楼层
感谢大佬,太强了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-11-24 10:49:34 From FishC Mobile | 显示全部楼层
逃兵 发表于 2021-11-24 10:32
https://blog.csdn.net/weixin_42795087/article/details/89931944

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

使用道具 举报

发表于 2021-11-25 09:49:49 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-26 10:04:37 | 显示全部楼层
逃兵 发表于 2021-11-24 10:32
https://blog.csdn.net/weixin_42795087/article/details/89931944

为啥我代码运行了,啥都没显示?!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-12-5 14:52:25 From FishC Mobile | 显示全部楼层
adds52339 发表于 2021-11-26 10:04
为啥我代码运行了,啥都没显示?!!!

没有啊,我用IDLE运行很正常啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-30 22:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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