PyQt小部件在QVBoxLayout中的绝对位置

时间:2019-02-24 12:54:09

标签: python user-interface pyqt5

我正在从事一些PyQt5项目,我来自Web开发,并且坚持执行一些琐碎的任务。 我有一些QVBoxLayout,并且在其中放置了QWidget只是为了通过CSS添加一些背景色。之后,我想放一些图片, self.img  就在QWidget的中心。但是我无法做到这一点。 它只是将我的Image呈现在QWidget下面。

我尝试使用 move(x,y)方法,尝试对QWidget使用背景图片,但失败了。所以我真的坚持了。

我试图寻找一些可能的方法来解决它,但是我没有找到任何可以帮助我的东西。 如果有人可以帮助我,我将不胜感激。感谢您的帮助。

说实话-我是PyQt的新手。抱歉,我问了一个假的问题,但我确实需要帮助。

这是我的代码

import sys

from PyQt5.QtCore import QPoint
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QPixmap



class MainWindow(QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.layout  = QVBoxLayout()
        self.layout.addWidget(TopContentBlock(self))
        self.layout.addWidget(BottomContentBlock(self))
        self.setLayout(self.layout)
        self.layout.setContentsMargins(0,0,0,0)
        self.layout.setSpacing(0)
        self.layout.addStretch(-1)
        self.setMinimumSize(640,400)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.pressing = False


class TopContentBlock(QWidget):

    def __init__(self, parent):
        super(TopContentBlock, self).__init__();
        self.parent = parent;
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.content = QWidget()
        self.content.setFixedSize(640, 250)
        self.content.setStyleSheet("""
         background-color: #67BEC3;
        """)

        self.img = QLabel()
        pixmap = QPixmap('main_illustration.png')
        self.img.setPixmap(pixmap)
        print(pixmap.width(), pixmap.height())
        self.img.resize(pixmap.width(), pixmap.height())
        #self.img.setFixedSize(pixmap.width(), pixmap.height())

        self.layout.addWidget(self.img)
        self.layout.addWidget(self.content)
        self.setLayout(self.layout)


class BottomContentBlock(QWidget):

    def __init__(self, parent):
        super(BottomContentBlock, self).__init__();
        self.parent = parent;
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.content = QWidget()
        self.content.setFixedSize(640, 400)
        self.content.setStyleSheet("""
            background-color: cyan;
        """)
        self.layout.addWidget(self.content)
        self.setLayout(self.layout)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())


import resources_new

1 个答案:

答案 0 :(得分:1)

尝试一下:

import sys
from PyQt5.QtCore    import QPoint, Qt
from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QLabel,
                             QPushButton, QVBoxLayout, QWidget)
from PyQt5.QtGui     import QPixmap


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.layout  = QVBoxLayout()
        self.layout.addWidget(TopContentBlock(self))
        self.layout.addWidget(BottomContentBlock(self))
        self.setLayout(self.layout)
        self.layout.setContentsMargins(0,0,0,0)
        self.layout.setSpacing(0)
#        self.layout.addStretch(1)
        self.setMinimumSize(640,600)                         # 600
        self.setWindowFlags(Qt.FramelessWindowHint)
#?        self.pressing = False


class TopContentBlock(QWidget):
    def __init__(self, parent):
        super(TopContentBlock, self).__init__();
#?        self.parent = parent;
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.content = QWidget()
        self.content.setFixedSize(640, 200)                  #(640, 250)
        self.content.setStyleSheet("""
         background-color: #67BEC3;
        """)

        self.img = QLabel()
        self.img.setAlignment(Qt.AlignCenter)                # +++

        pixmap = QPixmap('im.png')                           #('main_illustration.png')
#        self.img.setPixmap(pixmap)
        self.img.setPixmap(pixmap.scaled(200, 200,           # +++
                                         Qt.IgnoreAspectRatio, 
                                         Qt.FastTransformation))
#        self.img.resize(pixmap.width(), pixmap.height())
        #self.img.setFixedSize(pixmap.width(), pixmap.height())

        self.layout.addWidget(self.img)
        self.layout.addWidget(self.content)
        self.layout.setSpacing(0)                            # +++
        self.setLayout(self.layout)


class BottomContentBlock(QWidget):
    def __init__(self, parent):
        super(BottomContentBlock, self).__init__();
        self.parent = parent;
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.content = QWidget()
        self.content.setFixedSize(640, 200)               #(640, 400)
        self.content.setStyleSheet("""
            background-color: cyan;
        """)
        self.layout.addWidget(self.content)
        self.setLayout(self.layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

enter image description here


更新

import sys
from PyQt5.QtCore    import QPoint, Qt
from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QLabel,
                             QPushButton, QVBoxLayout, QWidget)
from PyQt5.QtGui     import QPixmap


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.layout  = QVBoxLayout()
        self.layout.addWidget(TopContentBlock(self))
        self.layout.addWidget(BottomContentBlock(self))
        self.setLayout(self.layout)
        self.layout.setContentsMargins(0,0,0,0)
        self.layout.setSpacing(0)
#        self.layout.addStretch(1)
        self.setMinimumSize(640,400)                         # 400
        self.setWindowFlags(Qt.FramelessWindowHint)
#?        self.pressing = False


class TopContentBlock(QWidget):
    def __init__(self, parent):
        super(TopContentBlock, self).__init__();
#?        self.parent = parent;
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

#        self.content = QWidget()                            # --- <---
        self.setFixedSize(640, 200)                          # --- .content              
        self.setStyleSheet("""                               
         background-color: #67BEC3;
        """)                                                 # --- .content 

        self.img = QLabel()
        self.img.setAlignment(Qt.AlignCenter)                # +++

        pixmap = QPixmap('im.png')                           #('main_illustration.png')
#        self.img.setPixmap(pixmap)
        self.img.setPixmap(pixmap.scaled(200, 200,           # +++
                                         Qt.IgnoreAspectRatio, 
                                         Qt.FastTransformation))
#        self.img.resize(pixmap.width(), pixmap.height())
        #self.img.setFixedSize(pixmap.width(), pixmap.height())

        self.layout.addWidget(self.img)
#        self.layout.addWidget(self.content)                 # ---
        self.layout.setSpacing(0)                            # +++
        self.setLayout(self.layout)


class BottomContentBlock(QWidget):
    def __init__(self, parent):
        super(BottomContentBlock, self).__init__();
        self.parent = parent;
        self.layout = QVBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)

        self.content = QWidget()
        self.content.setFixedSize(640, 200)               #(640, 400)
        self.content.setStyleSheet("""
            background-color: cyan;
        """)
        self.layout.addWidget(self.content)
        self.setLayout(self.layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

enter image description here