【发布时间】:2021-11-07 04:16:45
【问题描述】:
我正在尝试使用Pyqt5 显示多个图像。最好在 GUI 中使图像可选择,以便用户可以立即轻松地选择和复制该图像。
“可选择”是指用户可以右键单击图像,然后将其复制,然后可能将其粘贴到 GUI 之外的其他位置。就像保存在 Word 中的普通图像一样。用户可以在 Word 中选择/复制图像,然后将其粘贴到其他位置。
我知道Qlabel 中的文本可以通过使用self.my_label.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse) 轻松实现。但是,对于图像,似乎没有这样的方法来处理它。有什么办法可以解决图片问题?
import sys
import PyQt5
from PyQt5.QtWidgets import (
QLabel,
QVBoxLayout,
QWidget
)
from PyQt5 import QtCore
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QSize
class Display_Window(QWidget):
def __init__(self):
super().__init__()
self.setMinimumSize(QSize(980,700))
self.layout = QVBoxLayout(self)
self.label1 = QLabel(self)
self.pixmap = QPixmap(path_to_my_image)
self.pixmap = self.pixmap.scaled(900, 900, QtCore.Qt.KeepAspectRatio)
self.label1.setPixmap(self.pixmap)
self.label1.resize(self.pixmap.width(), self.pixmap.height())
# Run if Script
if __name__ == "__main__":
app = PyQt5.QtWidgets.QApplication(sys.argv)
MainWindow = Display_Window() # Initialize GUI
MainWindow.show() # Show Window
app.exec_()
【问题讨论】:
-
你能解释一下你所说的“可选择”是什么意思吗?您想用鼠标选择图像的部分吗?
-
谢谢@musicamante。可选择的意思是用户可以右键单击图像,然后将其复制,然后可能将其粘贴到 GUI 之外的其他位置。就像保存在 Word 中的普通图像一样。
标签: python pyqt5 qlabel qpixmap