Python PYQT检测小部件上的鼠标位置
我试图在单击小部件时获取鼠标位置。似乎有多种方法可以做到这一点。鼠标下功能似乎是最流行的功能。我不知道我做错了什么。我尝试了所有可能的技巧。下面是我的python代码,除此之外我还有一个ui文件Python PYQT检测小部件上的鼠标位置,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我试图在单击小部件时获取鼠标位置。似乎有多种方法可以做到这一点。鼠标下功能似乎是最流行的功能。我不知道我做错了什么。我尝试了所有可能的技巧。下面是我的python代码,除此之外我还有一个ui文件 from PyQt5 import QtWidgets, uic, QtCore, QtGui from PyQt5.QtWidgets import * from PyQt5.QtGui import QPixmap, QMovie import pyqtgraph as qwt_plot impor
from PyQt5 import QtWidgets, uic, QtCore, QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QPixmap, QMovie
import pyqtgraph as qwt_plot
import sys
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__() # Call the inherited classes __init__ method
uic.loadUi('stupid.ui', self) # Load the .ui file
self.show() # Show the GUI
self.frame = None
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.run)
self.track = QtGui.QWidget(self.widget)
self.setMouseTracking(True)
self.track.mouseReleaseEvent = self.gig
def gig(self, event):
print ("Method 1 worked")
def mouseReleaseEvent(self, event):
posMouse = event.pos()
#print("%d, %d" % (posMouse.x(), posMouse.y()))
#print (self.track.geometry())
if self.track.geometry().contains(posMouse):
print "Under Mouse"
if self.track.underMouse():
print ("phew")
def run(self):
# Do nothing
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv) # Create an instance of QtWidgets.QApplication
window = Ui() # Create an instance of our class
window.setWindowTitle('Mouse Position GUI')
app.exec_() # Start the application
用户界面文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>932</width>
<height>546</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>icon.png</normaloff>icon.png</iconset>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>110</x>
<y>0</y>
<width>631</width>
<height>511</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>932</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
主窗口
0
0
932
546
主窗口
icon.pngicon.png
110
0
631
511
0
0
932
22
如果要检测何时单击,则必须使用eventfilter,不应将函数分配给方法,因为该方法可能会失败
导入系统
从PyQt5导入QtWidgets、uic、QtCore、QtGui
类Ui(QtWidgets.QMainWindow):
定义初始化(自):
超级(用户界面,自我)。\uuuu初始化
uic.loadUi(“愚蠢的.ui”,self)
self.show()
self.widget.installEventFilter(self)
def事件过滤器(自身、obj、事件):
如果self.widget是obj:
如果event.type()==QtCore.QEvent.mousebutton按下:
打印(self.widget,“按”)
elif event.type()==QtCore.QEvent.MouseButtonRelease:
打印(self.widget,“发布”)
返回super(Ui,self).eventFilter(obj,event)
如果名称=“\uuuuu main\uuuuuuuu”:
app=qtwidts.QApplication(sys.argv)
window=Ui()
setWindowTitle(“鼠标位置GUI”)
app.exec()
``
请更加小心使用和缩进,并始终检查预览和发布的结果。它显示错误,然后我更改了self.widget.installEventFilter(self.widget)。即使现在什么也没有发生。@user3141066您得到了什么错误?我已经测试过了,它工作正常,不要在我的代码中添加更多的代码,如果你添加了它,它就不再是我的责任了。我得到这个错误:TypeError:super()至少接受一个参数(0给定)@user3141066如果你没有提供python版本信息,那么我假设你使用的是最新版本,很明显,语法会随着版本的不同而变化。试试我更新的代码不知道你用了什么魔法,但现在它工作得很好。谢谢!