实例
- 创建基于Dialog without Buttons模板窗口
- 添加三个Label部件,其text为Displays the x,y coordinates where mouse is pressed and released
- 设定第二个Label部件的objectName为labelPress
- 设定第三个Label部件的objectName为labelRelease
- 保存为demoMouseClicks.ui
- 使用pyuic生成demoMouseClicks.py
- 创建callMouseClickCoordinates.py
import sys
from PyQt5.QtWidgets import QDialog,QApplication
from cookbook_200505.demoMouseClicks import *
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui=Ui_Dialog()
self.ui.setupUi(self)
self.show()
def mousePressEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
x=event.x()
y=event.y()
text="x: {0}, y: {1}".format(x,y)
self.ui.labelRelease.setText("Mouse button pressed at "+text)
def mouseReleaseEvent(self, event):
x = event.x()
y = event.y()
text = "x: {0}, y: {1}".format(x, y)
self.ui.labelRelease.setText("Mouse button relrased at " + text)
self.update()
if __name__=="__main__":
app=QApplication(sys.argv)
w=MyForm()
w.show()
sys.exit(app.exec())