Python NameError:全局名称“Form”没有定义pyqt

时间:2022-01-09 19:04:56

I am trying to design Garage App. The app has two options (Sign IN, Sign Up) and this Widget called 'Welcome Window' when user chooses Sign IN.

我正在尝试设计Garage App,这个App有两个选项(签到,签到),当用户选择签到的时候这个小部件叫做“欢迎窗口”。

The program should close 'Welcome Window' and open anther widget called 'Log In', then the 'Log In' widget has two push buttons: 'Ok' and 'Back'. When the user presses 'Back', it should Close 'Log In' widget and reopen 'Welcome Window'. But it open 'Welcome window' without close 'Login window' and raises the error

程序应该关闭“欢迎窗口”,打开另一个名为“登录”的小部件,然后“登录”小部件有两个按钮:“Ok”和“Back”。当用户按下'Back'时,它应该关闭'Log In' widget并重新打开'Welcome Window'。但它打开“欢迎窗口”,没有关闭“登录窗口”,并引发错误。

NameError: global name 'Form' is not defined code of 'WelcomeWidget'

NameError:全局名称“表单”并不是“WelcomeWidget”的定义代码

from PyQt4 import QtCore, QtGui
import SignIN

try:
     _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
     def _fromUtf8(s):
         return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, 
_encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def SignINF(self):
        self.SignINWindow = QtGui.QWidget()
        self.ui = SignIN.Ui_Form()
        self.ui.setupUi(self.SignINWindow)
        self.SignINWindow.show()
        Form.close()
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(500, 360)
        Form.setStyleSheet(_fromUtf8("background-image: 
url(:/img/Screenshot from 2017-09-20 17-37-17.png);"))
        self.pushButton_2 = QtGui.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(120, 290, 75, 31))
        self.pushButton_2.setStyleSheet(_fromUtf8("color: rgb(255, 
255, 255);\n"
"font: 63 oblique 12pt \"URW Gothic L\";"))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_2.clicked.connect(self.SignINF)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(29, 291, 75, 31))
        self.pushButton.setStyleSheet(_fromUtf8("color: rgb(255, 255, 
255);\n"
"font: 63 oblique 12pt \"URW Gothic L\";"))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(30, 50, 458, 34))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("DejaVu Serif"))
        font.setPointSize(22)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(9)
        self.label.setFont(font)
        self.label.setStyleSheet(_fromUtf8("color: rgb(255, 255, 
255);\n"
"font: 75 22pt \"DejaVu Serif\";\n"
""))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(360, 330, 131, 20))
        self.label_2.setStyleSheet(_fromUtf8("color: rgb(255, 255, 
255);\n"
"font: 63 italic 10pt \"URW Chancery L\";"))
        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton_2.setText(_translate("Form", "Sign In", None))
        self.pushButton.setText(_translate("Form", "Sign Up", None))
        self.label.setText(_translate("Form", "Welcome To Our Smart 
Garage", None))
        self.label_2.setText(_translate("Form", "Power By AIET 
Students", None))

import iamge_rc

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

Code for 'Sign In Widget'

“在小部件中签名”代码

from PyQt4 import QtCore, QtGui
import WelFrame

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def Back(self):
        self.WelcomWindow = QtGui.QWidget()
        self.ui = WelFrame.Ui_Form()
        self.ui.setupUi(self.WelcomWindow)
        self.WelcomWindow .show()
        Form.close()
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(500, 360)
        Form.setStyleSheet(_fromUtf8("background-image: url(:/img/Screenshot from 2017-09-20 17-37-17.png);"))
        self.gridLayout = QtGui.QGridLayout(Form)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.label = QtGui.QLabel(Form)
        self.label.setStyleSheet(_fromUtf8("color: rgb(255, 255, 255);\n"
"font: 63 oblique 12pt \"URW Gothic L\";"))
        self.label.setObjectName(_fromUtf8("label"))
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
        self.lineEdit_2 = QtGui.QLineEdit(Form)
        self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
        self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
        self.pushButton_2 = QtGui.QPushButton(Form)
        self.pushButton_2.setStyleSheet(_fromUtf8("color: rgb(255, 255, 255);\n"
"font: 63 oblique 12pt \"URW Gothic L\";"))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_2.clicked.connect(self.Back)
        self.gridLayout.addWidget(self.pushButton_2, 2, 0, 1, 1)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setStyleSheet(_fromUtf8("color: rgb(255, 255, 255);\n"
"font: 63 oblique 12pt \"URW Gothic L\";"))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.gridLayout.addWidget(self.pushButton, 2, 1, 1, 1)
        self.label_2 = QtGui.QLabel(Form)
        self.label_2.setStyleSheet(_fromUtf8("color: rgb(255, 255, 255);\n"
"font: 63 oblique 12pt \"URW Gothic L\";"))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.label.setText(_translate("Form", "Enter User Name", None))
        self.pushButton_2.setText(_translate("Form", "Cancel", None))
        self.pushButton.setText(_translate("Form", "OK", None))
        self.label_2.setText(_translate("Form", "Enter Password", None))

import iamge_rc

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

import SignIn -> code of SignIN

导入登录->登录代码

import WelFrame-> code of Welcome Window

导入WelFrame->欢迎窗口代码

1 个解决方案

#1


0  

I continually recommend that you do not modify the files that Qt Designer creates. These classes are oriented to the design so the most elegant thing is to create a class that implements the logic and use the previous design, the classes must have the following structure:

我一直建议您不要修改Qt设计器创建的文件。这些类是面向设计的,所以最优雅的事情是创建一个实现逻辑的类并使用以前的设计,这些类必须具有以下结构:

class LogicClass(QWidget, Ui_SomeDesign):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)

So I recommend you remove all the modifications to your design classes or create them using pyuic.

因此,我建议您删除所有对设计类的修改,或者使用pyuic创建它们。

In your case I recommend creating another file called main.py that is in the same folder as your other scripts:

在您的情况下,我建议创建另一个名为main的文件。py与其他脚本在同一个文件夹中:

.
├── main.py <-- new file
├── SignIn.py
└── WelFrame.py

In this file we create the classes that implement the logic for your designs and implement a method that performs the task of closing the current widget and displaying the new widget

在这个文件中,我们创建了实现设计逻辑的类,并实现了一个方法,该方法执行关闭当前小部件并显示新小部件的任务

import sys
from PyQt4.QtGui import QWidget, QApplication

from SignIn import Ui_Form as SignInForm
from WelFrame import Ui_Form as WelFrameForm


class Sign(QWidget, SignInForm):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)


class WelFrame(QWidget, WelFrameForm):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)

def foo(w1, w2):
    w1.show()
    w2.hide()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    wel = WelFrame()
    sign = Sign()
    wel.pushButton_2.clicked.connect(lambda : foo(sign, wel))
    sign.pushButton_2.clicked.connect(lambda: foo(wel, sign))
    wel.show()
    sys.exit(app.exec_())

#1


0  

I continually recommend that you do not modify the files that Qt Designer creates. These classes are oriented to the design so the most elegant thing is to create a class that implements the logic and use the previous design, the classes must have the following structure:

我一直建议您不要修改Qt设计器创建的文件。这些类是面向设计的,所以最优雅的事情是创建一个实现逻辑的类并使用以前的设计,这些类必须具有以下结构:

class LogicClass(QWidget, Ui_SomeDesign):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)

So I recommend you remove all the modifications to your design classes or create them using pyuic.

因此,我建议您删除所有对设计类的修改,或者使用pyuic创建它们。

In your case I recommend creating another file called main.py that is in the same folder as your other scripts:

在您的情况下,我建议创建另一个名为main的文件。py与其他脚本在同一个文件夹中:

.
├── main.py <-- new file
├── SignIn.py
└── WelFrame.py

In this file we create the classes that implement the logic for your designs and implement a method that performs the task of closing the current widget and displaying the new widget

在这个文件中,我们创建了实现设计逻辑的类,并实现了一个方法,该方法执行关闭当前小部件并显示新小部件的任务

import sys
from PyQt4.QtGui import QWidget, QApplication

from SignIn import Ui_Form as SignInForm
from WelFrame import Ui_Form as WelFrameForm


class Sign(QWidget, SignInForm):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)


class WelFrame(QWidget, WelFrameForm):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)

def foo(w1, w2):
    w1.show()
    w2.hide()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    wel = WelFrame()
    sign = Sign()
    wel.pushButton_2.clicked.connect(lambda : foo(sign, wel))
    sign.pushButton_2.clicked.connect(lambda: foo(wel, sign))
    wel.show()
    sys.exit(app.exec_())