0

这段代码中阻止小部件根据窗口大小扩展的错误是什么?

class FeedbackWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.main_layout = QVBoxLayout(self)
        self.main_widget = QWidget(self)
        self.main_widget.setLayout(self.main_layout)
        self.title_label = QLabel("Tell us what you think:")
        self.feedback_text_editor = QTextEdit()
        self.send_button = QPushButton("Send")

        self.main_layout.addWidget(self.title_label)
        self.main_layout.addWidget(self.feedback_text_editor)
        self.main_layout.addWidget(self.send_button)

        self.setWindowTitle("Feedback")
        self.setGeometry(200,120,300,300)

    if __name__ == "__main__":

        app = QApplication(sys.argv)
        w = FeedbackWindow()
        w.show()
        app.exec_()

主布局和小部件连接到自我,所以它应该采取它的维度。

4

2 回答 2

1

消除

self.main_widget = QWidget(self)
self.main_widget.setLayout(self.main_layout)

你不需要它们。在您的实现中,布局是在 self.main_widget 上设置的,它不是主要的小部件。您的主要小部件是您的 FeedbackWindows 本身。当您调用 时self.main_layout = QVBoxLayout(self),它会隐式地将布局应用到主小部件上。

于 2013-10-07T13:27:04.893 回答
1

该代码不使用self.main_widget. 删除self.main_widget

import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class FeedbackWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.main_layout = QVBoxLayout(self)
        #self.main_widget = QWidget(self)              # main_widget is not used.
        #self.main_widget.setLayout(self.main_layout)
        self.setLayout(self.main_layout)
        self.title_label = QLabel("Tell us what you think:")
        self.feedback_text_editor = QTextEdit()
        self.send_button = QPushButton("Send")

        self.main_layout.addWidget(self.title_label)
        self.main_layout.addWidget(self.feedback_text_editor)
        self.main_layout.addWidget(self.send_button)

        self.setWindowTitle("Feedback")
        self.setGeometry(200,120,300,300)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = FeedbackWindow()
    w.show()
    app.exec_()
于 2013-10-07T13:27:23.067 回答