PyQT5和Python3“退出(a.exec_())”NameError:名称'a'未定义

时间:2017-01-05 17:52:50

标签: python python-3.x pyqt pyqt5 qt-designer

我是python的新手,并试图学习为覆盆子pi编写GUI。我目前正在按照在线教程在QT设计器中创建GUI。我的QT设计师有一个自定义小部件,我从其他开发人员添加到GUI中显示LED。小部件/模块是qledplugin.py并保存在qt5.plugins.designer下的“python”文件夹中,我将qled.py保存在.local.lib.python3.5.site-packages下。

我创建了一个基本的GUI,文件保存为mainwindow.ui。我使用pyuic将其转换为python3并保存为mainwindow.py。然后我编写了一个名为main.py的基本主程序来启动GUI。没有功能,它应该只是加载我在窗口中创建的GUI。我遇到的问题是,当我运行python3 main.py时,我收到以下错误

myself@my-own-computer:~/Programming/Projects/GenUi$ python3 main.py
    Traceback (most recent call last):
      File "main.py", line 6, in <module>
        import mainwindow
      File "/home/clint/Programming/Projects/GenUi/mainwindow.py", line 86, in <module>
        from qled import QLed
      File "/home/clint/.local/lib/python3.5/site-packages/qled.py", line 398, in <module>
        exit(a.exec_())
    NameError: name 'a' is not defined

发生错误的qled.py代码是

if __name__=="__main__":
    from sys import argv, exit
    import sys

    class Test(QWidget):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)

            self.setWindowTitle("QLed Test")

            _l=QGridLayout()
            self.setLayout(_l)

            self.leds=[]
            for row, shape in enumerate(QLed.shapes.keys()):
                for col, colour in enumerate(QLed.colours.keys()):
                    if colour==QLed.Grey: continue
                    led=QLed(self, onColour=colour, shape=shape)
                    _l.addWidget(led, row, col, Qt.AlignCenter)
                    self.leds.append(led)

            self.toggleLeds()

        def toggleLeds(self):
            for led in self.leds: led.toggleValue()
            QTimer.singleShot(1000, self.toggleLeds)

    a = QApplication(sys.argv)
    t = Test()
    t.show()
    t.raise_()
exit(a.exec_())

在qled.py代码的顶部我有

from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QSizePolicy, QStyleOption
from PyQt5.QtCore import pyqtSignal, Qt, QSize, QTimer, QByteArray, QRectF, pyqtProperty
from PyQt5.QtSvg import QSvgRenderer
from PyQt5.QtGui import QPainter

main.py的代码是

import sys
import PyQt5

from PyQt5.QtWidgets import *

import mainwindow

class MainWindow(QMainWindow, mainwindow.Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
    self.setupUi(self)

    def main():
        app = QApplication(sys.argv)
        form = MainWindow()
        form.show()
sys.exit(app.exec_())

if __name__ == "__main__":
    main()

'a'在退出之前定义,但程序仍将其视为未定义。我确实需要为python3修改一些东西,因为它是用python2.7编写的,但我是python的新手,也许我错过了一些东西。所有帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

Python对缩进非常严格。在这两种情况下,你都没有尊重它。在第一种情况下,退出(a.exec_())if __name __ == "__ main__":处于同一级别,这应该在内部。在另一种情况下:

def main():
        app = QApplication(sys.argv)
        form = MainWindow()
        form.show()

它与类的功能处于同一级别,这是一个严重的错误,因为它不是类方法;你必须把它搬出去。

<强> qled.py

if __name__=="__main__":
    from sys import argv, exit
    import sys

    class Test(QWidget):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)

            self.setWindowTitle("QLed Test")

            _l=QGridLayout()
            self.setLayout(_l)

            self.leds=[]
            for row, shape in enumerate(QLed.shapes.keys()):
                for col, colour in enumerate(QLed.colours.keys()):
                    if colour==QLed.Grey: continue
                    led=QLed(self, onColour=colour, shape=shape)
                    _l.addWidget(led, row, col, Qt.AlignCenter)
                    self.leds.append(led)

            self.toggleLeds()

        def toggleLeds(self):
            for led in self.leds: led.toggleValue()
            QTimer.singleShot(1000, self.toggleLeds)

    a = QApplication(sys.argv)
    t = Test()
    t.show()
    t.raise_()
    exit(a.exec_())

<强> main.py

import sys
import PyQt5

from PyQt5.QtWidgets import *

import mainwindow

class MainWindow(QMainWindow, mainwindow.Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
    self.setupUi(self)

def main():
    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()