关于我想写个脚本,最后却搞了个桌面宠物这件事(三)

前言

在第二篇文章中,通过了python调用opencv的模板匹配函数干掉了自动锁屏功能,拒绝省电。然后因为没有提示框的问题准备用pyqt将功能纳入界面当中。不知道是不是很多人和我一样,就是不太喜欢用UI来画界面。。。


一、基本UI布局

头文件:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
···python
from random import random,choice
import string

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtCore import pyqtProperty

import os
from PIL import ImageGrab
import time
import pyautogui
import cv2
import numpy as np
import qtawesome

from PyQt5.QtMultimedia import QMediaContent,QMediaPlayer
import qtawesome as qta
import requests
import traceback

from math import floor, pi, cos, sin
from random import random, randint
from time import time,sleep
import math

一共分为三个Qwidget,主窗口main_widget , 其中又分为左侧和右测left_widget,right_widget,
基于大佬的布局,然后设置自己的背景图片,增加自己的细节,在python代码的文件夹同一目录,准备两张照片,Logo和background

# 主窗口
class Window(QMainWindow,QWidget):
    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        self.init_ui()
        
    def init_ui(self):
	  self.resize(798,489)	# 数据参考你的背景图片大小
	  # move()方法移动了窗口到屏幕坐标x=300, y=300的位置.
	  # self.move(300,300)
	  # 在这里我们设置了窗口的标题.标题会被显示在标题栏上.
	  self.setWindowTitle('Never Lock Windows')
	  # 设置左上角logo
	  self.setWindowIcon(QIcon('Logo.png'))
	  # 设置背景,必须要用QMainWindow,不能用QWidget
	  palette = QPalette()
	  palette.setBrush(QPalette.Background, QBrush(QPixmap("background.jpg")))
	  self.setPalette(palette)

增加button和label,并添加QSS美化,为了方便,将按钮事件放到一个函数里面

###################
 self.main_widget = QWidget()  # 创建窗口主部件
 self.main_layout = QGridLayout()  # 创建主部件的网格布局
 self.main_widget.setLayout(self.main_layout)  # 设置窗口主部件布局为网格布局
 
 ###################
 self.left_widget = QWidget()  # 创建左侧部件
 self.left_widget.setObjectName('left_widget')
 self.left_layout = QGridLayout()  # 创建左侧部件的网格布局层
 self.left_widget.setLayout(self.left_layout) # 设置左侧部件布局为网格
 self.left_widget.setStyleSheet('''
     QPushButton{border:none;color:white;}
     QPushButton#left_label{
         border:none;
         border-bottom:1px solid white;
         font-size:18px;
         font-weight:700;
         font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
     }
     QPushButton#left_button:hover{border-left:4px solid red;font-weight:700;}
 ''')

 self.right_widget = QWidget() # 创建右侧部件
 self.right_widget.setObjectName('right_widget')
 self.right_layout = QGridLayout()
 self.right_widget.setStyleSheet('''
     QWidget#right_widget{
         color:#232C51;
         background:none;
         border-top:1px solid darkGray;
         border-bottom:1px solid darkGray;
         border-right:1px solid darkGray;
         border-top-right-radius:10px;
         border-bottom-right-radius:10px;
     }
     QLabel#right_lable{
         border:none;
         font-size:16px;
         font-weight:700;
         font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
     }
 ''')
 self.right_widget.setLayout(self.right_layout) # 设置右侧部件布局为网格

 self.main_layout.addWidget(self.left_widget,0,0,12,2) # 左侧部件在第0行第0列,占8行3列
 self.main_layout.addWidget(self.right_widget,0,2,12,10) # 右侧部件在第0行第3列,占8行9列

 self.setCentralWidget(self.main_widget) # 设置窗口主部件
 # 必须在鼠标跟随粒子上层所有的widget加上setMouseTracking(True)才行
 self.main_widget.setMouseTracking(True)
 self.right_widget.setMouseTracking(True)
 self.left_widget.setMouseTracking(True)

 ################################
 self.left_close = QPushButton("") # 关闭按钮
 self.left_visit = QPushButton("") # 空白按钮
 self.left_mini = QPushButton("")  # 最小化按钮
 self.left_close.setObjectName("关闭")
 self.left_visit.setObjectName("最大化")
 self.left_mini .setObjectName("最小化")
 self.left_close.clicked.connect(self.ButtonClick)
 self.left_visit.clicked.connect(self.ButtonClick)
 self.left_mini.clicked.connect(self.ButtonClick)

 self.left_close.setFixedSize(15,15) # 设置关闭按钮的大小
 self.left_visit.setFixedSize(15, 15)  # 设置按钮大小
 self.left_mini.setFixedSize(15, 15) # 设置最小化按钮大小
 self.left_close.setStyleSheet('''QPushButton{background:#F76677;border-radius:5px;}QPushButton:hover{background:red;}''')
 self.left_visit.setStyleSheet('''QPushButton{background:#F7D674;border-radius:5px;}QPushButton:hover{background:yellow;}''')
 self.left_mini.setStyleSheet('''QPushButton{background:#6DDF6D;border-radius:5px;}QPushButton:hover{background:green;}''')

 self.left_label_1 = QPushButton("功能列表")
 self.left_label_1.setObjectName('left_label')
 self.left_label_2 = QPushButton("功能设置")
 self.left_label_2.setObjectName('left_label')
 self.left_label_3 = QPushButton("联系与帮助")
 self.left_label_3.setObjectName('left_label')

 self.left_button_1 = QPushButton(qtawesome.icon('fa.music',color='white'),"我爱我司")
 self.left_button_1.setObjectName('left_button')
 self.left_button_2 = QPushButton(qtawesome.icon('fa.sellsy',color='white'),"准入助手")
 self.left_button_2.setObjectName('left_button')
 self.left_button_3 = QPushButton(qtawesome.icon('fa.film',color='white'),"音乐转换")
 self.left_button_3.setObjectName('left_button')
 self.left_button_4 = QPushButton(qtawesome.icon('fa.home',color='white'),"时间设置")
 self.left_button_4.setObjectName('left_button')
 self.left_button_5 = QPushButton(qtawesome.icon('fa.download',color='white'),"启停管理")
 self.left_button_5.setObjectName('left_button')
 self.left_button_6 = QPushButton(qtawesome.icon('fa.heart',color='white'),"添加项目")
 self.left_button_6.setObjectName('left_button')
 self.left_button_7 = QPushButton(qtawesome.icon('fa.comment',color='white'),"反馈建议")
 self.left_button_7.setObjectName('left_button')
 self.left_button_8 = QPushButton(qtawesome.icon('fa.star',color='white'),"关注我们")
 self.left_button_8.setObjectName('left_button')
 self.left_button_9 = QPushButton(qtawesome.icon('fa.question',color='white'),"遇到问题")
 self.left_button_9.setObjectName('left_button')

 self.left_layout.addWidget(self.left_mini, 0, 0,1,1)
 self.left_layout.addWidget(self.left_close, 0, 2,1,1)
 self.left_layout.addWidget(self.left_visit, 0, 1, 1, 1)

 self.left_layout.addWidget(self.left_label_1,1,0,1,3)
 self.left_layout.addWidget(self.left_button_1, 2, 0,1,3)
 self.left_layout.addWidget(self.left_button_2, 3, 0,1,3)
 self.left_layout.addWidget(self.left_button_3, 4, 0,1,3)
 self.left_layout.addWidget(self.left_label_2, 5, 0,1,3)
 self.left_layout.addWidget(self.left_button_4, 6, 0,1,3)
 self.left_layout.addWidget(self.left_button_5, 7, 0,1,3)
 self.left_layout.addWidget(self.left_button_6, 8, 0,1,3)
 self.left_layout.addWidget(self.left_label_3, 9, 0,1,3)
 self.left_layout.addWidget(self.left_button_7, 10, 0,1,3)
 self.left_layout.addWidget(self.left_button_8, 11, 0,1,3)
 self.left_layout.addWidget(self.left_button_9, 12, 0, 1, 3)

 #############搜索框
 self.right_bar_layout = QGridLayout() # 右侧顶部搜索框网格布局
 self.right_bar_widget = QWidget() # 右侧顶部搜索框部件

 self.search_icon = QLabel(chr(0xf002) + ' '+'搜索  ')
 self.search_icon.setFont(qtawesome.font('fa', 16))
 self.right_bar_widget_search_input = QLineEdit()
 self.right_bar_widget_search_input.setPlaceholderText("输入功能, 回车进行搜索")
 self.right_bar_widget_search_input.setStyleSheet(
 '''QLineEdit{
         border:1px solid gray;
         width:300px;
         border-radius:10px;
         padding:2px 4px;
 }''')

 self.right_bar_layout.addWidget(self.search_icon,0,0,1,1)
 self.right_bar_layout.addWidget(self.right_bar_widget_search_input,0,1,1,8)
 self.right_bar_widget.setLayout(self.right_bar_layout)
 self.right_layout.addWidget(self.right_bar_widget, 0, 0, 1, 4)

 ##################################
 self.right_recommend_label = QLabel("热门推荐")
 self.right_recommend_label.setObjectName('right_lable')

 self.right_recommend_widget = QWidget() # 推荐封面部件
 self.right_recommend_layout = QGridLayout() # 推荐封面网格布局
 self.right_recommend_widget.setLayout(self.right_recommend_layout)
 self.right_recommend_widget.setStyleSheet(
 '''QToolButton{border:none;}
         QToolButton:hover{border-bottom:2px solid #F76677;}
 ''')

 self.recommend_button_1 = QToolButton()
 self.recommend_button_1.setText("打招呼") # 设置按钮文本
 self.recommend_button_1.setIcon(QIcon('Logo.png')) # 设置按钮图标
 self.recommend_button_1.setIconSize(QSize(100,100)) # 设置图标大小
 self.recommend_button_1.setObjectName("打招呼")
 self.recommend_button_1.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) # 设置按钮形式为上图下文
 self.recommend_button_1.clicked.connect(self.ButtonClick)

 self.right_recommend_layout.addWidget(self.recommend_button_1,0,0)


 self.right_layout.addWidget(self.right_recommend_label, 1, 0, 1, 9)
 self.right_layout.addWidget(self.right_recommend_widget, 2, 0, 2, 1)

 ############################
 self.right_newsong_lable = QLabel("拒绝锁屏")
 self.right_newsong_lable.setObjectName('right_lable')

 self.right_newsong_widget = QWidget()  # 最新歌曲部件
 self.right_newsong_layout = QGridLayout() # 最新歌曲部件网格布局
 self.right_newsong_widget.setLayout(self.right_newsong_layout)
 self.right_newsong_widget.setStyleSheet('''
     QPushButton{
         border:none;
         color:gray;
         font-size:12px;
         height:25px;
         padding-left:5px;
         padding-right:5px;
         text-align:center;
     }
     QPushButton:hover{
         color:black;
         border:1px solid #3E80FF;
         border-radius:10px;
     }
 ''')

 self.newsong_button_1 = QPushButton("雨打梨花深闭门")
 self.newsong_button_2 = QPushButton("赏心乐事共谁论")
 self.newsong_button_3 = QPushButton("愁聚眉峰尽日颦")
 self.newsong_button_4 = QPushButton("晓看天色暮看云")
 self.newsong_button_5 = QPushButton("忘了青春误了青春")
 self.newsong_button_6 = QPushButton("花下销魂月下销魂")
 self.newsong_button_7 = QPushButton("千点啼痕万点啼痕")
 self.newsong_button_8 = QPushButton("行也思君坐也思君")
 self.newsong_button_1.setObjectName("雨打")
 self.newsong_button_2.setObjectName("赏心")
 self.newsong_button_3.setObjectName("愁聚")
 self.newsong_button_4.setObjectName("晓看")
 self.newsong_button_5.setObjectName("忘了")
 self.newsong_button_6.setObjectName("花下")
 self.newsong_button_7.setObjectName("千点")
 self.newsong_button_8.setObjectName("行也")
 self.newsong_button_1.setEnabled(True) 
 self.newsong_button_5.setEnabled(False) 
 self.newsong_button_2.setEnabled(True) 
 self.newsong_button_6.setEnabled(False) 
 self.newsong_button_3.setEnabled(True) 
 self.newsong_button_7.setEnabled(False) 
 self.newsong_button_4.setEnabled(True) 
 self.newsong_button_8.setEnabled(False) 
 self.newsong_button_1.clicked.connect(self.ButtonClick)
 self.newsong_button_2.clicked.connect(self.ButtonClick)
 self.newsong_button_3.clicked.connect(self.ButtonClick)
 self.newsong_button_4.clicked.connect(self.ButtonClick)
 self.newsong_button_5.clicked.connect(self.ButtonClick)
 self.newsong_button_6.clicked.connect(self.ButtonClick)
 self.newsong_button_7.clicked.connect(self.ButtonClick)
 self.newsong_button_8.clicked.connect(self.ButtonClick)

 self.right_newsong_layout.addWidget(self.newsong_button_1,0,0,) # 0行0列
 self.right_newsong_layout.addWidget(self.newsong_button_2, 1, 0, )
 self.right_newsong_layout.addWidget(self.newsong_button_3,2,0,) # 0行0列
 self.right_newsong_layout.addWidget(self.newsong_button_4, 3, 0, )
 self.right_newsong_layout.addWidget(self.newsong_button_5,0,1,) # 0行0列
 self.right_newsong_layout.addWidget(self.newsong_button_6, 1, 1, )
 self.right_newsong_layout.addWidget(self.newsong_button_7,2,1,) # 0行0列
 self.right_newsong_layout.addWidget(self.newsong_button_8, 3, 1, )

 self.right_layout.addWidget(self.right_newsong_lable, 4, 0, 1, 2)   # 占一行2列
 self.right_layout.addWidget(self.right_newsong_widget, 5, 0, 1, 2)

 ###########################
 self.right_playlist_lable = QLabel("拒绝省电")
 self.right_playlist_lable.setObjectName('right_lable')
 
 self.right_playlist_widget = QWidget() # 播放歌单部件
 self.right_playlist_layout = QGridLayout() # 播放歌单网格布局
 self.right_playlist_widget.setLayout(self.right_playlist_layout)
 self.right_playlist_widget.setStyleSheet(
     '''
         QToolButton{border:none;}
         QToolButton:hover{border-bottom:2px solid #F76677;}
     ''')

 self.playlist_button_1 = QToolButton()
 self.playlist_button_1.setText("宠物挂件")
 self.playlist_button_1.setIcon(QIcon('Logo.png'))
 self.playlist_button_1.setIconSize(QSize(100, 100))
 self.playlist_button_1.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 self.playlist_button_1.setObjectName("宠物")
 self.playlist_button_1.clicked.connect(self.ButtonClick)

 self.right_playlist_layout.addWidget(self.playlist_button_1,0,0)

 self.right_layout.addWidget(self.right_playlist_lable, 4, 3, 1, 1)
 self.right_layout.addWidget(self.right_playlist_widget, 5, 3, 1, 1)

再加上拒绝锁屏功能,因为这个功能是无限循环的,所以要单独搞一个线程

Desktop = "./"
Picture = "lock.png"
# 拒绝省电功能
class WorkThread(QThread):
   # 写在这里是有讲究的,类外也用到了这个trigger
   trigger = pyqtSignal()

   def __int__(self):
       super(WorkThread, self).__init__()

   def Image_Compare(self, picture):
       #截屏,同时提前准备一张屏幕上会出现的小图bd.png
       img = ImageGrab.grab()
       img.save(Desktop+'screen.png','png')
       #加载原始RGB图像
       img_rgb = cv2.imread(Desktop+"screen.png")
       #创建一个原始图像的灰度版本,所有操作在灰度版本中处理,然后在RGB图像中使用相同坐标还原
       img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

       #加载将要搜索的图像模板
       template = cv2.imread(Desktop+picture,0)
       #使用matchTemplate对原始灰度图像和图像模板进行匹配
       res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
       #设定阈值,0.7应该可以
       threshold = 0.8
       #res大于99.9%
       loc = np.where( res >= threshold)

       flag = 0
       #得到原图像中的坐标
       for pt in zip(*loc[::-1]):
           if(pt[0] and pt[1]):
               # print(pt[0],pt[1])
               pyautogui.click(pt[0],pt[1])
               flag = 1
               break
       if(flag):
           print(picture+"capture succeess!")
       else:
           # print(picture+"capture fail!")
           pass
       
       os.remove(Desktop+'screen.png')

   def run(self):
       while(True):
           try:
               self.Image_Compare(Picture)
           except:  
               print("锁屏了,省电成功,呜呜")
               break
           # 自动锁屏大概时间间隔是5,6分钟,延时时间设为一分钟
           sleep(60*1)
       # 发出信号,如果接受到信号,说明G了,可以将按钮状态修改一下
       self.trigger.emit()

按钮事件,点击按钮的时候,启动功能,点击另一个按钮,关闭线程关闭功能

   # 按钮事件
   def ButtonClick(self):  
       # 左上角三个按钮功能
       if(self.sender().objectName() == "最小化"):
           self.showMinimized()
       elif(self.sender().objectName() == "最大化"):
           if(self.isMaximized()):
               self.resize(870,489)
               self.move(300,300)
           else:
               self.showMaximized()
       elif(self.sender().objectName() == "关闭"):
           self.doClose()

       # 拒绝锁屏功能
       elif(self.sender().objectName() == "雨打"):
           self.neverLockWindows = WorkThread()
           # 增加信号接收器
           self.neverLockWindows.trigger.connect(self.lockWindows)
           self.neverLockWindows.start()
           self.newsong_button_1.setStyleSheet('''QPushButton{ color:black;border:1px solid #F4DED5; border-radius:10px;}''')
           self.newsong_button_1.setEnabled(False) 
           self.newsong_button_5.setEnabled(True) 
       elif(self.sender().objectName() == "忘了"):
           self.neverLockWindows.terminate()
           self.newsong_button_1.setStyleSheet('''
               QPushButton{border:none;color:gray;font-size:12px;height:25px;padding-left:5px;padding-right:5px;text-align:center;}
               QPushButton:hover{color:black;border:1px solid #3E80FF;border-radius:10px;}
           ''')
           self.newsong_button_1.setEnabled(True) 
           self.newsong_button_5.setEnabled(False) 

main函数:

if __name__ == '__main__':
   import sys
   import cgitb
   cgitb.enable(1, None, 5, '')

   app = QApplication(sys.argv)

   QApplication.setQuitOnLastWindowClosed(False)
   w = Window()
   w.show()
   sys.exit(app.exec_())

二、运行效果

左侧的功能目前只是个摆设,暂时没那么多功能需要添加,主要看右边,在热门推荐里面放了一个“”打招呼“”的按钮,在拒绝省电里面放了一个“”宠物挂件“”的按钮,并通过QSS美化,后续会实现具体的功能。
左上角加了三个按钮,最小化、最大化、关闭,额外增加这三个是因为当设置背景透明,无边框的时候(用于靠边隐藏),原先的右上角的最小最多关闭按钮会消失
在这里插入图片描述
对于拒绝锁屏下面的几个按钮,随便找了首诗,搞的文艺一点。
我设计的是:每一行对应一个功能,左侧按钮按下表示启动功能,右侧按下表示关闭功能。
默认状态是:左侧按钮可以点击,右侧无法点击
QSS美化:点击之后是一个状态(粉色),关闭之后是一个状态(黑色),鼠标悬浮式一个状态(蓝色)
在这里插入图片描述

总体效果:
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值