我有此代码:
class Main(QWidget):
def __init__(self):
super().__init__()
self.init_gui()
def init_gui(self):
self.layout_main = QVBoxLayout()
self.setLayout(self.layout_main)
self.first()
self.second()
self.showMaximized()
def scroll_areas(self):
scroll_area = QScrollArea(self)
widget = QWidget()
layout = QVBoxLayout()
scroll_area.setWidgetResizable(True)
scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
scroll_area.setFixedSize(200, 200)
widget.setLayout(layout)
scroll_area.setWidget(widget)
# self.layout_main.addLayout(layout)
def first(self):
title = QLabel("<h1>First</h1>")
title.setTextFormat(Qt.RichText)
def second(self):
title = QLabel("<h1>Second</h1>")
title.setTextFormat(Qt.RichText)
我想在function scroll_areas
和functions first
中加入second
,然后我想要这样的东西:
add QLabel title to scroll_areas's layout
最后我要在主布局中添加scroll_areas的布局,我在scroll_areas
中注释了这一行,因为它必须在first
和second
的最后一行中。
谢谢!
答案 0 :(得分:0)
尝试一下:
class Main(QWidget):
def __init__(self):
super().__init__()
self.init_gui()
def init_gui(self):
self.layout_main = QVBoxLayout()
self.setLayout(self.layout_main)
self.scroll_areas() # +++
self.first()
self.second()
self.showMaximized()
def scroll_areas(self):
scroll_area = QScrollArea(self)
widget = QWidget()
self.layout = QVBoxLayout()
scroll_area.setWidgetResizable(True)
scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
scroll_area.setFixedSize(200, 200)
widget.setLayout(self.layout)
scroll_area.setWidget(widget)
# self.layout_main.addLayout(layout)
self.layout_main.addWidget(scroll_area) # +++
def first(self):
title = QLabel("<h1>First</h1>")
title.setTextFormat(Qt.RichText)
self.layout.addWidget(title) # +++
def second(self):
title = QLabel("<h1>Second</h1>")
title.setTextFormat(Qt.RichText)
self.layout.addWidget(title) # +++