比方便面还方便~利用Python开发一个桌面小程序
当使用桌面应用程序的时候,
有没有那么一瞬间,
想学习一下桌面应用程序开发?
建议此次课程大家稍作了解不要浪费太多时间,
因为没有哪家公司会招聘以为Python程序员开发桌面程序吧?
开发环境:
- Python 3.6
- Pycharm
代码
界面设置
- 导入模块
import tkinter as tk
- 实例化一个窗体对象
root = tk.Tk()
- 标题
root.title('计算器')
- 大小以及出现的位置
root.geometry("295x280+150+150")
- 透明度
root.attributes("-alpha", 0.9)
- 背景
root["background"] = "#ffffff"
- 标签
lable1 = tk.Label(root, textvariable=result_num, width=20, height=2, font=('宋体', 20), justify='left', background='#ffffff', anchor='se')
- 布局
lable1.grid(padx=4, pady=4, row=0, column=0, columnspan=4)
- 按钮
button_clear = tk.Button(root, text='C', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: clear())
button_back = tk.Button(root, text='←', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: back())
button_division = tk.Button(root, text='/', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('/'))
button_multiplication = tk.Button(root, text='x', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('*'))button_clear .grid(padx=4, row=1, column=0)
button_back .grid(padx=4, row=1, column=1)
button_division .grid(padx=4, row=1, column=2)
button_multiplication .grid(padx=4, row=1, column=3)button_seven = tk.Button(root, text='7', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('7'))
button_eight = tk.Button(root, text='8', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('8'))
button_nine = tk.Button(root, text='9', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('9'))
button_subtraction = tk.Button(root, text='—', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('-'))
button_seven .grid(padx=4, row=2, column=0)
button_eight .grid(padx=4, row=2, column=1)
button_nine .grid(padx=4, row=2, column=2)
button_subtraction .grid(padx=4, row=2, column=3)button_four = tk.Button(root, text='4', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('4'))
button_four.grid(padx=4, pady=4, row=3, column=0)
button_five = tk.Button(root, text='5', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('5'))
button_five.grid(padx=4, row=3, column=1)
button_six = tk.Button(root, text='6', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('6'))
button_six.grid(padx=4, row=3, column=2)
button_addition = tk.Button(root, text='+', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('+'))
button_addition.grid(padx=4, row=3, column=3)button_one = tk.Button(root, text='1', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('1'))
button_one.grid(padx=4, row=4, column=0)
button_two = tk.Button(root, text='2', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('2'))
button_two.grid(padx=4, row=4, column=1)
button_three = tk.Button(root, text='3', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('3'))
button_three.grid(padx=4, row=4, column=2)
button_equal = tk.Button(root, text='=', width=5, height=3, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: equal())
button_equal.grid(padx=4, row=4, rowspan=5, column=3)button_zero = tk.Button(root, text='0', width=12, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('0'))
button_zero.grid(padx=4, pady=4, row=5, column=0, columnspan=2)
button_decimal = tk.Button(root, text='.', width=5, font=('宋体', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('.'))
button_decimal.grid(padx=4, row=5, column=2)
现在得出界面效果
功能
添加数字
def append_num(i):lists.append(i)result_num.set(''.join(lists))
选择运算符号
def operator(i):if len(lists) > 0:if lists[-1] in ['+', '-', '*', '/']:lists[-1] = ielse:lists.append(i)result_num.set(''.join(lists))
清零
def clear():lists.clear()result_num.set(0)
退格
def back():del lists[-1]result_num.set(lists)
等号
def equal():a = ''.join(lists)end_num = eval(a)result_num.set(end_num)lists.clear()lists.append(str(end_num))
定义一个列表收集输入的内容
lists = []
result_num = tk.StringVar()
result_num.set(0)
最后运行代码,效果如下图
先试试
运算得出结果
比方便面还方便~利用Python开发一个桌面小程序相关推荐
- python快递分拣小程序_利用Python开发的ATM小程序
最近在学习Python,便利用业余时间开发了一个模拟ATM抢银行的小程序,不废话,直接上程序 #!/usr/bin/env python #coding=utf-8 # Name: AtmCard.p ...
- Python 开发一个间谍小程序
作者:寂夜云 https://www.cnblogs.com/lonenysky/p/12341074.html 这次我们使用 Python 来打造一款间谍程序,程序中会用到许多知识点,大致分为四块: ...
- Python 开发一个间谍小程序!编程语言中的无间道!
这次我们使用 Python 来打造一款间谍程序,程序中会用到许多知识点,大致分为四块: win32API Python基础,重点在cpytes库的使用 C语言基础 Hook 程序的基本原理在于通过注册 ...
- python英语词汇读音_利用Python制作查单词小程序(一):抓取来自百度翻译的单词释义和音标...
小编在学习英语的时候,遇到不认识的英语单词,会用百度翻译来查询单词的释义和音标,并播放单词的读音.为了便于复习和记忆,需要将单词的释义和音标以复制粘贴的方式保存到本地. 这个过程非常繁琐,于是小编就想 ...
- python制作阴阳师脚本_利用python制作一个阴阳师小游戏
利用python制作一个阴阳师小游戏 发布时间:2020-11-27 13:59:49 来源:亿速云 阅读:84 这期内容当中小编将会给大家带来有关利用python制作一个阴阳师小游戏,文章内容丰富且 ...
- python socket能做什么_用python写一个聊天小程序!和女朋友的专属聊天工具!
原标题:用python写一个聊天小程序!和女朋友的专属聊天工具! 1.UDP简介 Internet协议集支持一个无连接的传输协议,该协议称为用户数据报协议(UDP).UDP为应用程序提供了无需建立就可 ...
- 只需6步,教你从零开发一个签到小程序
摘要:针对于具备瞬时流量.业务场景比较简单的小程序,开发者借助FunctionGraph,可以方便快捷的搭建一个健壮的小程序后台. 本文分享自华为云社区<智慧校园想搞好,FunctionGrap ...
- 一个程序如何连接到外网_如何从头开始开发一个微信小程序
网上有很多的人在问:怎么开发一个微信小程序?今天我来给大家详细讲讲如何申请开发并部署一个微信小程序,大家看完这篇文章后就能够自己运营一个属于自己的小程序了. 现在的小程序有百度小程序,头条小程序,支付 ...
- 开发一个微信小程序,对酒店经营管理有哪些好处?
据腾讯2022年第一季度财报数据显示,微信小程序日活跃账户已经突破5亿,流量巨大.不论企业用户还是个体商家都积极使用小程序开展商业活动,从这庞大流量里获利. 酒店行业也不例外,很多酒店都开发了微信小程 ...
- 如何开发一个微信小程序
如今微信小程序的开发已经成为了一个不可忽视的趋势,不少企业都开始将自己的产品和服务通过微信小程序进行宣传.但是如何开发一个微信小程序大家又了解吗?下面就为大家介绍如何开发一个微信小程序. 首先我们做好 ...
最新文章
- 大话IT职场之工作和生活的平衡
- 数据集获取加速神器来了!
- SharePoint REST API - 同步SharePoint列表项
- mac下的抓包工具Charles
- 源文件的编码会对编译结果有影响
- struts2 跳转类型 result type=chain、dispatcher、redirect
- Visual Studio Code 里针对 SAP Commerce Cloud Impex 文件的语法高亮扩展
- c语言中汉字属于什么类型_练字应该练什么之:汉字基本结构的类型
- java jquery jsonp 跨域_Jquery跨域调用(JSONP)遇到error问题的解决
- windows server 安装php环境
- DVM 和 JVM 的区别?
- python安装plotly教程_python plotly 使用教程
- smobiler中实现页面切换_Smobiler学习日志——.Net语言 APP开发平台:如何在webview中加载网页,smobilerwebview...
- KMeans聚类算法
- 如何判断肖特基二极管的正负极
- Latex 摘要排版
- 查看笔记本预装系统的产品密钥
- 写一个简单的校园网多拨思路
- 助教日志_【沈阳航空航天大学软件工程 1,2班】期末排行
- 微信公众号所有文章下载链接获取