如何从 TaskBar 菜单应用程序 Python/Pyside2 for MacOS 显示 Hello World 对



首先创建一个虚拟的"hello world"对话框/窗口,如何在MacOS上从任务栏/菜单显示它。谢谢。

如果我理解你的问题,您想从QMainWindow的菜单栏中打开QDialog,对吗?

为此,这是一种简单的方法:

import sys                                                                                      
from PySide2.QtCore import Slot                                                                    
from PySide2.QtWidgets import (QApplication, QMainWindow, QAction,                                 
QDialog, QLabel, QHBoxLayout)                                                                      
class Dialog(QDialog):                                                                          
    def __init__(self):                                                                         
        QDialog.__init__(self)                                                                  
        layout = QHBoxLayout()                                                                  
        layout.addWidget(QLabel("Hello World"))                                                 
        self.setLayout(layout)                                                                  
class MainWindow(QMainWindow):                                                                  
    def __init__(self):                                                                         
        QMainWindow.__init__(self)                                                              
        self.menu = self.menuBar()                                                              
        self.test_menu = self.menu.addMenu("Test")                                              
        self.hello_action = QAction("hello", self)                                              
        self.hello_action.triggered.connect(self.hello_dialog)                                  
        self.test_menu.addAction(self.hello_action)                                             
    @Slot()                                                                                     
    def hello_dialog(self, checked):                                                            
        dialog = Dialog()                                                                       
        dialog.exec_()                                                                          

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

最新更新