Good day! Began to learn Python + PyQt to create a GUI. I want to write a picture viewer. Somewhere I found the idea that it is better to compress several modules: in one to describe the logic of the application, in the other to describe the interface (buttons, buttons, sliders). I started to write, I face the problem of the interface hanging (the progress bar falls asleep) while the internal processes of the program are running. Tell me the basics, what is the basis of GUI creation, or some tutorials.

Closed due to the fact that off-topic participants jfs , Denis Bubnov , ߊߚߤߘ , Akina , Grundy 7 Feb '17 at 14:46 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - jfs, Denis Bubnov, ߊߚߤߘ
If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    The interface most likely hangs because you are not using multithreading. Try to execute the module with the GUI and the main module asynchronously, so that the program simultaneously runs internal processes and draws the interface independently of these processes.

    Example of creating a second thread:

    import threading import time # бесконечный цикл, который никогда не прекращается def some_func(): while True: time.sleep(1) print("Test 1") # Запуск функции some_func вторым потоком task = threading.Thread(target=some_func) task.start() # этот цикл будет выполняться, хотя функция some_func тоже выполняется в этот момент while True: time.sleep(1) print("Test 2") 

    Thus, the progress bar in your program will work, although the program will be occupied by another task.

    Multithreading article on Habrahabr

    • Thank! Began to use QThread - MacBaine
     from PyQt5.QtWidgets import QApplication, QMainWindow, QProgressBar from PyQt5.QtCore import pyqtSignal, QTimer from threading import Thread from time import sleep def work(data): '''работа - каждые 0.5 сек уменьшать data''' while data: sleep(.5) data.pop() # обновить прогресс можно тут, тк dataLen это pyqtSignal, и можно обновлять gui из любого потока # минус в том что будет вызвано len(data)=100 обновлений прогресбара Wnd.dataLen.emit(len(data)) class Window(QMainWindow): '''прогресс длины списка data_''' dataLen = pyqtSignal(int) def __init__(self, data): QMainWindow.__init__(self) self.bar = QProgressBar(self) self.setCentralWidget(self.bar) self.data = data self.bar.setValue(len(data)) # установить значение dataLen в bar self.dataLen.connect(self.bar.setValue) # если не использовать dataLen, то можно обновлять прогресбар, каждые x секунд по таймеру # self.timer = QTimer() # self.timer.timeout.connect(self.update_bar) # по истечению таймаута запустить self.update_bar # self.update_bar() # # def update_bar(self): # '''обновлять прогресбар каждые 3 сек''' # data_len = len(self.data) # self.bar.setValue(data_len) # if data_len: # self.timer.start(3000) # установить таймер if __name__ == '__main__': qa = QApplication([]) data_ = list(range(100)) Wnd = Window(data_) Thread(target=work, args=[data_]).start() # работать в потоке Wnd.show() qa.exec_()