Python开发桌面小程序功能
作者:小熊猫爱恰饭 发布时间:2023-07-01 14:46:59
标签: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] = i
else:
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)
最后运行代码,效果如下图
先试试
运算得出结果
来源:https://blog.csdn.net/m0_67575344/article/details/124026574


猜你喜欢
- 不过最近发现这个可视化操作有点点问题,就是当数据条数超过一定数目EMS SQL Manager就挂了,也不知道是否是软件问题……当然该开始我
- 首先非常感谢作者针对bootstrap table分页问题进行详细的整理,并分享给了大家,希望通过这篇文章可以帮助大家解决Bootstrap
- 图像的全景拼接包括三大部分:特征点提取与匹配、图像配准、图像融合。1、基于SIFT的特征点的提取与匹配利用Sift提取图像的局部特征,在尺度
- 图片非常重要,它们可以让你的页面更好看,更引人注目。但是,高质量和漂亮的图片常常会很大,它们会让页面加载变慢并消耗更多带宽。所以我们,这些设
- 介绍Matplotlib是Python中使用最广泛的数据可视化库之一。无论是简单还是复杂的可视化项目,它都是大多数人的首选库。在本教程中,我
- 当代码已经写得差不多,发现某个变量名需要修改,但代码中很多地方都有该变量,一一修改太麻烦了,在不同的情景下,可以采取更加简便的方法,如下介绍
- 一、为什么要进行包管理?python中的三方包琳琅满目,提供了各种各样的功能,使得我们免于自己去手写很多代码。比如,我们在测试接口的时候,我
- 前言:文章利用Python pygame做一个贪吃蛇的小游戏而且讲清楚每一段代码是用来干嘛的。据说是贪吃蛇游戏是1976年,Gremlin公
- Transact-SQL(又称T-SQL),是在Microsoft SQL Server和Sybase SQL
- 我在配置mysql时将配置文件中的默认存储引擎设定为了InnoDB。今天查看了MyISAM与InnoDB的区别,在该文中的第七条“MyISA
- 一个不错的js星级评分代码,可以评多个指标。相关文章推荐:用css制作星级投票评分功能 效果图:<script language=&q
- 正确使用字体和颜色可以让网页内容更易阅读,下面我们来看看具体的优化措施。留意颜色的对比对于视力不太好的人或者对于不太好的显示设备来说,黑地白
- 我们进行CSS网页布局的时候,都知道它需要符合XHTML1.0规范。如果我们在进行CSS网页布局的时候,还在使用被W3C废弃的元素,那就失去
- LMDB的全称是Lightning Memory-Mapped Database(快如闪电的内存映射数据库),它的文件结构简单,包含一个数据
- 监控中,通常要使用图片更直观的看出集群的运行状况。以下是一个简单的demo,通过rrdtool生成动态的图片。Python3, tornad
- 现在向大家介绍mysql命令行下,从数据库的建立到表数据的删除全过程,希望对大家有所帮助。登陆mysql打cmd命令终端,如果已经添加了my
- html的标签的属性,比如id、class、href需要动态传递参数,拼接字符串,查了一些资料,并没有找到合适的解决方法,琢磨了一上午,终于
- BEGIN -- 声明变量 DECLARE v_addtime_begin varchar(13); DECLARE v_addtime_e
- 这是一个适合移动设备WEB应用的日期和时间拾取器,在桌面版的日期拾取器我们一般用jQuery UI的datepicker插件,而移动手机版的
- 一:模板要了解jinja2,那么需要先理解模板的概念。模板在Python的web开发中广泛使用,它能够有效的将业务逻辑和页面逻辑分开,使代码