from PySide6 import QtWidgets,QtCore,QtGui
#定义图片大小
WIDTH = 200
HEIGHT = 200
class Ui(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.resize(WIDTH,HEIGHT)
self.setPalette(QtGui.QPalette(QtGui.Qt.green))
self.setAutoFillBackground(True)
# 默认角度
self.angle = 0
#实例化定时器
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.timeTimeOut)
def timeTimeOut(self):
self.update()
def start(self):
self.timer.start(100)
def stop(self):
self.timer.stop()
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing,True)
self.angle+=5
if self.angle == 360:
self.angle = 0
image = QtGui.QPixmap()
image.load("./mm.jpg")
size = QtCore.QSize(WIDTH,HEIGHT)
scaleImage = image.scaled(size)
#计算窗口中心位置
windowCenterX = self.width()/2
windowCenterY = self.height()/2
#计算图片中心
imageCenterX = scaleImage.width()/2
imageCenterY = scaleImage.height()/2
#平移距离
translateX = windowCenterX - imageCenterX
translateY = windowCenterY - imageCenterY
recf = QtCore.QRect(translateX,translateY,scaleImage.width(),scaleImage.height())
#设置旋转参考点为窗口
painter.translate(windowCenterX,windowCenterY)
painter.rotate(self.angle)
#将参考点设置回图片
painter.translate(-windowCenterX,-windowCenterY)
painter.drawPixmap(recf,scaleImage,scaleImage.rect())
#再画一个矩形
pen = QtGui.QPen()
pen.setColor(QtGui.Qt.red)
pen.setWidth(2)
painter.drawRect(recf)
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.resize(1500,900)
self.motor = Ui()
self.motor.move(20,20)
self.motor.setParent(self)
self.motor2 = Ui()
self.motor2.move(500,20)
self.motor2.setParent(self)
self.btn = QtWidgets.QPushButton('start',self)
self.btn.move(50,800)
self.btn2 = QtWidgets.QPushButton("STOP",self)
self.btn2.move(200,800)
self.btn.clicked.connect(self.btn_click)
self.btn2.clicked.connect(self.btn2_click)
def btn_click(self):
self.motor.start()
self.motor2.start()
def btn2_click(self):
self.motor.stop()
self.motor2.stop()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())