Python DD.close()name错误:未定义全局名称“DD”

Python DD.close()name错误:未定义全局名称“DD”,python,pyqt,pyqt4,Python,Pyqt,Pyqt4,pyqt4,成功登录后,我的登录窗口关闭,下一个窗口打开。i、 e很好,但如果我想关闭下一个窗口并转到登录窗口,则显示错误 这是我的密码 Login.py import DD from PyQt4.QtGui import QMessageBox from PyQt4.QtGui import * from LoginError import LoginError import os class Ui_Login(object): def closeEvent(self, e

pyqt4,成功登录后,我的登录窗口关闭,下一个窗口打开。i、 e很好,但如果我想关闭下一个窗口并转到登录窗口,则显示错误

这是我的密码

Login.py

import DD

from PyQt4.QtGui import QMessageBox

from PyQt4.QtGui import *

from LoginError import LoginError

import os

class Ui_Login(object):

    def closeEvent(self, event):
        # type: (close) -> object
        QtGui.QMainWindow.closeEvent(self, event)
        self.saveLayout()


    def loginCheck(self):

        username = self.lineEdit_adminusername.text()
        password = self.lineEdit_adminpassword.text()
        for line in open("password.txt", "r").readlines():  # Read the lines
            # str = in.readlines();
            # str1 = in.readlines();
            loginCheck_info = line.split()  # Split on the space, and store the results in a list of two strings
            # if username == str and password == str1:
            if username == loginCheck_info[0] and password == loginCheck_info[1]:
                print("Correct credentials!")
                try:
                    self.window = QtGui.QMainWindow()
                    self.ui = DD.Ui_DD()
                    self.ui.setupUi(self.window)
                    self.window.show()
                    Login.close()
                    return True
                except LoginError:
                    sys.exit()
                sys.exit(app.exec_())


            else:
                print "Incorrect credentials!!!"
                widget = QWidget()
                message = QtGui.QMessageBox.warning(widget, "Warning", "Please enter valid username and password")
                return False
                # print("Incorrect credentials.")
                # return False
            line.close()

    def reboot(self, event):
        widget = QWidget()
        message = QtGui.QMessageBox.question(widget, "Reboot", "Do you really want to Reboot?",
                                             QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            print "Rebooting..........."
            os.system("reboot")

        else:
            print "Continue"
            global true;
            true = 1;

    def shutdown(self, event):
        widget = QWidget()
        message = QtGui.QMessageBox.question(widget, "Shutdown", "Do you really want to Shutdown?",
                                             QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            print "Going to shutting down..........."
            os.system("poweroff")

        else:
            print "Continue"
            global true;
            true = 1;

        self.btn_adminlogin = QtGui.QPushButton(self.frame)
        self.btn_adminlogin.setObjectName(_fromUtf8("btn_adminlogin"))
        self.btn_adminlogin.clicked.connect(self.loginCheck)
        self.gridLayout_2.addWidget(self.btn_adminlogin, 1, 0, 1, 1)
        self.gridLayout_3.addLayout(self.gridLayout_2, 0, 0, 1, 1)
迪比


我可以看出你的计划有两个主要问题

首先,为什么这两个Ui相互依赖?您已经在Login.py中导入DD并在DD.py中导入Login,这对我来说毫无意义。您应该做的是创建第三个文件,比如Main.py,并将Login和DD导入其中。然后你可以很容易地从一个窗口跳到另一个窗口

其次,在DD.py中,没有名为DD的对象。您的类名为Ui_DD,它位于DD.py中。所以,如果调用DD.close,python应该认为DD是什么?因此,投诉是错误的


简单的一句话:考虑一个叫XYZ公司的公司,一个叫P.先生的雇员,你想通过说XYZ公司来摆脱P先生,滚出去!这毫无意义。在您的例子中,DD.py是XYZ Inc,Ui_DD是P先生。

我是python的新手,请帮助我。您能更具体地说明错误,以及您猜测它失败的原因吗?不管怎样,我认为如果次要窗口由主要对象以任何方式维持,它可能是。但是不知道。错误是正确的!退出回溯最近一次调用最后一次:File/home/jai bholenath/Downloads/withBlueshade/DD.py,第73行,在close DD.close name错误:全局名称“DD”未定义登录完成并移动到第二个窗口并正确关闭登录窗口。但如果注销第二个未关闭的窗口。我知道有一些愚蠢的逻辑,但我不明白这是因为你正在做DD.close,但这只有当你从主对象导入DD时才有意义。关闭上下文中不存在的内容。只要找到关闭自己的方法。我不知道python,但是像self.window.close这样的东西?然后再次启动主程序。谢谢马库斯先生。我会尝试你的想法,并尽快回复。感谢您创建了一个新文件main.py,并在主文件中调用这两个文件。现在您可以将登录名和其他小部件放在一个循环中,可以这么说。太好了。我建议您编写一个简单的代码,说明它如何解决您的问题并接受它。通过这种方式,您和网站都会增长…:
from PyQt4 import QtCore, QtGui

import Login

from PyQt4.QtGui import QMessageBox

from PyQt4.QtGui import *

import os

import time

class Ui_DD(object):

    def Start(self):
        global true;
        true = 1;
        print "Start"
        widget = QWidget()
        message = QtGui.QMessageBox.information(widget, "Welcome", "Data Transfer Start")
        while true == 1:
            path = "/home/greensystem/PycharmProjects/data/"
            itemsTextList = 0
            for f in os.listdir(path):
                if os.path.isfile(os.path.join(path, f)):
                    itemsTextList += 1

            print "Total number of files in: " + path + " -", itemsTextList
            time.sleep(1)
            out = str(itemsTextList)
            self.textBrowser.setText(out)
            QtGui.qApp.processEvents()

    def Stop(self):
        global true;
        true = 0;
        print "Stop"
        widget = QWidget()
        QMessageBox.warning(widget, "Welcome", "Data Transfer Stop")
        # sys.exit()


    def close(self, event):
        #global DD
        widget = QWidget()
        message = QtGui.QMessageBox.question(widget, "Quit", "Do you really want to quit?",
                                             QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            print "exit"
            self.window = QtGui.QMainWindow()
            self.ui = Login.Ui_Login()
            self.ui.setupUi(self.window)
            self.window.show()
            #DD.hide()
            DD.close()


        else:
            print "Continue"
            global true;
            true = 1;

        self.label_7 = QtGui.QLabel(self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(10, 460, 131, 41))
        self.label_7.setStyleSheet(_fromUtf8("image: url(:/newPrefix/index.jpeg);"))
        self.label_7.setText(_fromUtf8(""))
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.label_7.mousePressEvent = self.close