简化我的代码,创建的一个函数被其他函数使用 [英] Simplify my code, one function to create is used by other functions
本文介绍了简化我的代码,创建的一个函数被其他函数使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码:
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)
我希望在 functions first
和 second
中有 function scroll_areas
然后我想要这样的东西:
I would like to have function scroll_areas
in functions first
and second
and then I would like something like this:
add QLabel title to scroll_areas's layout
我最后一件事是将scroll_areas的布局添加到主布局中,我在scroll_areas
中注释了该行,因为它必须在first
和的最后一行第二个
.
I the last thing is adding scroll_areas's layout to main layout, I commented the line in scroll_areas
, cuz it has to be in last line in first
and second
.
谢谢!
推荐答案
试试看:
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) # +++
这篇关于简化我的代码,创建的一个函数被其他函数使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文