怎么利用Python开发一个桌面小程序
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,这篇文章将为大家详细讲解有关怎么利用Python开发一个桌面小程序,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。开发环境:Python 3.6Pycharm代码界面
千家信息网最后更新 2025年02月01日怎么利用Python开发一个桌面小程序
这篇文章将为大家详细讲解有关怎么利用Python开发一个桌面小程序,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
开发环境:
Python 3.6
Pycharm
代码
界面设置
1.导入模块
import tkinter as tk
2.实例化一个窗体对象
root = tk.Tk()
3.标题
root.title('计算器')
4.大小以及出现的位置
root.geometry("295x280+150+150")
5.透明度
root.attributes("-alpha", 0.9)
6.背景
root["background"] = "#ffffff"
7.标签
lable1 = tk.Label(root, textvariable=result_num, width=20, height=2, font=('宋体', 20), justify='left', background='#ffffff', anchor='se')
8.布局
lable1.grid(padx=4, pady=4, row=0, column=0, columnspan=4)
9.按钮
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)
最后运行代码,效果如下图
先试试
运算得出结果
关于"怎么利用Python开发一个桌面小程序"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
宋体
开发
篇文章
桌面
程序
代码
内容
效果
更多
界面
运算
不错
实用
位置
功能
大小
实例
对象
布局
按钮
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务云服务器的价格对比
软件开发计划2006案例
迷你世界怎么把服务器弄崩溃
数据库表加前缀
狼人杀哪个服务器人多
舟山定制软件开发
乡村活寡美人 刘虎娃全文数据库
网络技术公司不用缴税
数据库原理及应用第四版答案吴靖
风电场单位网络安全自查报告
网络安全专家与黑客哪个牛逼
闵行区互联网软件开发大概费用
服务器中smt代表什么意思
关系数据库表的定义
素奇网络技术工作室
mc服务器10个人一个月
普华永道网络安全部管理人员
打印机服务器 固网
网络安全师怎么考
网络安全受到监控怎么改回来
国内电脑服务器排名
三级网络技术大题目空格
做网站属于软件开发吗
涉及软件开发的法律法规贵
九台区网络技术咨询服务保障
网络技术公司不用缴税
维护网络安全人人有责 关乎
麻将机软件开发定制
宏观网络安全
关于通信网络技术