簡體   English   中英

python NameError:全局名稱'Form'未定義pyqt

[英]Python NameError: global name 'Form' is not defined pyqt

我正在嘗試設計Garage App。 該應用程序有兩個選項(登錄,注冊),當用戶選擇登錄時,此小組件稱為“歡迎窗口”。

程序應關閉“歡迎窗口”並打開另一個名為“登錄”的小部件,然后“登錄”小部件具有兩個按鈕:“確定”和“返回”。 當用戶按下“返回”時,應關閉“登錄”小部件並重新打開“歡迎窗口”。 但是它打開“歡迎窗口”卻沒有關閉“登錄窗口”並引發錯誤

NameError:全局名稱“ Form”不是“ 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_())

“登錄小部件”的代碼

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_())

導入登錄->登錄代碼

導入WelFrame->歡迎窗口代碼

我一直建議您不要修改Qt Designer創建的文件。 這些類是針對設計的,因此最優雅的方法是創建一個實現邏輯並使用先前設計的類,這些類必須具有以下結構:

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

因此,我建議您刪除對設計類的所有修改或使用pyuic創建它們。

對於您的情況,我建議創建另一個名為main.py的文件,該文件與其他腳本位於同一文件夾中:

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

在此文件中,我們創建用於實現您的設計邏輯的類,並實現一種用於執行關閉當前小部件並顯示新小部件的任務的方法。

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_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM