简体   繁体   English

PyQt4问题和使用Python登录到主窗口的窗口

[英]PyQt4 Questions and Login WIndow to Main Window in Python

I'm new to PyQt and Python as a whole (more familiar with Java) and I'm trying to make an application where data is inserted and retrieved into the database. 我是PyQt和Python整体上的新手(对Java更加熟悉),我正在尝试创建一个将数据插入并检索到数据库中的应用程序。 For the connection to the database, I'm using mysql connector. 对于与数据库的连接,我正在使用mysql连接器。 I'm able to insert and retrieve data just fine but I'm not sure how to implement the GUI portion of my application. 我可以很好地插入和检索数据,但是我不确定如何实现应用程序的GUI部分。 What I want to do is have a login window that connected to the database for the main window to then insert data read from the file into the database and retrieve data based on what was selected by the user. 我想要做的是有一个登录窗口,该窗口连接到数据库的主窗口,然后将从文件中读取的数据插入数据库,并根据用户选择的内容检索数据。 Clicking "Sign in" should close the Login Window and open the Main Windows which shows a progress bar while inserting and should display results which users can sort by ( have not implemented it yet. 单击“登录”应关闭“登录”窗口并打开主窗口,该窗口在插入时显示进度条,并应显示用户可以按其排序的结果(尚未实现)。

What are ways I could improve my program? 有什么方法可以改善我的程序? My program sometimes hangs. 我的程序有时会挂起。

How could I go about my approach? 我该如何处理?

Is there an equivalent to Java's JFrame.dispose() in Python which closes then Window and clicking on a button? 是否有等效于Python中Java的JFrame.dispose()的函数,该函数关闭然后关闭Window并单击一个按钮?

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

The Main Window: 主窗口:

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.")    

I have found my solution. 我找到了解决方案。 I have made my instance of the MainGUI global so that it doesn't close. 我已经将MainGUI实例设为全局,因此不会关闭。

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

I have made some changes to my MainGUI itself by adding some show() statements to the title and button for before in initGUI() and after the loop of run(), and having repaint() after the show() after the loop. 我对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()

Thanks for taking your time to help me. 感谢您抽出宝贵的时间来帮助我。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM