简体   繁体   English

如何使小部件随PyQt5中的窗口缩放?

[英]How to make widgets scale with the window in PyQt5?

I'm trying to set up a GUI which will include multiple pages in PyQt5. 我正在尝试设置一个GUI,它将在PyQt5中包含多个页面。 The window will have a minimum size of 800x600, but is customisable beyond that. 窗口的最小尺寸为800x600,但可以自定义。 I want most, but not all, elements on the window to scale along with it. 我希望窗口中的大多数(但不是全部)元素能够随其缩放。 I have a solution already, but I feel that it is not very elegant. 我已经有一个解决方案,但是我觉得它不是很优雅。

Here is an example of the window at 800x600: 这是800x600窗口的示例:

在此处输入图片说明

And here's another example after scaling 这是缩放后的另一个示例

在此处输入图片说明

I've tried using QVBoxLayout, but with that system, I can't manage to keep the layout as it is here (not to say that it's impossible), rather the widgets all become centred and of the same width. 我尝试使用QVBoxLayout,但是对于该系统,我无法设法保持此处的布局(并不是说这是不可能的),而是所有小部件都变得居中且宽度相同。 At a later date, I might also be looking at adding widgets to the side that will be at the same y-value as some of the existing widgets, which is another thing that I'm not sure on how to do with the box layout. 稍后,我可能还会考虑在与某些现有小部件的y值相同的一侧添加小部件,这是我不确定如何处理盒子布局的另一件事。

Here's the relevant code: 以下是相关代码:

class CreatePage(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.homeBtn = QPushButton("Home", self)
        self.homeBtn.move(10, 10)

        self.frontLabel = QLabel("Front", self)
        self.frontLabel.setFont(QFont("Decorative", 20))

        self.frontEdit = QTextEdit(self)
        self.frontEdit.setFont(QFont("Decorative", 11))

        self.backLabel = QLabel("Back", self)
        self.backLabel.setFont(QFont("Decorative", 20))

        self.backEdit = QTextEdit(self)
        self.backEdit.setFont(QFont("Decorative", 11))

    def paintEvent(self, e):
        qp = QPainter()
        qp.setFont(QFont("Decorative", 20))

        size = self.size()
        h = size.height()
        w = size.width()

        frontW = qp.fontMetrics().width("Front")
        self.frontLabel.move((w/2) - (frontW/2) , h/15)
        #I use fontMetrics to determine the width of the text
        #I then use this information to centre the text

        self.frontEdit.move(50, h/15 + 40)
        self.frontEdit.resize(w-100, h/3)

        backW = qp.fontMetrics().width("Back")
        self.backLabel.move((w/2) - (backW/2), h/2)

        self.backEdit.move(50, h/2 + 40)
        self.backEdit.resize(w-100, h/3)

Apologies for any general sloppiness, I am new to PyQt and to GUI programming on the whole. 对于任何一般的草率,我深表歉意,我是PyQt和GUI编程的新手。 Anyway, the formulas I've used for resizing and moving widgets are quite arbitrary. 无论如何,我用于调整大小和移动小部件的公式是相当随意的。 Does anyone know a better way of achieving this effect? 有谁知道实现此效果的更好方法?

Try it: 试试吧:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore    import *
from PyQt5.QtGui     import *

class CreatePage(QWidget):
    def __init__(self, parent=None):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.homeBtn = QPushButton("Home") 

        self.frontLabel = QLabel("Front") 
        self.frontLabel.setFont(QFont("Decorative", 20))
        self.frontEdit = QTextEdit(placeholderText="frontEdit") 
        self.frontEdit.setFont(QFont("Decorative", 11))

        self.backLabel = QLabel("Back") 
        self.backLabel.setFont(QFont("Decorative", 20))
        self.backEdit = QTextEdit(placeholderText="backEdit") 
        self.backEdit.setFont(QFont("Decorative", 11))

        grid = QGridLayout()
        grid.addWidget(self.homeBtn,    0, 0, alignment=Qt.AlignTop | Qt.AlignLeft)
        grid.addWidget(self.frontLabel, 1, 0, alignment=Qt.AlignCenter)
        grid.addWidget(self.frontEdit,  2, 0)
        grid.addWidget(self.backLabel,  3, 0, alignment=Qt.AlignCenter)
        grid.addWidget(self.backEdit,   4, 0)

        self.setLayout(grid)

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM