前言
本片文章是Python实现一个计算器的小程序
涉及到了tkinter 的一个使用和掌握
大家要不是很懂tkinter 的话可以看看下面这个视频!
Python超全tkinter案例讲解,基础教学视频!!
相关文件
想学Python的小伙伴可以关注小编的公众号【Python日志】
有很多的资源可以白嫖的哈,不定时会更新一下Python的小知识的哈!!
需要源码的小伙伴可以在公众号回复计算器
Python源码、问题解答学习交流群:773162165
开发环境
Python版本:3.6.7
相关模块:
math
tkinter
以及一些python自带的模块。
环境搭建
安装Python并添加到环境变量,pip安装需要的相关模块即可。
效果展示
代码实现
模块导入
import math
import tkinter
按钮功能实现
def Demo():
root.minsize(320, 420)
root.title('Calculator')
# 布局
# --文本框
label = tkinter.Label(root, textvariable=CurrentShow, bg='black', anchor='e', bd=5, fg='white', font=('楷体', 20))
label.place(x=20, y=50, width=280, height=50)
# --第一行
# ----Memory clear
button1_1 = tkinter.Button(text='MC', bg='#666', bd=2, command=lambda:pressOperator('MC'))
button1_1.place(x=20, y=110, width=50, height=35)
# ----Memory read
button1_2 = tkinter.Button(text='MR', bg='#666', bd=2, command=lambda:pressOperator('MR'))
button1_2.place(x=77.5, y=110, width=50, height=35)
# ----Memory save
button1_3 = tkinter.Button(text='MS', bg='#666', bd=2, command=lambda:pressOperator('MS'))
button1_3.place(x=135, y=110, width=50, height=35)
# ----Memory +
button1_4 = tkinter.Button(text='M+', bg='#666', bd=2, command=lambda:pressOperator('M+'))
button1_4.place(x=192.5, y=110, width=50, height=35)
# ----Memory -
button1_5 = tkinter.Button(text='M-', bg='#666', bd=2, command=lambda:pressOperator('M-'))
button1_5.place(x=250, y=110, width=50, height=35)
# --第二行
# ----删除单个数字
button2_1 = tkinter.Button(text='del', bg='#666', bd=2, command=lambda:delOne())
button2_1.place(x=20, y=155, width=50, height=35)
# ----清除当前显示框内所有数字
button2_2 = tkinter.Button(text='CE', bg='#666', bd=2, command=lambda:clearCur