英文:
Pyside6 get index of dynamically created widget
问题
我写了这个简单的脚本(用于概念理解),以更好地理解如何处理/管理动态创建的组合框。
所以在这个示例中,我们总共有5个动态创建的组合框,每个组合框都包含3个变量的列表。
在选择任何变量时,将运行comboFunction
函数。
我想要理解的是:
1)如何获取正在选择的组合框的索引
2)正在选择的变量的索引。
并在comboFunction
中打印出组合框和变量的索引。
例如,在下面的截图中,我选择了索引为0的组合框和索引为0的变量。
import sys
from PySide6 import QtWidgets
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout = QtWidgets.QGridLayout(self)
self.lists = ["1", "2", "3"]
for i in range(5):
self.combobox = QtWidgets.QComboBox(self)
self.combobox.addItems(self.lists)
self.layout.addWidget(self.combobox, i, 0)
self.combobox.currentIndexChanged.connect(self.comboFunction)
def comboFunction(self):
index_combo = self.layout.indexOf(self.sender())
index_variable = self.sender().currentIndex()
print(f"Selected Combo Box Index: {index_combo}, Selected Variable Index: {index_variable}")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.resize(800, 600)
widget.show()
sys.exit(app.exec())
<details>
<summary>英文:</summary>
I've written this simple script(for concept understanding) to better understand how to handle/manage dynamically created combo boxes.
So in this example, we have a total of 5 dynamically created combo boxes, each containing a list of 3 variables.
When selecting any variable the function `comboFunction` is run.
What I want to understand, is:
1) How can I retrieve the index of the combo box being selected
2) The index of the variable being selected.
And print in the `comboFunction` the index of the Combobox and the variable.
---
For example in the screenshot below, I selected the combo box at index 0 and the variable at index 0.
---
[![enter image description here][1]][1]
import sys
from PySide6 import QtWidgets
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout = QtWidgets.QGridLayout(self)
self.lists = ["1","2","3"]
for i in range(5):
self.combobox = QtWidgets.QComboBox(self)
self.combobox.addItems(self.lists)
self.layout.addWidget(self.combobox, i,0)
self.combobox.currentIndexChanged.connect(self.comboFunction)
def comboFunction(self):
print("hello world")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.resize(800, 600)
widget.show()
sys.exit(app.exec())
[1]: https://i.stack.imgur.com/oCENX.png
</details>
# 答案1
**得分**: 1
```python
一个简单的方法是将其数字作为属性添加,然后稍后读取:
```python
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout = QtWidgets.QGridLayout(self)
self.lists = ["1", "2", "3"]
for i in range(5):
self.combobox = QtWidgets.QComboBox(self)
self.combobox.id_number = i
self.combobox.addItems(self.lists)
self.layout.addWidget(self.combobox, i, 0)
self.combobox.currentIndexChanged.connect(self.comboFunction)
def comboFunction(self, idx):
combo = self.sender()
print(f"从组合框 {combo.id_number} 中选择了变量 {idx}")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.resize(800, 600)
widget.show()
sys.exit(app.exec_())
英文:
A simple way would be to add its number as an attribute and read it later:
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout = QtWidgets.QGridLayout(self)
self.lists = ["1", "2", "3"]
for i in range(5):
self.combobox = QtWidgets.QComboBox(self)
self.combobox.id_number = i
self.combobox.addItems(self.lists)
self.layout.addWidget(self.combobox, i, 0)
self.combobox.currentIndexChanged.connect(self.comboFunction)
def comboFunction(self, idx):
combo = self.sender()
print(f"Selected the variable {idx} from combo {combo.id_number}")
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.resize(800, 600)
widget.show()
sys.exit(app.exec_())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论