Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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检查模块找不到pyqt类_Python_Python 2.7_Class_Pyqt_Pyqt5 - Fatal编程技术网

Python检查模块找不到pyqt类

Python检查模块找不到pyqt类,python,python-2.7,class,pyqt,pyqt5,Python,Python 2.7,Class,Pyqt,Pyqt5,为了在pyqt5中存储gui设置,我在该网站上找到了一个非常好的解决方案: 解决方案保存在函数guisave中 现在我正试图在我的代码中实现这一点。 这个想法是用退出按钮关闭我的gui。这将启动closeApp函数,该函数将启动guisave函数。 guisave函数现在应该保存我的所有pyqt对象。 问题是这种情况不会发生。我不确定如何在guisave函数中分配ui变量。 正如您所看到的,我试图分配主窗口类。但这是行不通的。我也不确定这是否有效,或者是否需要单独扫描所有函数,因为QEditLi

为了在pyqt5中存储gui设置,我在该网站上找到了一个非常好的解决方案:

解决方案保存在函数guisave

现在我正试图在我的代码中实现这一点。 这个想法是用退出按钮关闭我的gui。这将启动closeApp函数,该函数将启动guisave函数。 guisave函数现在应该保存我的所有pyqt对象。
问题是这种情况不会发生。我不确定如何在guisave函数中分配ui变量。 正如您所看到的,我试图分配主窗口类。但这是行不通的。我也不确定这是否有效,或者是否需要单独扫描所有函数,因为QEditLine在tab2UI函数中

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import inspect

class mainwindow(QMainWindow):
    def __init__(self, parent = None):
        super(mainwindow, self).__init__(parent)

        self.initUI()


    def initUI(self):
        exitAction = QAction(QIcon('icon\\exit.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)     
        exitAction.triggered.connect(self.closeApp)     

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.setMovable(False)
        self.toolbar.addAction(exitAction)

        self.tab_widget = QTabWidget(self)     # add tab

        self.tab2 = QWidget()
        self.tab_widget.addTab(self.tab2, "Tab_2") 
        self.tab2UI()

        self.setCentralWidget(self.tab_widget)


    def tab2UI(self):
        self.layout = QFormLayout()
        self.layout.addRow("Name",QLineEdit())
        self.layout.addRow("Address",QLineEdit())
        self.tab2.setLayout(self.layout)


    def closeApp(self):
        guisave()


def guisave():
    ui = mainwindow
    settings = QSettings('gui.ini', QSettings.IniFormat)

    for name, obj in inspect.getmembers(ui): 
        if isinstance(obj, QComboBox):
            name = obj.objectName()  # get combobox name
            index = obj.currentIndex()  # get current index from combobox
            text = obj.itemText(index)  # get the text for current index
            settings.setValue(name, text)  # save combobox selection to registry

        if isinstance(obj, QLineEdit):
            print obj.objectName()
            name = obj.objectName()
            value = obj.text()
            settings.setValue(name, value)  # save ui values, so they can be restored next time
            print name, value

        if isinstance(obj, QCheckBox):
            name = obj.objectName()
            state = obj.isChecked()
            settings.setValue(name, state)

        if isinstance(obj, QRadioButton):
            name = obj.objectName()
            value = obj.isChecked()  # get stored value from registry
            settings.setValue(name, value)


def main():
    app = QApplication(sys.argv)
    ex = mainwindow()
    ex.setGeometry(100,100,1000,600)
    ex.show()
    sys.exit(app.exec_())



if __name__ == '__main__':
    main()

我提出的解决方案负责保存QWidgets的状态,但为此,您必须在属性中输入一个名称:

objectName:QString

此属性包含此对象的名称。可以使用findChild()按名称(和类型)查找对象。你可以 使用findChildren()查找一组对象

访问功能:

QStringobjectName()const

voidsetObjectName(常量QString&name)

因此,您应该输入一个名称,例如:

self.tab_widget.setObjectName("tabWidget")
我们可以使用QApplication::allWidgets()函数获取应用程序的所有小部件,然后获取属性并保存它们,恢复过程与前一个过程相反

def restore(settings):
    finfo = QFileInfo(settings.fileName())

    if finfo.exists() and finfo.isFile():
        for w in qApp.allWidgets():
            mo = w.metaObject()
            if w.objectName() != "":
                for i in range(mo.propertyCount()):
                    name = mo.property(i).name()
                    val = settings.value("{}/{}".format(w.objectName(), name), w.property(name))
                    w.setProperty(name, val)


def save(settings):
    for w in qApp.allWidgets():
        mo = w.metaObject()
        if w.objectName() != "":
            for i in range(mo.propertyCount()):
                name = mo.property(i).name()
                settings.setValue("{}/{}".format(w.objectName(), name), w.property(name))

完整的示例已找到

非常感谢。这比我最初的解决方案更好。