Pyqt5, threading and opening a new window

My electronic scoreboard program for competitions has a long process of recording data in excel, so that the interface does not freeze, I use multithreading. Under certain conditions, after recording in Excel, one application window should close and another should open. that is, the created thread must pass information to the main thread, which is now the case when another window needs to be opened. But I don’t know how to pass this information so that:

  • the interface did not freeze (because multithreading is not used)
  • a second window opened (the main window opens, but the other one does not, because the method is not called from the main thread)

It is very important that the interface does not freeze while the function is running

Below I will show you a simplified representation, where:

  • the MainWindow.foo() method creates a thread with the writeToExcel function simulating writing to an excel file and fulfilling the condition for opening another window (making sure that the condition is always satisfied)
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget, QVBoxLayout, QLabel
from PyQt5.QtCore import QTimer
import threading
import time


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Main Window")

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        layout = QVBoxLayout()
        self.central_widget.setLayout(layout)

        self.label = QLabel("00:00:00")
        layout.addWidget(self.label)

        self.button = QPushButton("Open Secondary Window")
        self.button.clicked.connect(self.open_secondary_window)
        layout.addWidget(self.button)

        self.label2 = QLabel("foo not runned")
        layout.addWidget(self.label2)

        self.button2 = QPushButton("Run foo()")
        self.button2.clicked.connect(self.foo)
        layout.addWidget(self.button2)

        self.timer = QTimer()
        self.timer.timeout.connect(self.update_stopwatch)
        self.time_elapsed = 0

        self.start_stopwatch()  # Start the stopwatch when the window is created

    def open_secondary_window(self):
        self.secondary_window = SecondaryWindow()
        self.close()
        self.secondary_window.show()

    def update_stopwatch(self):
        self.time_elapsed += 1
        hours = self.time_elapsed // 3600
        minutes = (self.time_elapsed // 60) % 60
        seconds = self.time_elapsed % 60
        self.label.setText(f"{hours:02d}:{minutes:02d}:{seconds:02d}")

    def start_stopwatch(self):
        self.timer.start(1000)

    def stop_stopwatch(self):
        self.timer.stop()

    def foo(self):
        if threading.activeCount() <= 1:
            thread1 = threading.Thread(target=self.writeToExcel, daemon=True)
            thread1.start()
        else:
            self.label2.setText('foo still running')

    def writeToExcel(self):
        self.label2.setText('foo is run')
        time.sleep(5)
        self.label2.setText('foo was done')
        time.sleep(1)
        if True:
            self.open_secondary_window()



class SecondaryWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Secondary Window")

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        layout = QVBoxLayout()
        self.central_widget.setLayout(layout)

        self.button = QPushButton("Open Main Window")
        self.button.clicked.connect(self.open_main_window)
        layout.addWidget(self.button)

    def open_main_window(self):
        self.main_window = MainWindow()
        self.close()
        self.main_window.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật