PyQt4问题并登录到Python的主窗口

时间:2017-11-12 03:41:50

标签: python mysql user-interface pyqt4

我是PyQt和Python的新手(更熟悉Java),我试图创建一个将数据插入并检索到数据库中的应用程序。对于数据库的连接,我使用的是mysql连接器。我能够很好地插入和检索数据,但我不确定如何实现我的应用程序的GUI部分。我想要做的是有一个登录窗口连接到主窗口的数据库,然后将从文件读取的数据插入到数据库中,并根据用户选择的内容检索数据。点击"登录"应该关闭登录窗口并打开显示进度条的主窗口,并显示用户可以排序的结果(尚未实现。

我有什么方法可以改善我的计划?我的程序有时会挂起。

我怎么能继续我的方法呢?

在Python中是否有与Java的JFrame.dispose()相同的东西,然后关闭Window并单击按钮?

登录窗口:

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import QPushButton, QLabel
from PyQt4.QtGui import QPlainTextEdit
from PyQt4.QtGui import QLineEdit
from MainGUI import MainGUI
import time

class LoginGUI(QtGui.QMainWindow):

    def __init__(self):
        super(LoginGUI, self).__init__()
        self.setGeometry(730, 350, 500, 300)
        self.setWindowTitle("Login")
        self.initGUI()

    def initGUI(self):

        titleLabel = QLabel("Login", self)
        titleLabel.move(200, 20)
        titleLabel.setFont(QtGui.QFont("", 20))

        loginLabel = QLabel("Username: ", self)
        loginLabel.move(135, 120)

        passwordLabel = QLabel("Password: ", self)
        passwordLabel.move(135, 150)

        loginText = QPlainTextEdit("root", self)
        loginText.move(195, 120)

        passwordText = QLineEdit("",self)
        passwordText.move(195, 150)
        passwordText.setEchoMode(QtGui.QLineEdit.Password)

        loginBtn = QtGui.QPushButton("Sign in", self)
        loginBtn.clicked.connect(lambda: 
              self.connectToDB(loginText.toPlainText(), passwordText.text()))
        loginBtn.resize(loginBtn.sizeHint())
        loginBtn.move(170, 250)

        quitBtn = QtGui.QPushButton("Quit", self)
        quitBtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        quitBtn.resize(quitBtn.sizeHint())
        quitBtn.move(245,250)

        self.show()

    def connectToDB(self,username,password):
        pmg = MainGUI()
        pmg.prep("D:\\folder\\data.csv", username, password)
        pmg.run()
        #logonGUI.close()
        #mainApp = QtGui.QApplication(sys.argv)
        #mainGUI = MainGUI()
        #sys.exit(app.exec_())
        #return pmg      

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    logonGUI = LoginGUI()
    sys.exit(app.exec_())  

主窗口:

other imports
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.Qt import QPushButton, QLabel
from PyQt4.QtGui import QPlainTextEdit
from PyQt4.QtCore import QEventLoop
from viewer.DataSource import DataSource

class MainGUI(QtGui.QMainWindow):

    theFile = None

    username = None

    password = None

    source = None

    con = None

    rowsInserted = 0

    progressBar = None

    completed = 0

    def __init__(self):

        #app = QtGui.QApplication(sys.argv)
        #mainGUI = MainGUI()
        #sys.exit(app.exec_()).

        super(MainGUI, self).__init__()
        self.setGeometry(730, 350, 1000, 600)
        self.setWindowTitle("MainWindow")
        self.initGUI()
        #self.show()


    def prep(self, x, username, password):
        try:
            self.theFile = open(x, "r")

            self.username = username

            self.password = password

            #Connect to db and pass connection to each class.
            #Close connection at the end in a Finally statement.

            self.source = DataSource()

            self.con = self.source.getConnection(username, password)


        except FileNotFoundError:
            print("No file of {} found.".format(x))  

    def initGUI(self):
            titleLabel = QLabel("MainWindow", self)
            titleLabel.resize(200, 20)
            titleLabel.move(450, 30)
            titleLabel.setFont(QtGui.QFont("", 20))
            quitBtn = QtGui.QPushButton("Quit", self)
            quitBtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
            quitBtn.resize(quitBtn.sizeHint())
            quitBtn.move(800,550)

            self.progressBar = QtGui.QProgressBar(self)
            self.progressBar.setGeometry(200, 80, 250, 20)

    def run(self):        

        with self.theFile as data:
            lines = data.readlines()[1:]
            for line in lines:
            QtCore.QCoreApplication.processEvents(flags=QEventLoop.AllEvents)
            #Line with QtCore supposed to be indented.
            cleanDataFromDB(self.con)
            insertData(self.con)

dao.retrieve(userInput)

            try:
                if self.con != None:
                    self.con.close()
            except:
                print("Error closing the database.")    

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 我已经使我的MainGUI全局实例无法关闭。

def connectToDB(self,username,password):
    global pmg
    pmg = MainGUI()
    pmg.prep("D:\\folder\\data.csv", username, password)
    pmg.run()
    self.close()

我已经对我的MainGUI本身进行了一些更改,在initGUI()之前和run(()循环之后向标题和按钮添加了一些show()语句,并在show()之后添加了repaint()循环。

def initGUI(self):
        titleLabel = QLabel("MainWindow", self)
        titleLabel.resize(200, 20)
        titleLabel.move(450, 30)
        titleLabel.setFont(QtGui.QFont("", 20))
        titleLabel.hide()

        quitBtn = QtGui.QPushButton("Quit", self)
        quitBtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        quitBtn.resize(quitBtn.sizeHint())
        quitBtn.move(800,550)
        quitBtn.hide()

        self.progressBar = QtGui.QProgressBar(self)
        self.progressBar.setGeometry(200, 80, 250, 20)
        self.progressBar.show()


def run(self):        

    with self.theFile as data:
        lines = data.readlines()[1:]
        for line in lines:
           QtCore.QCoreApplication.processEvents()
           cleanDataFromDB(self.con)
           insertData(self.con)
        progressBar.hide()
        titleLabel.show()
        quitBtn.show()
        self.repaint()

感谢您抽出宝贵时间帮助我。 :d