简体   繁体   English

(PyQt5)创建QLineEdit的列表

[英](PyQt5) Create list of QLineEdit

How to make a list of line editors without many variables? 如何制作没有太多变量的行编辑器列表? (smth like self.line_1 = QLineEdit(self), self.line_2 = QLineEdit(self), ... , self.line_9000 = QLineEdit(self) ) (像self.line_1 = QLineEdit(self), self.line_2 = QLineEdit(self), ... , self.line_9000 = QLineEdit(self)

For example, I want to create this 例如,我要创建这个

QLineEdit列表

window with ability to get access to each element. 能够访问每个元素的窗口。 A simple cycle does not provide access to each element, only last. 一个简单的循环不会提供对每个元素的访问,只能访问最后一个。 How I can do this? 我该怎么做?

One way is to make widgets as you said - cycle, and you can access to the widget with using layout.itemAtPosition 一种方法是按照您所说的制作小部件-循环,然后可以使用layout.itemAtPosition访问小部件

it would go like this : 它会像这样:

layout = QVBoxLayout()
for i in range(list_length):
    line_edit = QLineEdit(self)
    layout.addWidget(line_edit)

to access the widget : 访问小部件:

def access_widget(int):
    item = layout.itemAtPosition(int)
    line_edit = item.widget()
    return line_edit

now you can access to the designated QLineEdit. 现在您可以访问指定的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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM