Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:AttributeError:type对象';用户界面表格';没有属性';组合框';_Python - Fatal编程技术网

Python:AttributeError:type对象';用户界面表格';没有属性';组合框';

Python:AttributeError:type对象';用户界面表格';没有属性';组合框';,python,Python,我有两个模块test.py和fill_combobox.py。“填充”组合框用于组合框中的默认值。模块之间的通信仍然存在问题 test.py from PyQt4 import QtCore, QtGui import fill_combobox try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encodin

我有两个模块test.py和fill_combobox.py。“填充”组合框用于组合框中的默认值。模块之间的通信仍然存在问题

test.py

from PyQt4 import QtCore, QtGui import fill_combobox try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Form(object): def setupUi(self, Form): Form.setObjectName(_fromUtf8("Form")) Form.resize(400, 300) self.comboBox = QtGui.QComboBox(Form) self.comboBox.setGeometry(QtCore.QRect(160, 120, 51, 25)) self.comboBox.setObjectName(_fromUtf8("comboBox")) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): Form.setWindowTitle(_translate("Form", "Form", None)) fill_combobox.fill() if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) Form = QtGui.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_()) 从PyQt4导入QtCore、QtGui 导入填充组合框 尝试: _fromUtf8=QtCore.QString.fromUtf8 除属性错误外: 来自UTF8的def_: 返回s 尝试: _编码=QtGui.QApplication.UnicodeUTF8 定义翻译(上下文、文本、消歧): 返回QtGui.QApplication.translate(上下文、文本、消歧、_编码) 除属性错误外: 定义翻译(上下文、文本、消歧): 返回QtGui.QApplication.translate(上下文、文本、消歧) 类Ui_表单(对象): def setupUi(自我,表格): setObjectName(_fromUtf8(“表单”)) 表单。调整大小(400300) self.comboBox=QtGui.QComboBox(表单) self.comboBox.setGeometry(QtCore.QRect(160、120、51、25)) self.comboBox.setObjectName(_fromUtf8(“comboBox”)) 自重传(表格) QtCore.QMetaObject.connectSlotsByName(表单) def重新传输(自身,表格): Form.setWindowTitle(_translate(“Form”,“Form”,None)) fill_组合框。fill() 如果名称=“\uuuuu main\uuuuuuuu”: 导入系统 app=QtGui.QApplication(sys.argv) Form=QtGui.QWidget() ui=ui_Form() ui.setupUi(表单) 表格.show() sys.exit(app.exec_()) fill_combobox.py

def fill(): import test for i in range(40): test.Ui_Form.comboBox.addItems(str(i)) def fill(): 导入测试 对于范围(40)内的i: test.Ui_Form.comboBox.addItems(str(i)) 在您的代码行中:

test.Ui_Form.comboBox.addItems(str(i))
正在访问类
Ui\u表单
,而不是
Ui\u表单
实例。因此,该类没有任何
组合框
属性。如果要使用
Ui\u表单
类,请使用
test.py
末尾显示的模式:

form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(form)
form.show()
您应该将该代码放在for循环的
之前,并在循环中访问
表单
实例


如果您在许多地方都这样做,那么最好将自定义小部件编写为:

class MyWidget(QWidget, Ui_Form):
    def __init__(self, parent=None, ...):
        super(MyWidget, self).__init__(parent)
        self.setupUi(self)
然后可以避免创建
Ui\u表单
实例,只需执行以下操作:

form = MyWidget()
form.show()

请详细说明您面临的确切问题。这样您将获得更多帮助:-)