大家好,我是帅哥(*:ஐ٩(๑´ᵕ`)۶ஐ:* 学习python使我进步)

今天来一起做一个计算器!!!

首先,我们要用到几个python三方库

from tkinter import *
import tkinter.font
from functools import partial

然后,定义几个函数

def get_input(entry, argu):entry.insert(END, argu)def backspace(entry):input_len = len(entry.get())entry.delete(input_len - 1)def clear(entry):entry.delete(0, END)def calc(entry):input = entry.get()output = str(eval(input.strip()))clear(entry)entry.insert(END, output)

定义好了之后,再定义一个主函数

def cal():root = Tk()root.title("Calc")root.resizable(0, 0)entry_font = tkinter.font.Font(size=12)entry = Entry(root, justify="right", font=entry_font)entry.grid(row=0, column=0, columnspan=4, sticky=N+W+S+E, padx=5,  pady=5)button_font = tkinter.font.Font(size=10, weight=tkinter.font.BOLD)button_bg = '#D5E0EE'button_active_bg = '#E5E35B'myButton = partial(Button, root, bg=button_bg, padx=10, pady=3, activebackground = button_active_bg)button7 = myButton(text='7', command=lambda: get_input(entry, '7'))button7.grid(row=1, column=0, pady=5)button8 = myButton(text='8', command=lambda: get_input(entry, '8'))button8.grid(row=1, column=1, pady=5)button9 = myButton(text='9', command=lambda: get_input(entry, '9'))button9.grid(row=1, column=2, pady=5)button10 = myButton(text='+', command=lambda: get_input(entry, '+'))button10.grid(row=1, column=3, pady=5)button4 = myButton(text='4', command=lambda: get_input(entry, '4'))button4.grid(row=2, column=0, pady=5)button5 = myButton(text='5', command=lambda: get_input(entry, '5'))button5.grid(row=2, column=1, pady=5)button6 = myButton(text='6', command=lambda: get_input(entry, '6'))button6.grid(row=2, column=2, pady=5)button11 = myButton(text='-', command=lambda: get_input(entry, '-'))button11.grid(row=2, column=3, pady=5)button1 = myButton(text='1', command=lambda: get_input(entry, '1'))button1.grid(row=3, column=0, pady=5)button2 = myButton(text='2', command=lambda: get_input(entry, '2'))button2.grid(row=3, column=1, pady=5)button3 = myButton(text='3', command=lambda: get_input(entry, '3'))button3.grid(row=3, column=2, pady=5)button12 = myButton(text='*', command=lambda: get_input(entry, '*'))button12.grid(row=3, column=3, pady=5)button0 = myButton(text='0', command=lambda: get_input(entry, '0'))button0.grid(row=4, column=0, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)button13 = myButton(text='.', command=lambda: get_input(entry, '.'))button13.grid(row=4, column=2, pady=5)button14 = Button(root, text='÷', bg=button_bg, padx=10, pady=3,command=lambda: get_input(entry, '÷'))button14.grid(row=4, column=3, pady=5)button15 = Button(root, text='×', bg=button_bg, padx=10, pady=3,command=lambda: backspace(entry), activebackground=button_active_bg)button15.grid(row=5, column=0, pady=5)button16 = Button(root, text='C', bg=button_bg, padx=10, pady=3,command=lambda : clear(entry), activebackground=button_active_bg)button16.grid(row=5, column=1, pady=5)button17 = Button(root, text='=', bg=button_bg, padx=10, pady=3,command=lambda: calc(entry), activebackground=button_active_bg)button17.grid(row=5, column=2, columnspan=2, padx=3, pady=5, sticky=N+S+E+W)root.mainloop()

弄完了之后,运行代码

if __name__ == '__main__':cal()

效果展示

知识点:

if语句

def创建函数语句

使用知识点(代码):

1.if:

if 内容:

执行

2.def

def 函数名():

函数执行的内容

以上就是所有内容,记得关注帅哥,有问题可以私聊我哦~(java、html、python、js、css都可以问我~)

【python基础教程】计算器相关推荐

  1. python基础教程博客_python基础教程(一)

    之所以选择py交易有以下几点:1.python是胶水语言(跨平台),2.python无所不能(除了底层),3.python编写方便(notepad++等文本编辑器就能搞事情),4.渗透方面很多脚本都是 ...

  2. python基础教程攻略-python基础教程(一)

    之所以选择py交易有以下几点:1.python是胶水语言(跨平台),2.python无所不能(除了底层),3.python编写方便(notepad++等文本编辑器就能搞事情),4.渗透方面很多脚本都是 ...

  3. python基础教程是什么-Python基础教程(一)

    之所以选择py交易有以下几点:1.python是胶水语言(跨平台),2.python无所不能(除了底层),3.python编写方便(notepad++等文本编辑器就能搞事情),4.渗透方面很多脚本都是 ...

  4. 《Python基础教程(第3版)》笔记:第8章异常

    <Python基础教程(第3版)>笔记:异常 重点 第8章 异常 异常对象未被处理(或捕获)时,程序将终止并显示一条错误信息:traceback 每个异常都是某个类的实例, 如何创建异常 ...

  5. 《Python基础教程》学习笔记——条件、循环及其他语句

    Python基础教程--条件.循环及其他语句 条件.循环及其他语句 再谈 print 和 import 打印多个参数 导入时重命名 赋值魔法 序列解包 链式赋值 增强赋值 代码块:缩进的乐趣 条件和条 ...

  6. 编程入门书籍-Python基础教程(第3版)

    Python基础教程(第3版) Magnus Lie Hetland 著,袁国忠 译 ISBN:9787115474889 包装:平装 开本:16开 正文语种:中文 出版社:人民邮电出版社 出版时间: ...

  7. Python基础教程学习笔记:第一章 基础知识

    Python基础教程 第二版 学习笔记 1.python的每一个语句的后面可以添加分号也可以不添加分号:在一行有多条语句的时候,必须使用分号加以区分 2.查看Python版本号,在Dos窗口中输入&q ...

  8. python基础教程第二版和第三版哪个好-python基础教程 2版和3版哪个适合新手?!...

    python基础教程 2版和3版哪个适合新手? 现在学是学python3. 两者的差异发者本身影响并不大,个别语法细微的差比如python3的print数方式使用,一些常用模块的名称差异,一些机制的差 ...

  9. python基础教程第二版答案-《Python基础教程》(第2版修订版)中要注意的地方...

    想必现在大家学习Python都会用3.0及以上的版本了,虽然2.X的版本依然很多,但是这两年随着部分开源项目不再支持2.X版本,3.X已经成为趋势. 正是在这样的大背景下,我最近用这本<Pyth ...

  10. python基础教程第三版电子版百度云-《python基础教程第三版》高清版PDF免费下载...

    下载地址1:http://t.cn/EGxO1sW Python基础教程 第3版Python简明教程书籍 Python编程从入门到实践 灵程序设计丛书 <python基础教程第三版>高清版 ...

最新文章

  1. Spring入门第十三课
  2. python判断实例的类型
  3. 复习(三)—— 进程管理详解
  4. Java中使用Jedis连接池连接Redis数据库流程
  5. NYOJ 595 乱七八糟
  6. spring+hibernate中的Result object returned from HibernateCallback isn't a List
  7. vue中用的swiper轮播图的用法github的地址
  8. DB2 常用命令小结
  9. 遗传算法原理及算法步骤(学习篇·上)
  10. 银行系统需要处理的信息
  11. ME525+刷机2.3.6版本过程分享
  12. 从云到「链」,京东云成为中国第四朵云背后
  13. C++笔记 文件处理笔记
  14. Data URL和图片(前端细节优化)
  15. sequoia中的自动故障处理
  16. java early eof_idea克隆项目,git clone出现early EOF问题的解决方案
  17. 王道数据结构习题代码3.3.7(栈和队列的应用)
  18. Linux 安装Mysql5.6教程
  19. R语言在线地图神器:Leaflet for R包(三) 符号标记
  20. C# GZip 压缩 / 解压

热门文章

  1. 教程篇(6.0) 01. FortiGate及其Security Fabric介绍 ❀ FortiGate 安全 ❀ Fortinet 网络安全专家 NSE 4
  2. Python 自然语言处理(基于jieba分词和NLTK)
  3. app分享到微信的方案
  4. mysql lbs_基于LBS的地理位置附近的搜索以及由近及远的排序(MYSQL)
  5. 在Nignx增加http2模块顺便聊聊HTTP的八卦
  6. gpu精粹2.高性能图形芯片和通用计算机编程技巧,哎哟喂啊
  7. Mysql数据库-表的增、删、查、改
  8. (二)使用Keadm进行部署KubeEdge
  9. 获取数据库表格的字段名、类型、长度、注释等属性
  10. docker安装mysql遇到的问题