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

被折叠的 条评论
为什么被折叠?



