名單如何使行編輯器的列表,沒有太多的變數? (如self.line_1 = QLineEdit(self), self.line_2 = QLineEdit(self), ... , self.line_9000 = QLineEdit(self)
水木清華)(PyQt5)創建的QLineEdit的
例如,我想與能力,可以訪問每一個元素創建這個
窗口。 一個簡單的週期不提供訪問每個元素,只持續。我如何做到這一點?
名單如何使行編輯器的列表,沒有太多的變數? (如self.line_1 = QLineEdit(self), self.line_2 = QLineEdit(self), ... , self.line_9000 = QLineEdit(self)
水木清華)(PyQt5)創建的QLineEdit的
例如,我想與能力,可以訪問每一個元素創建這個
窗口。 一個簡單的週期不提供訪問每個元素,只持續。我如何做到這一點?
的一種方法是使部件如你所說 - 週期, ,您可以訪問到窗口小部件使用layout.itemAtPosition
它會是這樣的:
layout = QVBoxLayout()
for i in range(list_length):
line_edit = QLineEdit(self)
layout.addWidget(line_edit)
訪問控件:
def access_widget(int):
item = layout.itemAtPosition(int)
line_edit = item.widget()
return line_edit
現在您可以訪問指定的QLineEdit。
layout = QFormLayout()
self.alphabet_line_edits = dict.fromkeys(['а', 'б', 'в', 'г'])
for letter in self.alphabet_line_edits:
line_edit = QLineEdit()
layout.addRow(letter, line_edit)
self.alphabet_line_edits[letter] = line_edit
def button_clicked(self):
print(self.alphabet_line_edit['б'].text())