相关学习视频:Python Tkinter 绘图项目 - 网易云课堂​study.163.com

import tkinter

import math

import tkinter.messagebox

class calculator:

#界面布局方法

def __init__(self):

#创建主界面,并且保存到成员属性中

self.root = tkinter.Tk()

self.root.minsize(280, 450)

self.root.maxsize(280, 470)

self.root.title('小餅餅丶的简易计算器1.0')

# 设置显式面板的变量

self.result = tkinter.StringVar()

self.result.set(0)

# 设置一个全局变量 运算数字和f符号的列表

self.lists = []

# 添加一个用于判断是否按下运算符号的标志

self.ispresssign = False

# 界面布局

self.menus()

self.layout()

self.root.mainloop()

#计算器菜单界面摆放

def menus(self):

# 添加菜单

# 创建总菜单

allmenu = tkinter.Menu(self.root)

# 添加子菜单

filemenu = tkinter.Menu(allmenu, tearoff=0)

# 添加选项卡

filemenu.add_command(label='标准型(T) Alt+1', command=self.myfunc)

filemenu.add_command(label='科学型(S) Alt+2', command=self.myfunc)

filemenu.add_command(label='程序员(P) Alt+3', command=self.myfunc)

filemenu.add_command(label='统计信息(A) Alt+4', command=self.myfunc)

# 添加分割线

filemenu.add_separator()

# 添加选项卡

filemenu.add_command(label='历史记录(Y) Ctrl+H', command=self.myfunc)

filemenu.add_command(label='数字分组(I)', command=self.myfunc)

# 添加分割线

filemenu.add_separator()

# 添加选项卡

filemenu.add_command(label='基本(B) Ctrl+F4', command=self.myfunc)

filemenu.add_command(label='单位转换(U) Ctrl+U', command=self.myfunc)

filemenu.add_command(label='日期计算(D) Ctrl+E', command=self.myfunc)

menu1 = tkinter.Menu(filemenu, tearoff=0)

menu1.add_command(label='抵押(M)', command=self.myfunc)

menu1.add_command(label='汽车租赁(V)', command=self.myfunc)

menu1.add_command(label='油耗(mpg)(F)', command=self.myfunc)

menu1.add_command(label='油耗(l/100km)(U)', command=self.myfunc)

filemenu.add_cascade(label='工作表(W)', menu=menu1)

allmenu.add_cascade(label='查看(V)', menu=filemenu)

# 添加子菜单2

editmenu = tkinter.Menu(allmenu, tearoff=0)

# 添加选项卡

editmenu.add_command(label='复制(C) Ctrl+C', command=self.myfunc)

editmenu.add_command(label='粘贴(V) Ctrl+V', command=self.myfunc)

# 添加分割线

editmenu.add_separator()

# 添加选项卡

menu2 = tkinter.Menu(filemenu, tearoff=0)

menu2.add_command(label='复制历史记录(I)', command=self.myfunc)

menu2.add_command(label='编辑(E) F2', command=self.myfunc)

menu2.add_command(label='取消编辑(N) Esc', command=self.myfunc)

menu2.add_command(label='清除(L) Ctrl+Shift+D', command=self.myfunc)

editmenu.add_cascade(label='历史记录(H)', menu=menu2)

allmenu.add_cascade(label='编辑(E)', menu=editmenu)

# 添加子菜单3

helpmenu = tkinter.Menu(allmenu, tearoff=0)

# 添加选项卡

helpmenu.add_command(label='查看帮助(V) F1', command=self.myfunc)

# 添加分割线

helpmenu.add_separator()

# 添加选项卡

helpmenu.add_command(label='关于计算器(A)', command=self.myfunc)

allmenu.add_cascade(label='帮助(H)', menu=helpmenu)

self.root.config(menu=allmenu)

#计算器主界面摆放

def layout(self):

# 显示屏

result = tkinter.StringVar()

result.set(0)

show_label = tkinter.Label(self.root, bd=3, bg='white', font=('宋体', 30), anchor='e', textvariable=self.result)

show_label.place(x=5, y=20, width=270, height=70)

# 功能按钮MC

button_mc = tkinter.Button(self.root, text='MC', command=self.wait)

button_mc.place(x=5, y=95, width=50, height=50)

# 功能按钮MR

button_mr = tkinter.Button(self.root, text='MR', command=self.wait)

button_mr.place(x=60, y=95, width=50, height=50)

# 功能按钮MS

button_ms = tkinter.Button(self.root, text='MS', command=self.wait)

button_ms.place(x=115, y=95, width=50, height=50)

# 功能按钮M+

button_mjia = tkinter.Button(self.root, text='M+', command=self.wait)

button_mjia.place(x=170, y=95, width=50, height=50)

# 功能按钮M-

button_mjian = tkinter.Button(self.root, text='M-', command=self.wait)

button_mjian.place(x=225, y=95, width=50, height=50)

# 功能按钮←

button_zuo = tkinter.Button(self.root, text='←', command=self.dele_one)

button_zuo.place(x=5, y=150, width=50, height=50)

# 功能按钮CE

button_ce = tkinter.Button(self.root, text='CE', command=lambda: self.result.set(0))

button_ce.place(x=60, y=150, width=50, height=50)

# 功能按钮C

button_c = tkinter.Button(self.root, text='C', command=self.sweeppress)

button_c.place(x=115, y=150, width=50, height=50)

# 功能按钮±

button_zf = tkinter.Button(self.root, text='±', command=self.zf)

button_zf.place(x=170, y=150, width=50, height=50)

# 功能按钮√

button_kpf = tkinter.Button(self.root, text='√', command=self.kpf)

button_kpf.place(x=225, y=150, width=50, height=50)

# 数字按钮7

button_7 = tkinter.Button(self.root, text='7', command=lambda: self.pressnum('7'))

button_7.place(x=5, y=205, width=50, height=50)

# 数字按钮8

button_8 = tkinter.Button(self.root, text='8', command=lambda: self.pressnum('8'))

button_8.place(x=60, y=205, width=50, height=50)

# 数字按钮9

button_9 = tkinter.Button(self.root, text='9', command=lambda: self.pressnum('9'))

button_9.place(x=115, y=205, width=50, height=50)

# 功能按钮/

button_division = tkinter.Button(self.root, text='/', command=lambda: self.presscalculate('/'))

button_division.place(x=170, y=205, width=50, height=50)

# 功能按钮%

button_remainder = tkinter.Button(self.root, text='//', command=lambda:self.presscalculate('//'))

button_remainder.place(x=225, y=205, width=50, height=50)

# 数字按钮4

button_4 = tkinter.Button(self.root, text='4', command=lambda: self.pressnum('4'))

button_4.place(x=5, y=260, width=50, height=50)

# 数字按钮5

button_5 = tkinter.Button(self.root, text='5', command=lambda: self.pressnum('5'))

button_5.place(x=60, y=260, width=50, height=50)

# 数字按钮6

button_6 = tkinter.Button(self.root, text='6', command=lambda: self.pressnum('6'))

button_6.place(x=115, y=260, width=50, height=50)

# 功能按钮*

button_multiplication = tkinter.Button(self.root, text='*', command=lambda: self.presscalculate('*'))

button_multiplication.place(x=170, y=260, width=50, height=50)

# 功能按钮1/x

button_reciprocal = tkinter.Button(self.root, text='1/x', command=self.ds)

button_reciprocal.place(x=225, y=260, width=50, height=50)

# 数字按钮1

button_1 = tkinter.Button(self.root, text='1', command=lambda: self.pressnum('1'))

button_1.place(x=5, y=315, width=50, height=50)

# 数字按钮2

button_2 = tkinter.Button(self.root, text='2', command=lambda: self.pressnum('2'))

button_2.place(x=60, y=315, width=50, height=50)

# 数字按钮3

button_3 = tkinter.Button(self.root, text='3', command=lambda: self.pressnum('3'))

button_3.place(x=115, y=315, width=50, height=50)

# 功能按钮-

button_subtraction = tkinter.Button(self.root, text='-', command=lambda: self.presscalculate('-'))

button_subtraction.place(x=170, y=315, width=50, height=50)

# 功能按钮=

button_equal = tkinter.Button(self.root, text='=', command=lambda: self.pressequal())

button_equal.place(x=225, y=315, width=50, height=105)

# 数字按钮0

button_0 = tkinter.Button(self.root, text='0', command=lambda: self.pressnum('0'))

button_0.place(x=5, y=370, width=105, height=50)

# 功能按钮.

button_point = tkinter.Button(self.root, text='.', command=lambda: self.pressnum('.'))

button_point.place(x=115, y=370, width=50, height=50)

# 功能按钮+

button_plus = tkinter.Button(self.root, text='+', command=lambda: self.presscalculate('+'))

button_plus.place(x=170, y=370, width=50, height=50)

#计算器菜单功能

def myfunc(self):

tkinter.messagebox.showinfo('','程序员懒死在电脑前,打死也做不出的功能,只是装饰而已~')

#数字方法

def pressnum(self,num):

# 全局化变量

# 判断是否按下了运算符号

if self.ispresssign == False:

pass

else:

self.result.set(0)

# 重置运算符号的状态

self.ispresssign = False

if num == '.':

num = '0.'

# 获取面板中的原有数字

oldnum = self.result.get()

# 判断界面数字是否为0

if oldnum == '0':

self.result.set(num)

else:

# 连接上新按下的数字

newnum = oldnum + num

# 将按下的数字写到面板中

self.result.set(newnum)

#运算函数

def presscalculate(self,sign):

# 保存已经按下的数字和运算符号

# 获取界面数字

num = self.result.get()

self.lists.append(num)

# 保存按下的操作符号

self.lists.append(sign)

# 设置运算符号为按下状态

self.ispresssign = True

#获取运算结果

def pressequal(self):

# 获取所有的列表中的内容(之前的数字和操作)

# 获取当前界面上的数字

curnum = self.result.get()

# 将当前界面的数字存入列表

self.lists.append(curnum)

# 将列表转化为字符串

calculatestr = ''.join(self.lists)

# 使用eval执行字符串中的运算即可

endnum = eval(calculatestr)

# 将运算结果显示在界面中

self.result.set(str(endnum)[:10])

if self.lists != 0:

self.ispresssign = True

# 清空运算列表

self.lists.clear()

#暂未开发说明

def wait(self):

tkinter.messagebox.showinfo('','功能在努力的实现,请期待2.0版本的更新')

#←按键功能

def dele_one(self):

if self.result.get() == '' or self.result.get() == '0':

self.result.set('0')

return

else:

num = len(self.result.get())

if num > 1:

strnum = self.result.get()

strnum = strnum[0:num - 1]

self.result.set(strnum)

else:

self.result.set('0')

#±按键功能

def zf(self):

strnum = self.result.get()

if strnum[0] == '-':

self.result.set(strnum[1:])

elif strnum[0] != '-' and strnum != '0':

self.result.set('-' + strnum)

#1/x按键功能

def ds(self):

dsnum = 1 / int(self.result.get())

self.result.set(str(dsnum)[:10])

if self.lists != 0:

self.ispresssign = True

# 清空运算列表

self.lists.clear()

#C按键功能

def sweeppress(self):

self.lists.clear()

self.result.set(0)

#√按键功能

def kpf(self):

strnum = float(self.result.get())

endnum = math.sqrt(strnum)

if str(endnum)[-1] == '0':

self.result.set(str(endnum)[:-2])

else:

self.result.set(str(endnum)[:10])

if self.lists != 0:

self.ispresssign = True

# 清空运算列表

self.lists.clear()

#实例化对象

mycalculator = calculator()

pythontkinter真实的例子_python小实例——tkinter实战(计算器)相关推荐

  1. python tkinter计算器实例_python小实例——tkinter实战(计算器)

    1 importtkinter2 importmath3 importtkinter.messagebox4 5 classcalculator:6 #界面布局方法 7 def __init__(se ...

  2. pythontkinter真实的例子_Python Tkinter真实的例子

    不管常规方法如何,我们先尝试一个轻巧并且很实用的小例子.它会让你体会到在Tk程序背后看起来的最初感觉. 设计 我们要是用的例子是一个简单的GUI工具,用来将一个单位为英尺的数字转换为与其相等的米制单位 ...

  3. python数学计算例子_Python OpenCV实例:直方图计算(数学公式简单实现)

    #coding:utf-8 ''' 直方图 ''' import cv2 import numpy as np import matplotlib.pyplot as plt ''' 计算RGB图像每 ...

  4. python七段数码管显示学号_python小实例——七段数码管绘制

    首先用time库获取系统当前时间 然后用turtle库画出来 算是对于turtle库内函数的一次实践运用叭 import turtle as t import time def drawGap():/ ...

  5. 学习笔记(1):微信小程序开发实战-计算器实战页面布局

    立即学习:https://edu.csdn.net/course/play/3011/49695?utm_source=blogtoedu 20200701晚,学到12min

  6. python游戏贪吃蛇_Python写的贪吃蛇游戏例子_python

    这篇文章主要介绍了Python写的贪吃蛇游戏例子,练手作品,又好玩又可以学到东西,需要的朋友可以参考下 第一次用Python写这种比较实用且好玩的东西,权当练手吧 游戏说明: * P键控制" ...

  7. spring aop实例讲解_小实例理解Spring中的AOP----面向切面编程

    关于面向切面编程(Spring AOP),是在java面试中经常提及的,只有在充分理解了,日常工作中才能得心应手. 如何理解AOP呢?首先我们要思考为什么要使用切面编程,如下图: 对于一个系统来说保存 ...

  8. spring+mybatis之声明式事务管理初识(小实例)

    前几篇的文章都只是初步学习spring和mybatis框架,所写的实例也都非常简单,所进行的数据访问控制也都很简单,没有加入事务管理.这篇文章将初步接触事务管理. 1.事务管理 理解事务管理之前,先通 ...

  9. 三层架构-----实践篇-登录小实例

    上一篇博客小编简单介绍了一下我们在软件开发过程中应用到的三层架构基本理论.光有理论还是远远不够的,只有真正的 在项目中实践过,才能更好地理解它的精髓所在.接下来小编以一个实现登录功能的小例子来展现三层 ...

最新文章

  1. 一致性哈希算法的理解
  2. java定义一个类计算圆的半径,C++编程:定义一个圆类要求属性为半径,操作为计算圆的周长和面积...,java编程:定义一个圆类,属性为半径,方法为对输入的半径计...
  3. 发生地震等灾难,死难者的存款会怎么处理?
  4. 最详细的排序解析,理解七大排序
  5. C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
  6. spring官网下载
  7. linux网络 (二):无线网络操作
  8. oh my Zsh使用手册
  9. 化学堵水剂处理油井出水问题
  10. 使用RedisTemplate执行lua脚本
  11. 拉格朗日乘数法怎么判断极大极小_用拉格朗日乘数法求出极值后如何判断其是极大值还是极小值?...
  12. 关于概要设计文档的写作
  13. 窝囊同事做测试三年未涨工资,被开当天,bat全部高管门口迎接。
  14. NAS媒体库资源归集整理工具nas-tools
  15. 家用计算机防火墙设置,360家庭防火墙路由器电脑怎么设置?
  16. wetool企业微信营销管理系统开发
  17. Springboot毕业设计管理系统毕业设计-附源码221032
  18. 2022-2028全球及中国食品加工机械行业研究及十四五规划分析报告
  19. 涨知识--地球自转会影响飞机飞行时间吗?
  20. AutoJs超神级代码分享大更新

热门文章

  1. Android中出现内存泄露的原因
  2. 2021年大数据Hadoop(十二):HDFS的API操作
  3. 【CV】吴恩达机器学习课程笔记第18章
  4. [JavaScript] Map类型在JavaScript中的使用
  5. [JavaScript] 好用的 JavaScript Symbol 类型
  6. 中文版证书_CIA考试多久后才可以领取证书?领取的方式是什么?
  7. Installation failed with message Invalid File:(Application Installatino Failed)
  8. hdu5701-中位数计数
  9. Django的下载与基本命令
  10. AVL树、splay树(伸展树)和红黑树比较