Python开发桌面小程序功能

 更新时间:2022年04月11日 08:08:53   作者:小熊猫爱恰饭  
这篇文章主要介绍了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)

最后运行代码,效果如下图

先试试

请添加图片描述

运算得出结果

请添加图片描述

到此这篇关于Python开发一个桌面小程序的文章就介绍到这了,更多相关Python开发一个桌面小程序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python实战整活之聊天机器人

    Python实战整活之聊天机器人

    这篇文章主要介绍了Python实战整活之聊天机器人,文中有非常详细的代码示例,对正在学习python的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • Python脚本Selenium及页面Web元素定位详解

    Python脚本Selenium及页面Web元素定位详解

    这篇文章主要为大家介绍了Python脚本中如何使用Selenium定位页面Web元素的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-10-10
  • python使用Apriori算法进行关联性解析

    python使用Apriori算法进行关联性解析

    这篇文章主要为大家分享了python使用Apriori算法进行关联性的解析,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Python实现监听目录并取消文件只读属性

    Python实现监听目录并取消文件只读属性

    这篇文章主要为大家详细介绍了如何利用Python实现监听目录并取消文件只读属性,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-07-07
  • python中判断文件结束符的具体方法

    python中判断文件结束符的具体方法

    在本篇文章里小编给大家分享的是一篇关于python中判断文件结束符的具体方法,有兴趣的朋友们可以参考学习下。
    2020-08-08
  • python数据处理详情

    python数据处理详情

    这篇文章主要介绍了python数据处理详情,文章通过json文件的一个数据表展开对主题的详细说明,具有一定的参考价值需要的小伙伴可以参考一下
    2022-04-04
  • Python编译过程和执行原理解析

    Python编译过程和执行原理解析

    这篇文章主要介绍了Python编译过程和执行原理解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • Python实现大数据收集至excel的思路详解

    Python实现大数据收集至excel的思路详解

    这篇文章主要介绍了Python实现大数据收集至excel的思路,本文通过完整代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • Python模拟百度自动输入搜索功能的实例

    Python模拟百度自动输入搜索功能的实例

    今天小编就为大家分享一篇Python模拟百度自动输入搜索功能的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • python多线程调用exit无法退出的解决方法

    python多线程调用exit无法退出的解决方法

    今天小编就为大家分享一篇python多线程调用exit无法退出的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02

最新评论