Python实现GUI计算器(附源码)

作者:川川菜鸟 时间:2022-06-07 03:32:47 

效果

可执行正常加减乘除相关运算,源码已贴,自行测试。

Python实现GUI计算器(附源码)

源码

# coding=gbk
"""
作者:川川
@时间  : 2022/11/6 3:10
"""
from tkinter import *

calc = Tk()
calc.title('川川计算器')
operator = ''

def clear():
   global operator
   operator=''
   txt_input.set("")
   display.insert(0,'开始计算...')

def button_press(number_or_operator):
   global operator
   operator = operator+str(number_or_operator)
   txt_input.set(operator)

def equal():
   global operator
   result = float(eval(operator))
   txt_input.set(result)
   operator=''

txt_input =StringVar(value='开始计算...')

#-----------------DISPLAY-----------------------------------
display = Entry(calc,font =('arial',30),fg='white',bg='green'
               ,justify='right',bd='50',textvariable=txt_input)
display.grid(columnspan=4)

#------------------row 1--------------------------------------

button7 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='7',command=lambda:button_press(7)).grid(row=1,column=0)
button8 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='8',command=lambda:button_press(8)).grid(row=1,column=1)
button9 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='9',command=lambda:button_press(9)).grid(row=1,column=2)
button_clear = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='green',
                font=('arial',30,'bold'),text='C',command=clear).grid(row=1,column=3)

#------------------row 2--------------------------------------

button4 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='4',command=lambda:button_press(4)).grid(row=2,column=0)
button5 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='5',command=lambda:button_press(5)).grid(row=2,column=1)
button6 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='6',command=lambda:button_press(6)).grid(row=2,column=2)
button_plus = Button(calc,padx =34,pady =12,bd=8,fg='black',bg='orange',
                font=('arial',30,'bold'),text='+',command=lambda:button_press('+')).grid(row=2,column=3)

#------------------row 3--------------------------------------

button1 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='1',command=lambda:button_press(1)).grid(row=3,column=0)
button2 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='2',command=lambda:button_press(2)).grid(row=3,column=1)
button3 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='3',command=lambda:button_press(3)).grid(row=3,column=2)
button_minus = Button(calc,padx =38,pady =12,bd=8,fg='black',bg='orange',
                font=('arial',30,'bold'),text='-',command=lambda:button_press('-')).grid(row=3,column=3)

#------------------row 4--------------------------------------

button_0 = Button(calc,padx =30,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='0',command=lambda:button_press(0)).grid(row=4,column=0)
button_dot = Button(calc,padx =36,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='.',command=lambda:button_press('.')).grid(row=4,column=1)
button_divide = Button(calc,padx =36,pady =12,bd=8,fg='black',bg='orange',
                font=('arial',30,'bold'),text='/',command=lambda:button_press('/')).grid(row=4,column=2)
button_mult = Button(calc,padx =38,pady =12,bd=8,fg='black',bg='orange',
                font=('arial',30,'bold'),text='*',command=lambda:button_press('*')).grid(row=4,column=3)

#------------------row 5--------------------------------------

button_equal = Button(calc,padx =95,pady =12,bd=8,fg='black',bg='green',
                font=('arial',30,'bold'),text='=',command=equal).grid(row=5,column=0,columnspan=2)
button_open = Button(calc,padx =35,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text='(',command=lambda:button_press('(')).grid(row=5,column=2)
button_close = Button(calc,padx =38,pady =12,bd=8,fg='black',bg='white',
                font=('arial',30,'bold'),text=')',command=lambda:button_press(')')).grid(row=5,column=3)

calc.mainloop()

来源:https://blog.csdn.net/weixin_46211269/article/details/127711872

标签:Python,GUI,计算器
0
投稿

猜你喜欢

  • Python作用域用法实例详解

    2022-12-10 15:44:42
  • python中gevent库的用法详情

    2022-10-17 11:14:43
  • 使用 createProcessingInstruction 方法不能输出 encoding 的解决方法

    2009-03-10 18:22:00
  • Python批量查询关键词微信指数实例方法

    2022-06-28 14:54:20
  • Python编程基础之构造方法和析构方法详解

    2022-02-26 02:38:03
  • pytorch+sklearn实现数据加载的流程

    2022-05-15 14:44:27
  • 大牌 Banner 设计欣赏(468x60)

    2008-01-20 13:02:00
  • python+pyqt实现右下角弹出框

    2023-09-07 16:04:22
  • Innodb中mysql快速删除2T的大表方法示例

    2024-01-22 09:14:21
  • MySQL慢查日志的开启方式与存储格式详析

    2024-01-12 14:21:47
  • python 实现按对象传值

    2023-05-26 14:20:18
  • Python可视化最频繁使用的10大工具总结

    2022-07-01 04:00:17
  • 在系统崩溃的时候如何恢复原有的数据

    2009-01-08 13:26:00
  • opencv中cv2.minAreaRect函数输出角度问题详解

    2023-09-08 06:38:47
  • 详解eslint在vue中如何使用

    2023-07-02 16:58:58
  • Oracle数据库集复制方法浅议

    2010-07-21 12:50:00
  • python中的生成器、迭代器、装饰器详解

    2022-03-01 20:36:46
  • Python闭包装饰器使用方法汇总

    2022-12-21 05:47:17
  • python 梯度法求解函数极值的实例

    2023-08-11 07:33:05
  • python知识:装饰器@property到底有啥用途

    2022-07-23 12:25:45
  • asp之家 网络编程 m.aspxhome.com