gpt4 book ai didi

python - PyQt4 问题和在 Python 中登录 WIndow 到主窗口

转载 作者:行者123 更新时间:2023-11-29 18:13:02 25 4
gpt4 key购买 nike

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

我可以通过哪些方法来改进我的程序?我的程序有时会挂起。

我该如何实现我的方法?

Python 中是否有与 Java 的 JFrame.dispose() 等效的方法,它可以关闭窗口并单击按钮?

登录窗口:

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

最佳答案

我已经找到了解决方案。我已将 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

关于python - PyQt4 问题和在 Python 中登录 WIndow 到主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47245218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com