gpt4 book ai didi

python - 如何在 PyQt5 中使用 slider 旋转图像,同时保持图像居中?

转载 作者:行者123 更新时间:2023-12-03 07:57:44 29 4
gpt4 key购买 nike

我想使用 PyQt5 将图像 (dial.png) 旋转一度,而不将其从中心位置移动。然而,尽管我尽了最大努力,图像并没有保持在中心位置,并且在旋转时会困惑地移动。每次 QSlider 值改变时,图像应旋转一度。

下面是我当前用于创建包含要旋转的图像的标签的代码:

        self.dial = QtWidgets.QLabel(self.centralwidget)
self.dial.setGeometry(QtCore.QRect(150, 50, 500, 440))
self.dial.setText("")
self.dial.setObjectName("dial")
self.pixmap = QtGui.QPixmap("dial.png")
self.dial.setPixmap(self.pixmap)

我一直无法找到满足我要求的解决方案。任何想法或起点将不胜感激。

        MainWindow.setObjectName("MainWindow")
MainWindow.resize(920, 620)
MainWindow.setMinimumSize(QtCore.QSize(920, 620))
MainWindow.setMaximumSize(QtCore.QSize(920, 620))

这是dial.png:

dial.png

最佳答案

我不确定你的代码是否正确,但即使是正确的,你提到的困惑运动仍然存在,因为 PNG 图像没有正确居中,如下所示:

cp

当然,计算机程序会利用图像的实际中心,而不是人类逻辑的中心。

话虽这么说,这是完成您所要求的一种可能的方法,包括足够的注释来帮助您理解逻辑:

from PyQt5.QtGui import QPixmap, QTransform
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QSlider, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt

# Define a class that inherits from QMainWindow
class ImageRotator(QMainWindow):
def __init__(self, image_path):
# Call the __init__ method of the QMainWindow class to initialize the basic functionality of the main window
super().__init__()

# Load the image from the given image path
self.image = QPixmap(image_path)

# Create a QLabel to display the image
self.label = QLabel()
self.label.setPixmap(self.image)
# Set the alignment of the label to center
self.label.setAlignment(Qt.AlignCenter)

# Create a QSlider to control the rotation
self.slider = QSlider(Qt.Horizontal)
# Set the minimum and maximum values of the slider to 0 and 360 degrees, respectively
self.slider.setMinimum(0)
self.slider.setMaximum(360)
# Set the initial value of the slider to 0 degrees
self.slider.setValue(0)

# Connect the valueChanged signal of the slider to the rotate_image method
self.slider.valueChanged.connect(self.rotate_image)

# Create a QVBoxLayout to hold the QLabel and QSlider
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.slider)

# Create a QWidget to hold the QVBoxLayout
widget = QWidget()
widget.setLayout(layout)
# Set the QWidget as the central widget of the QMainWindow
self.setCentralWidget(widget)

# Show the QMainWindow
self.show()

def rotate_image(self, angle):
# Create a QTransform object that will rotate the image around its center point by the given angle
transform = QTransform().rotate(angle)
# Apply the transformation to the image and create a new QPixmap object
rotated_image = self.image.transformed(transform, Qt.SmoothTransformation)
# Set the rotated image as the pixmap of the QLabel
self.label.setPixmap(rotated_image)

# Create a QApplication object
app = QApplication([])
# Create an instance of the ImageRotator class with the "dial.png" image file
window = ImageRotator("dial.png")
# Start the QApplication
app.exec_()

关于python - 如何在 PyQt5 中使用 slider 旋转图像,同时保持图像居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75635358/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com