简体   繁体   English

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

[英]PyQT5 and Python3 “exit(a.exec_())” NameError: name 'a' is not defined

I am new to python and trying to learn to write a GUI for a raspberry pi. 我是python的新手,正在尝试学习为树莓派编写GUI。 I am currently just following an online tutorial to create a GUI in QT designer. 我目前正在关注在线教程,以在QT设计器中创建GUI。 My QT designer has a custom widget that I added from another developer to display an LED in the GUI. 我的QT设计师有一个自定义窗口小部件,我从另一个开发人员添加了该窗口小部件以在GUI中显示LED。 The widget/module is qledplugin.py and saved in a "python" folder under the qt5.plugins.designer, and I saved the qled.py under .local.lib.python3.5.site-packages. 小部件/模块是qledplugin.py,保存在qt5.plugins.designer下的“ python”文件夹中,我将qled.py保存在.local.lib.python3.5.site-packages下。

I created a basic GUI and the file is saved as mainwindow.ui. 我创建了一个基本的GUI,并将文件另存为mainwindow.ui。 I used pyuic to convert it to python3 and saved as mainwindow.py. 我使用pyuic将其转换为python3并另存为mainwindow.py。 I then wrote a basic main program called main.py to launch the GUI. 然后,我编写了一个名为main.py的基本主程序以启动GUI。 There are no functions, it should simply load the GUI I created in a window. 没有功能,它应该只加载我在窗口中创建的GUI。 The problem I encounter is that when I run python3 main.py I get the following error 我遇到的问题是,当我运行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

The code for qled.py where the error occurs is 发生错误的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_())

at the top of the code for qled.py I have 在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

The code for main.py is 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' is defined right before the exit, but the program still sees it as undefined. “ a”在退出之前就已定义,但程序仍将其视为未定义。 I did have to modify some things for python3 since it was written in python2.7, but I'm new to python and maybe I missed something. 自从python3是用python2.7编写的以来,我确实不得不修改一些东西,但是我是python的新手,也许我错过了一些东西。 All help appreciated. 所有帮助表示赞赏。

Python is very strict with indentation. Python对缩进非常严格。 In both cases you have not respected it. 在两种情况下,您都没有尊重它。 In the first case exit (a.exec_()) is on the same level as if __name __ == "__ main__": , this should be inside. 在第一种情况下,出口(a.exec_())if __name __ == "__ main__":处于同一级别,该if __name __ == "__ main__":应在内部。 In the other case: 在另一种情况下:

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

It is at the same level as the functions of the class, and that is a serious error since it is not a class method; 它与类的功能处于同一级别,这是一个严重的错误,因为它不是类的方法。 You must move it out. 您必须将其移出。

qled.py 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 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM