Skip to content

Instantly share code, notes, and snippets.

@Tekunogosu
Created March 9, 2023 20:05
Show Gist options
  • Save Tekunogosu/4aba898bbd6c318102e913239cba052d to your computer and use it in GitHub Desktop.
Save Tekunogosu/4aba898bbd6c318102e913239cba052d to your computer and use it in GitHub Desktop.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, QLabel
from PyQt5.QtGui import QCursor, QKeyEvent
from PyQt5.QtCore import Qt, QEvent
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.delaylabel = QLabel('Delay:', self)
self.delaylabel.move(20, 20)
self.delaytextbox = QLineEdit(self)
self.delaytextbox.move(80, 20)
self.delaytextbox.resize(40, 20)
self.inputlabel = QLabel('Input:', self)
self.inputlabel.move(20, 50)
self.inputtextbox = QLineEdit(self)
self.inputtextbox.move(80, 50)
self.inputtextbox.resize(40, 20)
self.inputbutton = QPushButton('Grab Input', self)
self.inputbutton.move(130, 50)
self.inputbutton.clicked.connect(self.grabInput)
self.textbox = QLineEdit(self)
self.textbox.move(20, 80)
self.textbox.resize(200, 20)
self.button = QPushButton('Grab Coords', self)
self.button.move(20, 110)
self.button.clicked.connect(self.grabCoords)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Mouse Coordinates')
self.show()
def grabCoords(self):
self.setWindowOpacity(0.25)
self.setWindowState(Qt.WindowFullScreen)
QApplication.instance().installEventFilter(self)
def grabInput(self):
self.inputtextbox.installEventFilter(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.MouseButtonPress:
x = QCursor.pos().x()
y = QCursor.pos().y()
self.textbox.setText('x: ' + str(x) + ', y: ' + str(y))
self.setWindowState(Qt.WindowNoState)
self.setWindowOpacity(1.0)
self.setGeometry(300, 300, 250, 150)
QApplication.instance().removeEventFilter(self)
return True
return super().eventFilter(obj, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment