要在PySide6中添加同一文件夹下的图片和文本,你可以使用QLabel
和QPixmap
来显示图片,以及使用QTextEdit
或QPlainTextEdit
来显示文本。
以下是一个示例代码,展示了如何添加图片和文本:
import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QTextEdit
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Image and Text Example")
self.setFixedSize(600, 400)
image_label = QLabel(self)
image_label.setGeometry(50, 50, 200, 200)
# 添加图片路径
image_path = "./image.jpg"
pixmap = QPixmap(image_path)
image_label.setPixmap(pixmap.scaled(200, 200, Qt.AspectRatioMode.KeepAspectRatio))
text_edit = QTextEdit(self)
text_edit.setGeometry(300, 50, 250, 300)
# 添加文本内容
text_content = """
这里是文本内容。
您可以在这里输入并显示多行文本。
"""
text_edit.setText(text_content)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
在这个示例代码中,我们首先创建一个窗口,并使用QLabel
在指定位置添加了一个固定大小的标签用于显示图片。然后通过设置图像路径,在该标签上加载并显示该图像。
接下来,我们使用QTextEdit
在指定位置添加了一个文本编辑框,并通过设置文本内容,在该编辑框中显示多行文本。
请注意,你需要替换示例代码中的图片路径和文本内容为你自己的实际路径和内容。
希望这个示例对你有所帮助!
内容由零声教学AI助手提供,问题来源于学员提问