我实现了一个UI,它在顶部包含几个按钮,并且这些按钮下面有一个QStackedLayout对象,点击不同按钮的目的应该自动切换到其关联的QWidget,里面是QStackedLayout对象。按钮的数量及其相关的QWidget是动态添加的。
例如,在这里,我想实现一个包含两个按钮的简单窗口' a'和' b',理想情况下点击' a'按钮,你应该看到标签' a'在下面,这意味着QStackedLayout被设置为一个适当的小部件,其中包含QLabel'类似的规则应该适用于按钮' b'同样。所以我写了以下代码:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class main_gui(QWidget):
def __init__(self, parent = None):
super(main_gui, self).__init__(parent)
#create a root level widget
self.main_widget = QWidget()
#create a root level layout that holds all the child widgets
self.main_layout = QVBoxLayout(self.main_widget)
# widgets for buttons layout
self.asset_type_buttons_widget = QWidget()
# layout for all buttons
self.asset_type_buttons_layout = QHBoxLayout(self.asset_type_buttons_widget)
# now create the stack layout
self.asset_stacked_widget = QWidget()
self.asset_stacked_layout = QStackedLayout(self.asset_stacked_widget)
# add the buttons and stack layout into the root level layout
self.main_layout.addWidget(self.asset_type_buttons_widget)
self.main_layout.addWidget(self.asset_stacked_widget)
#dynamically adds buttons and stacked widgets, here we're going to add 2 buttons, which their labels are 'a' and 'b'
self.create_asset_sections(self.asset_stacked_layout, self.asset_type_buttons_layout, 'a','b')
self.main_widget.show()
def create_asset_sections(self, stack_layout, parent_buttons_layout, *asset_types):
# asset_types represents any arbitrary number of buttons and their associated stacked widgets
for type in asset_types:
index = asset_types.index(type)
self.type_top_button = QPushButton(type)
parent_buttons_layout.addWidget(self.type_top_button)
self.type_top_widget = QWidget()
self.type_top_layout = QVBoxLayout(self.type_top_widget)
i = stack_layout.insertWidget(index, self.type_top_widget)
self.test_label = QLabel(type)
self.type_top_layout.addWidget(self.test_label)
self.type_top_button.clicked.connect(lambda: stack_layout.setCurrentIndex(i))
app = QApplication(sys.argv)
gui = main_gui()
app.exec_()
但执行上述代码后,您应该注意到标签' a'只出现一次,然后无论你点击按钮' a'或按钮'标签' b'总是显示在按钮下面。
我还尝试修改代码然后使用QStackedLayout.setCurrentWidget()方法,这里是另一个版本的create_asset_sections()
def create_asset_sections(self, stack_layout, parent_buttons_layout, *asset_types):
# create a dictionary that holds the buttons and their associated stack widgets
self.map_buttons_widgets = {}
for type in asset_types:
self.type_top_button = QPushButton(type)
parent_buttons_layout.addWidget(self.type_top_button)
self.type_top_widget = QWidget()
self.type_top_layout = QVBoxLayout(self.type_top_widget)
stack_layout.addWidget(self.type_top_widget)
self.test_label = QLabel(type)
self.type_top_layout.addWidget(self.test_label)
self.map_buttons_widgets[self.type_top_button] = self.type_top_widget
for btn, wigt in self.map_buttons_widgets.iteritems():
btn.clicked.connect(lambda: stack_layout.setCurrentWidget(wigt))
因此,使用上述方法替换之前的方法也不能帮助实现我的原始目的。
有什么想法?提前感谢您的回答。
答案 0 :(得分:2)
见question。这是一个不同的问题,但他们的问题基本相同。这一行是问题
self.type_top_button.clicked.connect(lambda: stack_layout.setCurrentIndex(i))
具体地
lambda: stack_layout.setCurrentIndex(i)
问题是在创建lambda函数时不会评估i
的值。它在lambda函数被调用时进行了评估,到那时,循环已经完成,每个lambda
将在循环结束时使用完全相同的i
值,而不是创建i
时lambda
的值。
您可以通过以下两种方式解决此问题。第一种是在lambda
中使用默认参数,这会强制在函数创建时评估i
的值。
lambda i=i: stack_layout.setCurrentIndex(i)
或者,您可以使用functools.partial
from functools import partial
self.type_top_button.clicked.connect(partial(layout.setCurrentIndex, i))