im使用QPixmap加载图像并设置位置。
在我的主窗口中加载图像,但图像的位置不是我使用的设置setPos
,但没有发生任何事情
from PyQt5 import QtGui
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel
from PyQt5 import QtCore
import sys
from PyQt5.QtGui import QPixmap
class Window(QDialog):
def __init__(self):
super().__init__()
self.title = "PyQt5 Adding Image To Label"
self.top = 200
self.left = 500
self.width = 400
self.height = 500
self.InitWindow()
def InitWindow(self):
self.setWindowIcon(QtGui.QIcon("icon.png"))
self.setWindowTitle(self.title)
self.setStyleSheet("background-color:#202020")
self.setGeometry(self.left, self.top, self.width, self.height)
vbox = QVBoxLayout()
labelImage = QLabel(self)
pixmap = QPixmap("mario.png")
pixmap=pixmap.scaled(50, 50, QtCore.Qt.KeepAspectRatio)
#pixmap.setPos(100,60)
labelImage.setPixmap(pixmap)
vbox.addWidget(labelImage)
self.setLayout(vbox)
self.show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())
必须明确以下概念:
QPixmap不是视觉元素,而是组成图像的位的容器,因此它们没有任何
setPos()
方法(建议:check the Qt docs)在代码中显示QPixmap内容的可视元素是QLabel,因此必须为该小部件设置位置,但在这种情况下,OP使用的布局旨在管理几何体(位置和大小),因此如果要手动设置,则不应使用它
相关问题 更多 >
编程相关推荐