PyQT5 正确的布局对齐

Posted

技术标签:

【中文标题】PyQT5 正确的布局对齐【英文标题】:PyQT5 proper layout alignment 【发布时间】:2018-04-21 18:42:34 【问题描述】:

我的PyQt5 GUI 有以下代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QMainWindow, QGridLayout, QLayout, QTableWidget, QHeaderView, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QComboBox
from PyQt5.QtCore import QDate, Qt
from datetime import date, datetime


class CalendarWindow(QMainWindow):
    def __init__(self, parent=None):
        super(CalendarWindow, self).__init__(parent)
        self.setUpUI()

    def setUpUI(self):
        self.setGeometry(50, 50, 1200, 700)
        # Labels
        self.label_events = QLabel(self)
        self.label_new_event = QLabel(self)
        self.label_event_name = QLabel(self)
        self.label_start_date = QLabel(self)
        self.label_repeat = QLabel(self)
        self.label_description = QLabel(self)
        # Labels parameters
        self.label_events.setAlignment(Qt.AlignCenter)
        self.label_events.setText("Wydarzenia danego dnia:")

        # self.label_new_event.setAlignment(Qt.AlignCenter)
        self.label_new_event.setText("Dodaj event:")

        self.label_event_name.setText("Nazwa eventu:")
        self.label_start_date.setText("Data eventu:")
        self.label_repeat.setText("Powtarzalność eventu:")
        self.label_description.setText("Opis:")
        # TextEdits
        self.text_event_name = QLineEdit(self)
        self.text_start_date = QLineEdit(self)
        self.text_description = QLineEdit(self)
        # TextEdits parameters

        # ComboBoxes
        self.combo_repeat = QComboBox(self)
        # ComboBoxes parameters
        self.combo_repeat.addItems(
            ["No", "Day", "Week", "Two weeks", "Month", "Half a year", "Year"])

        # Widgets
        self.setWindowTitle("Python Calendar App")
        self.calendar_widget = QCalendarWidget(self)
        # User date select event
        self.calendar_widget.clicked[QDate].connect(self.dateChanged)
        self.events_list_widget = QTableWidget(self)
        self.events_list_widget.setColumnCount(5)
        self.events_list_widget.setHorizontalHeaderLabels(
            ["ID", "Name", "Date", "Repetition", "Description"])
        # Columns stretched and fit
        self.events_list_widget.horizontalHeader().setSectionResizeMode(
            QHeaderView.Stretch)
        self.main_widget = QWidget(self)
        self.main_layout = QGridLayout(self.main_widget)
        self.main_layout.sizeConstraint = QLayout.SetDefaultConstraint
        self.main_layout.addWidget(self.calendar_widget, 0, 0)
        self.init_sublayouts()
        self.main_layout.addLayout(self.sublayout_events, 0, 1)
        self.main_layout.addLayout(self.sublayout_new_event, 0, 2)
        self.main_layout.setColumnStretch(0, 1)
        self.main_layout.setColumnStretch(1, 3)
        self.main_layout.setColumnStretch(2, 2)
        self.main_widget.setLayout(self.main_layout)
        self.setCentralWidget(self.main_widget)

    def init_sublayouts(self):
        # Events sublayout
        self.sublayout_events = QVBoxLayout()
        self.sublayout_events.addWidget(self.label_events)
        self.sublayout_events.addWidget(self.events_list_widget)
        # New event sublayout
        self.sublayout_new_event = QVBoxLayout()
        self.sublayout_new_event.addWidget(self.label_new_event)
        self.sublayout_new_event_grid = QGridLayout()
        self.sublayout_new_event_grid.addWidget(self.label_event_name, 1, 0)
        self.sublayout_new_event_grid.addWidget(self.text_event_name, 1, 1)
        self.sublayout_new_event_grid.addWidget(self.label_start_date, 2, 0)
        self.sublayout_new_event_grid.addWidget(self.text_start_date, 2, 1)
        self.sublayout_new_event_grid.addWidget(self.label_repeat, 3, 0)
        self.sublayout_new_event_grid.addWidget(self.combo_repeat, 3, 1)
        self.sublayout_new_event_grid.addWidget(self.label_description, 4, 0)
        self.sublayout_new_event_grid.addWidget(self.text_description, 4, 1)
        self.sublayout_new_event.addLayout(self.sublayout_new_event_grid)

    def dateChanged(self, qdate):
        print("Date changed to:", self.getDaysEvents())
        selected_date = self.getDaysEvents()
        # self.events_list_widget.addItem(selected_date.strftime("%d/%m/%y"))

    def getDaysEvents(self):
        selected_date = self.calendar_widget.selectedDate()
        return selected_date.toPyDate()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    foo = CalendarWindow()
    foo.show()
    sys.exit(app.exec_())

当你启动它时,它看起来像这样:

我对第 3 列有疑问。我希望Dodaj event: 标签位于顶部,而那些带有标签的文本框位于其下方不远处。相关代码在init_sublayouts# New event sublayout注释下。我试图将这些文本框变成QGridLayoutDodaj event 和这个QGridLayoutQVBoxLabout,但你看到发生了什么。我还尝试将我的QGridLayout 的每一行都设为单独的QHBoxLayout,结果几乎相同。有没有办法做到这一点?我在第 2 列中做了类似的事情,我在 QTableWidget 上堆叠了一个 QLabel,但在第 3 列中它似乎没有按预期工作。

【问题讨论】:

【参考方案1】:

void QBoxLayout::addStretch(int stretch = 0) 将最小尺寸为零且拉伸因子拉伸的可拉伸空间(QSpacerItem)添加到此框布局的末尾。

试试看:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QMainWindow, QGridLayout, QLayout, QTableWidget, QHeaderView, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit, QComboBox
from PyQt5.QtCore import QDate, Qt
from datetime import date, datetime


class CalendarWindow(QMainWindow):
    def __init__(self, parent=None):
        super(CalendarWindow, self).__init__(parent)
        self.setUpUI()

    def setUpUI(self):
        self.setGeometry(50, 50, 1200, 700)
        # Labels
        self.label_events = QLabel(self)
        self.label_new_event = QLabel(self)
        self.label_event_name = QLabel(self)
        self.label_start_date = QLabel(self)
        self.label_repeat = QLabel(self)
        self.label_description = QLabel(self)
        # Labels parameters
        self.label_events.setAlignment(Qt.AlignCenter)
        self.label_events.setText("Wydarzenia danego dnia:")

        self.label_new_event.setAlignment(Qt.AlignCenter)         # +
        self.label_new_event.setText("Dodaj event:")

        self.label_event_name.setText("Nazwa eventu:")
        self.label_start_date.setText("Data eventu:")
        self.label_repeat.setText("Powtarzalność eventu:")
        self.label_description.setText("Opis:")
        # TextEdits
        self.text_event_name = QLineEdit(self)
        self.text_start_date = QLineEdit(self)
        self.text_description = QLineEdit(self)
        # TextEdits parameters

        # ComboBoxes
        self.combo_repeat = QComboBox(self)
        # ComboBoxes parameters
        self.combo_repeat.addItems(
            ["No", "Day", "Week", "Two weeks", "Month", "Half a year", "Year"])

        # Widgets
        self.setWindowTitle("Python Calendar App")
        self.calendar_widget = QCalendarWidget(self)
        # User date select event
        self.calendar_widget.clicked[QDate].connect(self.dateChanged)
        self.events_list_widget = QTableWidget(self)
        self.events_list_widget.setColumnCount(5)
        self.events_list_widget.setHorizontalHeaderLabels(
            ["ID", "Name", "Date", "Repetition", "Description"])
        # Columns stretched and fit
        self.events_list_widget.horizontalHeader().setSectionResizeMode(
            QHeaderView.Stretch)
        self.main_widget = QWidget(self)
        self.main_layout = QGridLayout(self.main_widget)
        self.main_layout.sizeConstraint = QLayout.SetDefaultConstraint
        self.main_layout.addWidget(self.calendar_widget, 0, 0)
        self.init_sublayouts()
        self.main_layout.addLayout(self.sublayout_events, 0, 1)
        self.main_layout.addLayout(self.sublayout_new_event, 0, 2)
        self.main_layout.setColumnStretch(0, 1)
        self.main_layout.setColumnStretch(1, 3)
        self.main_layout.setColumnStretch(2, 2)
        self.main_widget.setLayout(self.main_layout)
        self.setCentralWidget(self.main_widget)

    def init_sublayouts(self):
        # Events sublayout
        self.sublayout_events = QVBoxLayout()
        self.sublayout_events.addWidget(self.label_events)
        self.sublayout_events.addWidget(self.events_list_widget)
        # New event sublayout
        self.sublayout_new_event = QVBoxLayout()
        self.sublayout_new_event.addWidget(self.label_new_event)
        self.sublayout_new_event.addStretch(1)                            # +

        self.sublayout_new_event_grid = QGridLayout()
        self.sublayout_new_event_grid.addWidget(self.label_event_name, 1, 0)
        self.sublayout_new_event_grid.addWidget(self.text_event_name, 1, 1)
        self.sublayout_new_event_grid.addWidget(self.label_start_date, 2, 0)
        self.sublayout_new_event_grid.addWidget(self.text_start_date, 2, 1)
        self.sublayout_new_event_grid.addWidget(self.label_repeat, 3, 0)
        self.sublayout_new_event_grid.addWidget(self.combo_repeat, 3, 1)
        self.sublayout_new_event_grid.addWidget(self.label_description, 4, 0)
        self.sublayout_new_event_grid.addWidget(self.text_description, 4, 1)
        self.sublayout_new_event.addLayout(self.sublayout_new_event_grid)
        self.sublayout_new_event.addStretch(20)                            # +

    def dateChanged(self, qdate):
        print("Date changed to:", self.getDaysEvents())
        selected_date = self.getDaysEvents()
        # self.events_list_widget.addItem(selected_date.strftime("%d/%m/%y"))

    def getDaysEvents(self):
        selected_date = self.calendar_widget.selectedDate()
        return selected_date.toPyDate()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    foo = CalendarWindow()
    foo.show()
    sys.exit(app.exec_())

【讨论】:

以上是关于PyQT5 正确的布局对齐的主要内容,如果未能解决你的问题,请参考以下文章

如何在 PyQt5 中保持行和列大小与网格布局相同?

PyQt5编程(24):在窗口中布局组件—水平对齐和垂直对齐

如何在折叠布局下的 CoordinatorLayout 中正确对齐元素?

为啥段落文本在默认的 CSS3 多列网站布局中不能正确对齐?

使用自动布局正确对齐通知中心小部件中的元素

如何在 PyQt5 中设置布局的固定高度?