凌的博客

您现在的位置是:首页  > 学无止境 > python

python

PyQt5 窗口置顶

2019-09-25python0

1.png

self.setWindowFlags(Qt.WindowStaysOnTopHint)

附:获取坐标

listen.py

from pynput.mouse import Listener as MouseListener
from pynput.keyboard import Listener as KeyboardListener

class Listen:

    def __init__(self):
        pass

    def mouseMove(self, x,y):
        self.triggerMove.emit("{},{}".format(x,y))

    def mouseClick(self, x, y, button, pressed):
        self.triggerClick.emit("{},{}".format(x, y))

    def mouseListen(self):
        with MouseListener(on_move=self.mouseMove, on_click=self.mouseClick) as listerner:
            listerner.join()

if __name__ == "__main__":
    rec = Listen()
    # rec.mouseListen()
    rec.mouseClickPos()

mpos.py

import sys
from PyQt5.QtWidgets import QWidget, QMainWindow, QApplication, QDesktopWidget, QPushButton, QVBoxLayout, QHBoxLayout, \
    QLineEdit
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QObject

from lib.Listen import Listen

class CombWidget(QObject):
    def __init__(self):
        super().__init__()

    def combHBox(self, lst: list):
        h = QHBoxLayout()
        for x, y in lst:
            h.addWidget(x, y)
        wg = QWidget()
        wg.setLayout(h)
        return wg

    def combVBox(self, lst: list):
        h = QVBoxLayout()
        for x, y in lst:
            h.addWidget(x, y)
        wg = QWidget()
        wg.setLayout(h)
        return wg

class MouseThread(QThread):
    clickPos = pyqtSignal(str)
    movePos = pyqtSignal(str)
    def run(self):
        print("mouse thread init")
        self.mouseListener.triggerClick = self.clickPos
        self.mouseListener.triggerMove = self.movePos
        self.mouseListener.mouseListen()



class Mpos(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        comb = CombWidget()

        v = QVBoxLayout()
        self.pos_btn = QPushButton("获取坐标")
        self.pos_btn.clicked.connect(self.getPos)
        self.pos_txt = QLineEdit("")
        self.pos_txt.setReadOnly(True)
        self.movepos_txt = QLineEdit("")
        self.movepos_txt.setReadOnly(True)
        v.addWidget(comb.combHBox([(self.pos_btn, 2), (self.pos_txt, 2), (self.movepos_txt, 2)]), 2)
        # v.addWidget(QTextEdit(""), 22)

        wg = QWidget()
        wg.setLayout(v)
        self.setCentralWidget(wg)


        self.mouseThread = MouseThread()
        self.mouseThread.clickPos.connect(self.clickPos)
        self.mouseThread.movePos.connect(self.movePos)

        self.mouseThread.mouseListener = self.mouseListener = Listen()



        self.setGeometry(300, 300, 300, 100)
        self.setWindowTitle("坐标/点击/移动")
        self.center()

    def getPos(self):
        self.mouseThread.start()

    def clickPos(self,pos):
        self.pos_txt.setText(pos)

    def movePos(self,pos):
        self.movepos_txt.setText(pos)

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mpos = Mpos()
    mpos.show()
    sys.exit(app.exec_())


如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论