python3 exec not defined_PyQT5 and Python3 "exit(a.exec_())" NameError: name 'a' is not defined

解决Python GUI中Qt LED插件错误
一位Python新手在尝试为Raspberry Pi创建GUI时遇到问题。使用Qt Designer及自定义LED插件时,在运行主程序main.py时出现名称'a'未定义的错误。本文详细介绍了项目的目录结构及代码配置,并寻求帮助解决该问题。

2017-01-05 17:52:50

0

I am new to python and trying to learn to write a GUI for a raspberry pi. I am currently just following an online tutorial to create a GUI in QT designer. My QT designer has a custom widget that I added from another developer to display an LED in the GUI. The widget/module is qledplugin.py and saved in a "python" folder under the qt5.plugins.desinger, and I saved the qled.py under .local.lib.python3.5.site-packages.

I created a basic GUI and the file is saved as mainwindow.ui. I used pyuic to convert it to python3 and saved as mainwindow.py. I then wrote a basic main program called main.py to launch the GUI. There are no functions, it should simply load the GUI I created in a window. The problem I encounter is that when I run python3 main.py I get the following error

myself@my-own-computer:~/Programming/Projects/GenUi$ python3 main.py

Traceback (most recent call last):

File "main.py", line 6, in

import mainwindow

File "/home/clint/Programming/Projects/GenUi/mainwindow.py", line 86, in

from qled import QLed

File "/home/clint/.local/lib/python3.5/site-packages/qled.py", line 398, in

exit(a.exec_())

NameError: name 'a' is not defined

The code for qled.py where the error occurs is

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

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

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. 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. All help appreciated.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值