2017-11-12 3 views
0

전체적으로 PyQt와 Python을 처음 접했고 (자바에 익숙 함) 데이터를 삽입하고 데이터베이스로 가져 오는 응용 프로그램을 만들려고합니다. 데이터베이스에 연결하기 위해 mysql 커넥터를 사용하고있다. 삽입하고 데이터를 잘 검색 할 수 있지만 응용 프로그램의 GUI 부분을 구현하는 방법을 잘 모르겠습니다. 내가하고 싶은 일은 주 창에 대한 데이터베이스에 연결된 로그인 창을 사용하여 파일에서 읽은 데이터를 데이터베이스에 삽입하고 사용자가 선택한 것을 기반으로 데이터를 검색하는 것입니다. "로그인"을 클릭하면 로그인 창이 닫히고 삽입하는 동안 진행률 막대를 표시하는 메인 윈도우가 열리고 사용자가 정렬 할 수있는 결과가 표시되어야합니다 (아직 구현하지 않았습니다).PyQt4 질문 및 로그인 Python의 기본 창으로 이동

프로그램을 개선 할 수있는 방법은 무엇인가요? ? 내 프로그램은 때때로 중단됩니다.

내 접근 방식에 대해 갈 수 있습니까?

다음 창 닫 파이썬에서 자바의 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.")  
+0

'your_button.clicked.connect (your_window.close) '사용 – eyllanesc

+0

내 람다가 호출 한 함수에 logonGUI.close()를 사용했지만 작동하지 않았습니다. – Brock

+0

'logonGUI.close()'를'self.close()'로 변경하십시오. – eyllanesc

답변

0

내 해결책을 발견했다. MainGUI의 인스턴스를 닫지 않도록 전역으로 설정했습니다. 애프터) (

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

나는) 전에 initGUI (에 대한 제목과 버튼에 문을 몇 가지 쇼를 (추가) 및 실행()의 루프 후 내 MainGUI 자체에 약간의 변경을했고, 가지는 칠 루프 다음에 show().

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