更改为 PyQt4 combo_boxes 中列出的选项,在不同的 class 中定义未反映
Changed to options listed in PyQt4 combo_boxes defined in a different class not reflected
我正在使用 PyQt4 开发一个小型 GUI 项目。我已经定义了一个 class(在一个单独的文件中)来定义我必须使用的 combo_boxes 的基本功能,另一个 class 来使用所有 combo_boxes 的功能。
代码看起来像
class core:
def __init__(self, default_value, window_name):
self.combo_box = QtGui.QComboBox(window_name)
self.combo_box.addItem(default_value)
self.combo_box.addItem("some other value")
self.combo_box.addItem("a third value")
self.combo_box.activated[str].connect(self.set_text)
self.text = default_value
def set_text(self, text):
print text
主要的 class 是这样的:
from file import *
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(200, 100, 820, 700)
combo_box_one = core("first", self)
combo_box_two = core("second", self)
#some other methods follow defining the geometry for each combo_box and other functions
def main():
app = QtGui.QApplication(sys.argv)
gui = Window()
sys.exit(app.exec_())
main()
GUI 正在按预期工作。所有 combo_boxes 都按照定义的几何形状出现。但是,在选择不同的选项时,似乎没有任何事情发生。理想情况下,我希望打印选项上的文本。事实上,当我 return 将 combo_box 对象指向主 class 并在那里设置它的连接时,选项的变化就会反映出来。但是当在 core
class 中做同样的事情时,更改不会反映为打印文本。它是一个范围相关的东西吗?请帮助我了解发生了什么。
槽只能在继承自QObject
的classes中实现,一个简单的解决方案是核心class继承自QComboBox
,因为QComboBox
继承自 QObject
.
class core(QtGui.QComboBox):
def __init__(self, default_value, window_name):
QtGui.QComboBox.__init__(self, window_name)
self.addItem(default_value)
self.addItem("some other value")
self.addItem("a third value")
self.activated[str].connect(self.set_text)
def set_text(self, text):
print(text)
我正在使用 PyQt4 开发一个小型 GUI 项目。我已经定义了一个 class(在一个单独的文件中)来定义我必须使用的 combo_boxes 的基本功能,另一个 class 来使用所有 combo_boxes 的功能。 代码看起来像
class core:
def __init__(self, default_value, window_name):
self.combo_box = QtGui.QComboBox(window_name)
self.combo_box.addItem(default_value)
self.combo_box.addItem("some other value")
self.combo_box.addItem("a third value")
self.combo_box.activated[str].connect(self.set_text)
self.text = default_value
def set_text(self, text):
print text
主要的 class 是这样的:
from file import *
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(200, 100, 820, 700)
combo_box_one = core("first", self)
combo_box_two = core("second", self)
#some other methods follow defining the geometry for each combo_box and other functions
def main():
app = QtGui.QApplication(sys.argv)
gui = Window()
sys.exit(app.exec_())
main()
GUI 正在按预期工作。所有 combo_boxes 都按照定义的几何形状出现。但是,在选择不同的选项时,似乎没有任何事情发生。理想情况下,我希望打印选项上的文本。事实上,当我 return 将 combo_box 对象指向主 class 并在那里设置它的连接时,选项的变化就会反映出来。但是当在 core
class 中做同样的事情时,更改不会反映为打印文本。它是一个范围相关的东西吗?请帮助我了解发生了什么。
槽只能在继承自QObject
的classes中实现,一个简单的解决方案是核心class继承自QComboBox
,因为QComboBox
继承自 QObject
.
class core(QtGui.QComboBox):
def __init__(self, default_value, window_name):
QtGui.QComboBox.__init__(self, window_name)
self.addItem(default_value)
self.addItem("some other value")
self.addItem("a third value")
self.activated[str].connect(self.set_text)
def set_text(self, text):
print(text)