Python开发桌面小程序功能
作者:小熊猫爱恰饭 时间:2023-07-01 14:46:59
当使用桌面应用程序的时候,
有没有那么一瞬间,
想学习一下桌面应用程序开发?
建议此次课程大家稍作了解不要浪费太多时间,
因为没有哪家公司会招聘以为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
标签:Python,桌面,小程序
0
投稿
猜你喜欢
通过Python收集汇聚MySQL 表信息的实例详解
2024-01-18 17:25:20
17个Python小技巧分享
2022-04-17 23:26:53
python中的插值 scipy-interp的实现代码
2022-02-23 02:50:09
javascript中利用柯里化函数实现bind方法【推荐】
2024-05-02 17:29:26
简单了解Go语言中函数作为值以及函数闭包的使用
2024-04-29 13:05:50
Golang的关键字defer的使用方法
2023-07-12 21:10:49
vue+AI智能机器人回复功能实现
2024-04-27 15:49:14
asp 快钱网关接口 支付宝接口 财付通接口代码
2011-03-08 10:55:00
python自动化测试三部曲之request+django实现接口测试
2023-07-11 05:12:00
在Python 的线程中运行协程的方法
2021-03-10 18:06:38
解决pytorch 保存模型遇到的问题
2021-12-13 08:26:37
Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法详解
2021-10-15 16:14:07
MySQL创建数据库并支持中文字符的操作方法
2024-01-27 16:09:45
关于MySql 10038错误的完美解决方法(三种)
2024-01-27 06:16:18
pandas string转dataframe的方法
2021-08-15 14:47:04
python中 logging的使用详解
2023-01-11 21:31:14
windows11安装SQL server数据库报错等待数据库引擎恢复句柄失败解决办法
2024-01-16 07:18:52
Python采集电影评论实战示例
2022-02-25 22:14:37
mssql查找备注(text,ntext)类型字段为空的方法
2024-01-28 04:13:18
flask-restful使用总结
2023-02-10 13:24:21