pyqt5 制作壁纸切换工具实例 第一章

pyqt5 制作壁纸切换工具实例 第二章 https://blog.csdn.net/mtl1994/article/details/118152403


前言

效果图

bizhi


一、代码

1.结构

wallpaper

----resource

--------icon

------------icon.png

----src

--------frame

------------main.py

------------ui_mainwindow.py

----main.py
frame/main.py

​	MainWidget

​		__init__

​			1.初始化frame/ui_mainwindows.py

​			2.初始化系统托盘

​			3.给系统托盘设置点击方法

frame/ui_mainwindow.py

​	Ui_MainWindow

​		setupUi

​			1.布局

​		retranslateUi

​			1.赋值

main.py

​	1.主函数启动类

frame/main.py

# -*- coding: utf-8 -*-
import os
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, QSystemTrayIcon

from src.frame.ui_mainwindow import Ui_MainWindow


class MainWidget(Ui_MainWindow, QMainWindow):
    def __init__(self, parent=None):
        self.icon_path = os.path.join(sys.path[1], 'resource/icon/icon.png')
        super(MainWidget, self).__init__(parent)
        self.setupUi(self)
        self.show_flag = False
        self.setWindowTitle("壁纸")
        self.setWindowIcon(QIcon(self.icon_path))
        self.Ui_MainWindow = Ui_MainWindow
        #系统托盘
        self.trayIcon = QSystemTrayIcon(self)
        self.trayIcon.setIcon(QIcon(self.icon_path))
        self.trayIcon.setToolTip("壁纸")
        self.trayIcon.show()
        self.trayIcon.activated.connect(self.tray_click)
        #设置在系统托盘上方显示
        tg = self.trayIcon.geometry()
        self.setGeometry(tg.x()-452/2,tg.y()-802,452, 802)

        

    def tray_click(self, reason):
        if reason in [1,3]:
            if self.show_flag:
                self.hide()
            else:
                self.show()
            self.show_flag = not self.show_flag

frame/ui_mainwindow.py

# -*- coding: utf-8 -*-
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMainWindow, QHBoxLayout, QLabel, QWidget, QGridLayout


class Ui_MainWindow(QMainWindow):
    def setupUi(self,MainWindow):
        self.resize(452, 802)
        MainWindow.setWindowFlags(QtCore.Qt.SubWindow|QtCore.Qt.FramelessWindowHint)
        self.title = QLabel(self)
        self.retranslateUi()

        self.center_widget = QWidget()
        self.hbox = QHBoxLayout()
        self.label = QLabel("1",self)
        self.label2 = QLabel("2",self)
        self.label3 = QLabel("3",self)
        self.hbox.addWidget(self.label)
        self.hbox.addWidget(self.label2)
        self.hbox.addWidget(self.label3)
        self.center_widget.setLayout(self.hbox)

        self.bottom_widget = QWidget()

        # 创建窗口主部件
        self.main_widget = QWidget()
        # 创建主部件的网格布局
        self.main_layout = QGridLayout()
        # 设置窗口主部件布局为网格布局
        self.main_widget.setLayout(self.main_layout)

        # 左侧部件在第0行第0列,占12行5列
        self.main_layout.addWidget(self.title, 0, 0, 1, 12, QtCore.Qt.AlignCenter)
        self.main_layout.addWidget(self.center_widget,1,0,1,12)
        # 右侧部件在第0行第6列,占12行7列
        self.main_layout.addWidget(self.bottom_widget, 2, 0, 12, 8)
        # 设置窗口主部件
        MainWindow.setCentralWidget(self.main_widget)



    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.title.setText(_translate("MainWindow", "壁纸"))

main.py

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import traceback

from PyQt5.QtWidgets import QApplication

from frame.main import  MainWidget

if __name__ == '__main__':
    try:
        app = QApplication(sys.argv)
        QApplication.setQuitOnLastWindowClosed(False)  # 关闭最后一个窗口不退出程序
        window = MainWidget()
        # window.show()
        sys.exit(app.exec_())
    except Exception as e:
        traceback.print_exc()




总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

matianlongg

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

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

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

打赏作者

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

抵扣说明:

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

余额充值