python - 如何在水平布局中调整小部件的大小

标签 python pyqt pyqt5 qlayout

当我放大 GUI 窗口时,我希望所有小部件的长度(水平)发生变化。现在,当我增大窗口时,只有行编辑会更改大小。最重要的是,我希望我的按钮和组合框更长。

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QGridLayout, QWidget, QListWidget, QLineEdit, QHBoxLayout, QPushButton


class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        self.Search_Bar = QLineEdit(placeholderText="Search")
        self.Button = QPushButton('button')
        layout = QHBoxLayout(centralWidget)
        layout.addWidget(self.Button)
        layout.addWidget(self.Search_Bar)



if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

最佳答案

有两种可能的解决方案:

  • 通过将小部件添加到布局来设置相同的拉伸(stretch):

    layout.addWidget(self.Button, <b>stretch=1</b>)
    layout.addWidget(self.Search_Bar, <b>stretch=1</b>)
  • 设置QSizePolicy::ExpandingsizePolicy 的水平分量上按钮:

    sp = self.Button.sizePolicy()
    sp.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
    self.Button.setSizePolicy(sp)

关于python - 如何在水平布局中调整小部件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60495523/

相关文章:

python - 如何从 pandas python 中另一个数据帧的子集交集减去一个数据帧?

python - 将代码从 Python 缓冲区发送到从 M-x ansi-term 运行的 IPython session

python - 如何在数据更新时自动刷新pyqt5表

python - 如何将 .ui 文件转换为 .py 文件

python - 如何制作动态组合框PYQT5

python - PyQt点击图像的不同部分有各自的功能

Python闰年计算器(用户选择的两年之间)

python - 如果否则无法在 python Pandas 中使用日期时间

python - 如何在Qt样式表中设置相对路径?

pyqt - 如何在 QScrollArea 中为 QScrollBar 设置样式表?