传递 currentTextChanged 的值
Pass the value of currentTextChanged
下面是一段代码
目前 combobox
由 self.Cbox.btn.clicked.connect(self.Goto_Analyze)
管理。
我想避免使用 btn
并使用 currentTextChanged
但是无论我把它放在哪里都会出错。
self.MyCbox = QtWidgets.QComboBox(self.My_tab)
self.MyCbox.setGeometry(QtCore.QRect(700, 30, 100, 21))
self.MyCbox.setObjectName("MyCbox")
self.MyCbox.addItems(Functions.nbr)
aa = str(self.MyCbox.currentText())
print(aa) # aa = 6
self.Cbox.btnsetText(_translate("Library_main", "Select"))
self.Cbox.btnclicked.connect(self.Goto_Analyze)
def Goto_Analyze(self, ):
aa = str(self.MyCbox.currentTextChanged())
[...]
some code
File "../index.py", line 796, in Goto_Analyze
aa = str(self.MyCbox.currentTextChanged())
TypeError: native Qt signal is not callable
是signal
所以你需要
currentTextChanged.connect(...)
喜欢信号 clicked
你需要 clicked.connect(...)
最少的工作代码
from PyQt5 import QtWidgets
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.cbox = QtWidgets.QComboBox(self) # PEP8: `lower_case_name` for variables
self.cbox.addItems([str(x) for x in range(3)])
self.cbox.currentTextChanged.connect(self.goto_analyze)
def goto_analyze(self, value): # PEP8: `lower_case_name` for functions
print(type(value), value)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
win = MyWindow()
win.show()
app.exec()
下面是一段代码
目前 combobox
由 self.Cbox.btn.clicked.connect(self.Goto_Analyze)
管理。
我想避免使用 btn
并使用 currentTextChanged
但是无论我把它放在哪里都会出错。
self.MyCbox = QtWidgets.QComboBox(self.My_tab)
self.MyCbox.setGeometry(QtCore.QRect(700, 30, 100, 21))
self.MyCbox.setObjectName("MyCbox")
self.MyCbox.addItems(Functions.nbr)
aa = str(self.MyCbox.currentText())
print(aa) # aa = 6
self.Cbox.btnsetText(_translate("Library_main", "Select"))
self.Cbox.btnclicked.connect(self.Goto_Analyze)
def Goto_Analyze(self, ):
aa = str(self.MyCbox.currentTextChanged())
[...]
some code
File "../index.py", line 796, in Goto_Analyze
aa = str(self.MyCbox.currentTextChanged())
TypeError: native Qt signal is not callable
是signal
所以你需要
currentTextChanged.connect(...)
喜欢信号 clicked
你需要 clicked.connect(...)
最少的工作代码
from PyQt5 import QtWidgets
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.cbox = QtWidgets.QComboBox(self) # PEP8: `lower_case_name` for variables
self.cbox.addItems([str(x) for x in range(3)])
self.cbox.currentTextChanged.connect(self.goto_analyze)
def goto_analyze(self, value): # PEP8: `lower_case_name` for functions
print(type(value), value)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
win = MyWindow()
win.show()
app.exec()