我是 PyQt / PySide 的新手。
我有很多行编辑(用于显示文件位置),并且对于每行文本,我都有一个按钮(用于显示打开文件对话框)。
我有一个方法:
def selectSelf1(self):
""" browse for file dialog """
myDialog = QtGui.QFileDialog
self.lineSelf1.setText(myDialog.getOpenFileName())
并使用以下代码绑定按钮
self.btnSelf1.clicked.connect(self.selectSelf1)
我有大约 20 个这样的按钮和 20 个这样的行编辑。有没有一种简单的方法可以将所有这些按钮绑定到相应的行编辑,而不是输入所有内容。
Thanks!
如果您有按钮和行编辑列表,您可以使用以下内容:
QSignalMapper https://doc.qt.io/qt-4.8/qsignalmapper.html, 另一种描述 https://doc.qt.io/archives/qq/qq10-signalmapper.html
-
functools.partial http://docs.python.org/library/functools.html#functools.partial, 像这样:
def show_dialog(self, line_edit):
...
line_edit.setText(...)
for button, line_edit in zip(buttons, line_edits):
button.clicked.connect(functools.partial(self.show_dialog, line_edit))
-
lambda
's
for button, line_edit in ...:
button.clicked.connect(lambda : self.show_dialog(line_edit))
如果您使用 Qt Designer,并且没有按钮和行编辑列表,但它们都具有相同的命名模式,您可以使用一些内省:
class Foo(object):
def __init__(self):
self.edit1 = 1
self.edit2 = 2
self.edit3 = 3
self.button1 = 1
self.button2 = 2
self.button3 = 3
def find_attributes(self, name_start):
return [value for name, value in sorted(self.__dict__.items())
if name.startswith(name_start)]
foo = Foo()
print foo.find_attributes('edit')
print foo.find_attributes('button')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)