此代码中的错误是什么阻止窗口小部件根据窗口大小进行扩展?
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_()
主要布局和小部件已连接到self,因此它应该采用其维度。
答案 0 :(得分:1)
删除
self.main_widget = QWidget(self)
self.main_widget.setLayout(self.main_layout)
你不需要它们。在您的实现中,布局设置在self.main_widget上,而不是主要的小部件。您的主要小部件是您的FeedbackWindows本身。当您致电self.main_layout = QVBoxLayout(self)
时,它会隐含地在主要小部件上应用布局。
答案 1 :(得分: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_()