学会Python开发的第一步:写一个桌面小程序

前言

嗨喽,大家好呀~这里是爱看美女的茜茜呐

又到了学Python时刻~

当使用桌面应用程序的时候,有没有那么一瞬间,

想学习一下桌面应用程序开发?

建议此次课程大家稍作了解不要浪费太多时间,

因为没有哪家公司会招聘以为Python程序员开发桌面程序吧?

开发环境:

  • Python 3.6

    Python是一种代表简单主义思想的语言。

    阅读一个良好的Python程序就感觉像是在读英语一样。

    它使你能够专注于解决问题而不是去搞明白语言本身。

  • Pycharm

    PyCharm是一种Python IDE(Integrated Development Environment,集成开发环境),

    带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具

    比如调试、语法高亮、项目管理、代码跳转、智能提示、自动完成、单元测试、版本控制。

    此外,该IDE提供了一些高级功能,以用于支持Django框架下的专业Web开发。

代码

界面设置

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)

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

先试试

运算得出结果

PS:如有需要Python学习资料的小伙伴可以加下方的群去找免费管理员领取

可以免费领取源码、项目实战视频、PDF文件等

宁外给大家推荐一个好的python教程:

【48小时搞定全套教程!你和大佬只有一步之遥【python教程】

尾语 💝

感谢你观看我的文章呐~本次航班到这里就结束啦 🛬

希望本篇文章有对你带来帮助 🎉,有学习到一点知识~

躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。

最后,博主要一下你们的三连呀(点赞、评论、收藏),不要钱的还是可以搞一搞的嘛~

不知道评论啥的,即使扣个6666也是对博主的鼓舞吖 💞 感谢 💐

  • 105
    点赞
  • 476
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 60
    评论
好的,以下是用 Python 一个简单的网站导航桌面程序的基本步骤: 1. 首先,需要安装 PyQt5 库,这是一个 Python 的 GUI 工具包,可以用来创建桌面应用程序。可以使用 pip 命令来安装 PyQt5 库。 2. 接下来,需要设计程序的界面。可以使用 Qt Designer 来设计程序的界面,然后将设计好的 UI 文件转换成 Python 代码。这可以通过使用 pyuic 工具来完成,例如: pyuic5 mydesign.ui -o mydesign.py 其中 mydesign.ui 是 Qt Designer 设计的 UI 文件,mydesign.py 是生成的 Python 代码文件。 3. 创建主程序。主程序需要导入 PyQt5 库和刚刚转换得到的 UI 代码。然后,可以使用 PyQt5 提供的一些组件来实现程序的功能,例如 QLabel、QLineEdit、QPushButton 等。可以使用这些组件来创建一个窗口,包含网站导航的链接列表。 4. 在程序中添加链接。可以使用 QListWidget 组件来显示链接列表,并使用 QListWidgetItem 组件来添加链接。可以使用 QLineEdit 组件来添加新的链接,并使用 QPushButton 组件来添加“添加链接”按钮。 5. 实现链接跳转功能。可以使用 QDesktopServices.openUrl() 方法来实现链接的跳转。可以将该方法与 QListWidget 组件的 itemDoubleClicked 信号连接起来,以便在双击链接时打开链接。 6. 最后,运行程序,测试它是否正常工作。可以使用以下命令来启动程序python myprogram.py 其中 myprogram.py 是程序Python 代码文件名。 希望这些步骤可以帮助你开始编自己的网站导航桌面程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 60
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茜茜是帅哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值