Below basic pyqt5 program which displays a label and a button in a window works fine without any issue if I execute it using below command, given that I do not change my current window that is if I stay in terminal.
> python qtexample.py
If after issuing the above command (there is 5 secs delay before the window pops up) I change to someother window (other than terminal) then the PyQt5 Window does not pop up but stays on toolbar.
As shown in the attached screenshots.
qtexample.py
import sys
from time import sleep
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.clicksCount = 0
self.setupUi()
def setupUi(self):
self.setWindowTitle("Freezing GUI")
self.resize(300, 150)
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
# Create and connect widgets
self.clicksLabel = QLabel("Counting: 0 clicks", self)
self.clicksLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.countBtn = QPushButton("Click me!", self)
self.countBtn.clicked.connect(self.countClicks)
# Set the layout
layout = QVBoxLayout()
layout.addWidget(self.clicksLabel)
layout.addWidget(self.countBtn)
layout.addStretch()
self.centralWidget.setLayout(layout)
def countClicks(self):
self.clicksCount += 1
self.clicksLabel.setText(f"Counting: {self.clicksCount} clicks")
import time
time.sleep(5)
app = QApplication(sys.argv)
win = Window()
win.show()
win.raise_()
#win.activateWindow()
sys.exit(app.exec())
Below screenshot shows the window in toolbar without popup
Observed the same behavior in java swing application as well. Please let me know if this is something which can be modified. Desirable functionality is that no matter where the PyQT5 app is running (foreground/background) the window/dialog should pop up in whatever window I am in.
question from:
https://stackoverflow.com/questions/65921865/pyqt5-window-dialog-does-not-popup-when-switched-to-new-window 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…