不能在方法中使用我的小部件 Qcombobox,为什么?
Can't use my widget Qcombobox in method, why?
pyQt 的新手,我试图用项目列表填充 Qcombobox,然后检索用户选择的文本。
一切正常,除了当 CurrentIndexChanged 信号被触发时,我无法在我的方法中使用 .currentText() 获取选择的索引,而不是文本,因为我有一个错误告诉我我无法调用我在方法中的小部件。 Python 在方法中不识别我的QCombobox,所以我不能使用.currentText(),我也想不通为什么。
谢谢!
请参阅下面的代码。
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Votre territoire")
layout = QVBoxLayout()
listCom = self.getComINSEE()
cbCommunes = QComboBox()
cbCommunes.addItems(listCom)
cbCommunes.currentIndexChanged.connect(self.selectionChange)
layout.addWidget(cbCommunes)
cbCommunes = QWidget()
cbCommunes.setLayout(layout)
self.setCentralWidget(cbCommunes)
def getComINSEE(self):
# some code to fill my list com
return com
def selectionChange(self, i):
# Error : unhandled AttributeError "'MainWindow' object has no attribute 'cbCommunes'"
texte = self.cbCommunes.currentText()
print(f"Index {i} pour la commune {i}")
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
要在任何 class 方法中访问对象,您需要使该对象成为 class 的属性。
将 cbCommunes
更改为 self.cbCommunes
- 无处不在。
from PyQt5.Qt import *
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Votre territoire")
listCom = self.getComINSEE()
self.cbCommunes = QComboBox()
self.cbCommunes.addItems(listCom)
self.cbCommunes.currentIndexChanged.connect(self.selectionChange)
centralwidget = QWidget()
self.setCentralWidget(centralwidget)
layout = QVBoxLayout(centralwidget)
layout.addWidget(self.cbCommunes)
def getComINSEE(self):
# some code to fill my list com
com = ['item 1', 'item 2', 'item 3', 'item 4', 'item 5',]
return com
def selectionChange(self, i):
# Error : unhandled AttributeError "'MainWindow' object has no attribute 'cbCommunes'"
texte = self.cbCommunes.currentText()
print(f"Index {i} pour la commune {i}, texte -> {texte}")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
pyQt 的新手,我试图用项目列表填充 Qcombobox,然后检索用户选择的文本。 一切正常,除了当 CurrentIndexChanged 信号被触发时,我无法在我的方法中使用 .currentText() 获取选择的索引,而不是文本,因为我有一个错误告诉我我无法调用我在方法中的小部件。 Python 在方法中不识别我的QCombobox,所以我不能使用.currentText(),我也想不通为什么。
谢谢!
请参阅下面的代码。
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Votre territoire")
layout = QVBoxLayout()
listCom = self.getComINSEE()
cbCommunes = QComboBox()
cbCommunes.addItems(listCom)
cbCommunes.currentIndexChanged.connect(self.selectionChange)
layout.addWidget(cbCommunes)
cbCommunes = QWidget()
cbCommunes.setLayout(layout)
self.setCentralWidget(cbCommunes)
def getComINSEE(self):
# some code to fill my list com
return com
def selectionChange(self, i):
# Error : unhandled AttributeError "'MainWindow' object has no attribute 'cbCommunes'"
texte = self.cbCommunes.currentText()
print(f"Index {i} pour la commune {i}")
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
要在任何 class 方法中访问对象,您需要使该对象成为 class 的属性。
将 cbCommunes
更改为 self.cbCommunes
- 无处不在。
from PyQt5.Qt import *
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Votre territoire")
listCom = self.getComINSEE()
self.cbCommunes = QComboBox()
self.cbCommunes.addItems(listCom)
self.cbCommunes.currentIndexChanged.connect(self.selectionChange)
centralwidget = QWidget()
self.setCentralWidget(centralwidget)
layout = QVBoxLayout(centralwidget)
layout.addWidget(self.cbCommunes)
def getComINSEE(self):
# some code to fill my list com
com = ['item 1', 'item 2', 'item 3', 'item 4', 'item 5',]
return com
def selectionChange(self, i):
# Error : unhandled AttributeError "'MainWindow' object has no attribute 'cbCommunes'"
texte = self.cbCommunes.currentText()
print(f"Index {i} pour la commune {i}, texte -> {texte}")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())