2016-09-14 4 views
0

두 파일 즉, main.pyclient_window.py이 있습니다. 진행률 표시 줄이 완료되면 클라이언트 창을 표시하려고합니다. 그러나 클라이언트 창이 즉시 열고 닫는 것을 알게되었습니다. 누군가가 코드를 변경하는 방법을 제안 할 수 있으므로 올바른 논리를 파악할 수 있습니까?열기 후 PyQt 창이 닫힙니다.

#client_window.py 

import sys 
from PyQt4 import QtCore 

from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class myListWidget(QListWidget): 
    def Clicked(self, item): 
     QMessageBox.information(self, "ListWidget", "You clicked: " + item.text()) 


class Example(QWidget): 
    def __init__(self): 
     super(Example, self).__init__() 
     self.listWidget = None 
     self.initUI() 

    def initUI(self): 
     hbox = QHBoxLayout(self) 

     topleftLayout = QVBoxLayout() 
     topleftLayout.setAlignment(QtCore.Qt.AlignTop) 
     topleft = QFrame() 
     topleft.setFrameShape(QFrame.StyledPanel) 
     label = QLabel() 
     label.setText("SERVER LIST") 
     label.setAlignment(QtCore.Qt.AlignCenter) 
     topleftLayout.addWidget(label) 
     self.addServerList(topleftLayout) 
     topleft.setLayout(topleftLayout) 
     bottom = QFrame() 
     bottom.setFrameShape(QFrame.StyledPanel) 

     splitter1 = QSplitter(Qt.Horizontal) 
     textedit = QTextEdit() 
     splitter1.addWidget(topleft) 
     splitter1.addWidget(textedit) 
     splitter1.setSizes([100, 200]) 

     splitter2 = QSplitter(Qt.Vertical) 
     splitter2.addWidget(splitter1) 
     splitter2.addWidget(bottom) 
     splitter2.setSizes([225, 100]) 

     hbox.addWidget(splitter2) 

     self.setLayout(hbox) 
     QApplication.setStyle(QStyleFactory.create('Cleanlooks')) 

     self.setGeometry(300, 300, 300, 200) 
     self.setWindowTitle('QSplitter demo') 
     self.showMaximized() 

    def addServerList(self, layout): 
     self.listWidget = myListWidget() 
     self.listWidget.resize(300, 120) 

     self.listWidget.addItem("Item 1"); 
     self.listWidget.addItem("Item 2"); 
     self.listWidget.addItem("Item 3"); 
     self.listWidget.addItem("Item 4"); 

     self.listWidget.setWindowTitle('PyQT QListwidget Demo') 
     self.listWidget.itemClicked.connect(self.listWidget.Clicked) 
     layout.addWidget(self.listWidget) 


def main(): 
    ex = Example() 
    ex.showMaximized() 
    return ex 


if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    main() 
    sys.exit(app.exec_()) 

나는에 client_window에 전화를 만들고있어 :

def onProgress(self, i): 
    self.progressBar.setValue(i) 
    if(self.progressBar.value() == 100): 
     #app = QApplication(sys.argv) 
     #global ex 
     ex = client_window.main() 
     ex.show() 
     #ex.main() 
     #ex.show() 
     #sys.exit(app.exec_()) 
    else: 
     self.tryAgain.setEnabled(True) 
     self.tryAgain.clicked.connect(self.onStart) 
     self.label.setText("Cannot connect to Server! Please Try Again!") 
다음과 같이이

""" 
This module defines the User Interface 
""" 

import sys 
import time 

from cloudL.ui import client_window 

from PyQt4 import QtGui 

from PyQt4 import QtCore 
from PyQt4.QtGui import * 


class ProgressBarWidget(QtGui.QDialog): 

    def __init__(self, parent=None): 
     super(ProgressBarWidget, self).__init__(parent) 
     layout = QtGui.QVBoxLayout(self) 

     self.label = QLabel() 
     self.tryAgain = QPushButton("Try Again") 
     self.progressBar = QtGui.QProgressBar(self) 
     self.progressBar.setRange(0,100) 
     self.tryAgain.setEnabled(False) 

     layout.addWidget(self.label) 
     layout.addWidget(self.progressBar) 
     layout.addWidget(self.tryAgain) 

     self.myLongTask = TaskThread() 
     self.myLongTask.notifyProgress.connect(self.onProgress) 
     self.myLongTask.start() 

    def onStart(self): 
     self.tryAgain.setEnabled(False) 
     self.myLongTask.start() 
     self.label.setText("") 

    def onProgress(self, i): 
     self.progressBar.setValue(i) 
     if(self.progressBar.value() == 100): 
      #app = QApplication(sys.argv) 
      #global ex 
      ex = client_window.main() 
      ex.show() 
      #ex.main() 
      #ex.show() 
      #sys.exit(app.exec_()) 
     else: 
      self.tryAgain.setEnabled(True) 
      self.tryAgain.clicked.connect(self.onStart) 
      self.label.setText("Cannot connect to Server! Please Try Again!") 


class TaskThread(QtCore.QThread): 
    notifyProgress = QtCore.pyqtSignal(int) 
    def run(self): 
     for i in range(101): 
      self.notifyProgress.emit(i) 
      time.sleep(0.005) 

class MainWindow(QWidget): 
    """ 
    This window will be the first screen to appear 
    """ 

    def __init__(self, parent = None): 
     super(MainWindow, self).__init__(parent) 
     """ 
     Initializes parameters required by the MainWindow 
     """ 
     self.option = ""  # to specify the if the selected option is Server or Client 
     self.ex = None 

    def main(self): 
     """ 
     This function is the origin where the initiation of the control takes place 
     :return: Returns nothing 
     """ 

     mainLayout = QVBoxLayout() 
     mainLayout.setAlignment(QtCore.Qt.AlignCenter) 

     radioLayout = QHBoxLayout() 
     mainLayout.setAlignment(QtCore.Qt.AlignCenter) 

     label = QLabel() 
     label.setText("Select whether to act as a Server or a Client") 
     labelFont = QFont() 
     labelFont.setBold(True) 
     labelFont.setPointSize(15) 
     label.setFont(labelFont) 

     mainLayout.addWidget(label) 
     mainLayout.addStretch() 

     b1 = QRadioButton("Server") 
     b1.setChecked(True) 
     b1.toggled.connect(lambda : btnstate(b1)) 
     radioLayout.addWidget(b1) 

     b2 = QRadioButton("Client") 
     b2.toggled.connect(lambda : btnstate(b2)) 
     radioLayout.addWidget(b2) 

     self.option = "" 

     def btnstate(b): 
      """ 
      Nested function to define the action when radio button is toggled 
      :param b: Button which is toggled currently 
      :return: Returns nothing 
      """ 
      if b.text() == "Server" and b.isChecked() == True: 
       self.option = "Server" 

      if b.text() == "Client" and b.isChecked() == True: 
       self.option = "Client" 

     start = QPushButton("Start") 

     mainLayout.addLayout(radioLayout) 
     mainLayout.addStretch() 
     mainLayout.addWidget(start) 

     self.setLayout(mainLayout) 
     self.setGeometry(400, 200, 300, 300) 
     self.setWindowTitle("CloudL") 

     start.clicked.connect(self.showdialog) 

    def showdialog(self): 
     """ 
     A sample message box to check and display which option is chosen 
     :return: Returns nothing 
     """ 
     #app = QApplication(sys.argv) 
     self.ex = ProgressBarWidget(self) 
     self.ex.setGeometry(400, 200, 500, 150) 
     self.destroy() 
     self.ex.setWindowTitle("CloudL") 
     self.ex.show() 


""" 
Execute the required action 
""" 
app = QApplication(sys.argv) 
ex = MainWindow() 
ex.main() 
ex.show() 
sys.exit(app.exec_()) 

그리고 client_window.py :

main.py입니다

또한 if inc 그렇지 않으면 가비지 수집 빨리 onProgress 반환과 같은 것입니다, 당신은 클라이언트 창에 대한 참조를 유지할 필요가

QCoreApplication::exec: The event loop is already running pyqt error

+0

작성 및 위젯을 표시하기 전에 당신이 을 응용 프로그램 = QtGui.QApplication (sys.argv에)가 있어야합니다 PyQt는에 창을 엽니 다. 왜 그런지 정확히 모르겠지만 참고로 [this] (http://zetcode.com/gui/pyqt4/firstprograms/) 사이트를 사용할 수 있습니다 (그것은 나를 위해 일했습니다 ...) 행운을 빌어 요 –

답변

0

: 클라이언트 창에서 선 app = QApplication(sys.argv)sys.exit(app.exec_()) 나는 다음과 같은 오류를 받고 있어요 lude

def onProgress(self, i): 
    self.progressBar.setValue(i) 
    if self.progressBar.value() == 100: 
     self.client_window = client_window.main() 
    else: 
     self.tryAgain.setEnabled(True) 
     self.tryAgain.clicked.connect(self.onStart) 
     self.label.setText("Cannot connect to Server! Please Try Again!") 
+0

감사합니다 !!! 수정되었습니다. –

관련 문제