Skip to main content
 首页 » 编程设计

python之PyQt 无模式对话框不会显示

2025年01月19日12落叶无声

我想在等待另一个进程接收消息时显示一个弹出对话框。我不希望这会阻止代码,因此使用 .exec_() 似乎不太理想。当我改为调用 show .show() 时,对话框根本不会弹出。有趣的是,它会显示我是否在它被调用之前使用调试器,但如果我在它之后立即 sleep /等待一段时间,它仍然不会显示任何内容。这是我的 PyQt 代码:

from PyQt5.QtWidgets import QApplication, QDialog, QLabel 
 
 
class Main(QApplication): 
   def __init__(self, *args): 
       super().__init__([]) 
       self.popup = Popup() 
 
   def show_popup(self, msg): 
       self.popup.set_msg(msg) 
       self.popup.show() 
 
 
class Popup(QDialog): 
   def __init__(self): 
       super().__init__() 
       self.label = QLabel(self) 
 
   def set_msg(self, msg): 
       self.label.setText(msg) 

在我调用它的过程中,我基本上实例化了 Main 对象并在发送消息后调用 show_popup,等待更改文本并在收到消息后关闭回复。使用 exec_() 会显示弹出窗口,但它会阻止代码在其他进程中运行。非常感谢任何帮助,谢谢。

请您参考如下方法:

class Popup(QMainWindow): 
def __init__(self): 
   super().__init__() 
   self.setWindowFlags(self.windowFlags() | Qt.Tool |Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint) 
 
   self.label = QLabel(self) 
 
def set_msg(self, msg): 
   self.label.setText(msg) 

我在使用 QMessageBox 和 QWidget 时遇到了问题,但是继承自 QMainWindows 并且它可以按我的需要工作。