0

I trying create GUI Api. First i build python script with only print information in console.

So I wanted to rebuild applications into applications with an interface. I decided to use PyQt5

Like this: enter image description here

To(first look): enter image description here

I ran into a problem with the loop While. Aplication just freeze when while is runing

I prepared a short script simulating the problem. The main program looks different

import sys
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets
from termcolor import colored
import time

class App(QMainWindow):
   def __init__(self):
       super().__init__()
       self.title = 'API NORD'
       self.left = 0
       self.top = 0
       self.width = 300
       self.height = 200
       self.setWindowTitle(self.title)
       self.resize(800, 600)
       self.center()
       self.table_widget = MyTableWidget(self)
       self.setCentralWidget(self.table_widget)

       self.show()
    def center(self):
       # geometry of the main window
       qr = self.frameGeometry()

       # center point of screen
       cp = QDesktopWidget().availableGeometry().center()

       # move rectangle's center point to screen's center point
       qr.moveCenter(cp)

    # top left of rectangle becomes top left of window centering it
       self.move(qr.topLeft())


class MyTableWidget(QWidget):

   def __init__(self, parent):
       super(QWidget, self).__init__(parent)

       self.layout = QVBoxLayout(self)
       self.pushButton1 = QPushButton("Run")
       self.layout.addWidget(self.pushButton1)
       self.pushButton1.clicked.connect(self.button2_clicked)
       self.textedit = QtWidgets.QTextEdit(readOnly=True)
       self.layout.addWidget(self.textedit)
       self.textedit.setText("STATUS")



   def onClicked(self):
       radioButton = self.sender()
       if radioButton.isChecked():
           x=0
        # print("Shop is %s" % (radioButton.shop))
           self.Sklep=radioButton.shop
           self.l1.setText(self.Sklep)
       return


   def checkBulkStatus(self):
       Status = "Start"
       x=0
       self.textedit.setText("Start")
       while x < 5:

           print("Aktualny Status:", colored(Status,"yellow"))
           Status="Running"
           self.textedit.append(Status)
           if Status=="FAILED":
               print("Error")
               break
           time.sleep(2.5)
           x+=1

       print("Aktualny Status: ", colored("COMPLETED", "green"))
       self.textedit.setText("COMPLETED")


   def button2_clicked(self):
       self.checkBulkStatus()

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

In main program I ussing while to check status of BULK request in GraphQL:

def checkBulkStatus(self):
    self.url = self.auth(self.Sklep)["url_auth"]
    print(self.url)
    Status = "Start"
    self.textedit.setText("Start")
    while Status != "COMPLETED":

    print("Aktualny Status:", colored(Status,"yellow"))
    checking = self.Core.callShopifyGraphQL(self.Core.CheckQuery,self.url)
    result = checking.json()
    Status=result["data"]["currentBulkOperation"]["status"]
    self.textedit.append(Status)
    if Status=="FAILED":
        print(result["data"]["currentBulkOperation"])
        break
    time.sleep(2.5)

print("Aktualny Status: ", colored("COMPLETED", "green"))
URL_bulk=result["data"]["currentBulkOperation"]["url"]
Small Atom
  • 164
  • 13
  • Python is single-threaded. When you execute while loop, the thread is blocked, so the UI seems frozen. – Epsi95 Feb 24 '21 at 08:50

1 Answers1

-1

The problem is that the gui runs in the same thread as the script, so when you run the script it freezes the interface. To prevent this from happening, you need to run the script in a thread, as this way you can share variables with the main thread.

I hope it helps you, greetings.