簡體   English   中英

MacOS PyQt5 - 右鍵單擊停靠欄中的圖標以顯示所有打開的 windows

[英]MacOS PyQt5 - Right-click icon in dock to show all open windows

標題基本概括了所有內容。

如何讓使用 PyQt5 構建的應用程序具有與 window 類似的行為,如下所示? 理想情況下,所有打開的 windows 都將顯示為前景中的 window 的指示器。

斐濟應用程序圖標右鍵單擊:

在此處輸入圖像描述

PyQt5 應用程序圖標右鍵單擊(打開多個 windows):

在此處輸入圖像描述

好的 - 這是我開發的粗略解決方案。 我仍然需要弄清楚如何在 window 項目被銷毀后從碼頭中刪除它們,我想添加指示器來顯示 windows 是可見還是隱藏。 不過這是一個好的開始!

感謝@musicamante 的setAsDockMenu()標志。

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QMainWindow,QWidget, QPushButton,
                             QMenu, QVBoxLayout, QAction)

import sys
import platform
    
def add_menu(mainWindow):
    if platform.system() == "Darwin":
        mainWindow.menuBar = MenuBar()
        mainWindow.centralWidget().layout().addWidget(mainWindow.menuBar)


class MenuBar(QMenu):
    """ Menu bar that displays the open windows on MacOS """
    menu_items = {}
    def __init__(self):
        super().__init__()
        self.setAsDockMenu()
        self.hide()
    
    def add_window(self, window, title):
        show_action = self.addAction(title)
        show.triggered.connect(window.showNormal)
        self.menu_items[title] = show_action
        return
        
    def remove_window(self, title):
        show_action = self.menu_items.pop[title]
        self.removeAction(show_action)
        return

class Window(QWidget):
    def __init__(self, title, windows, dockMenu):
        super().__init__()
        
        self.title = title
        self.windows = windows
        self.dockMenu = dockMenu
        self.setWindowTitle(title)
        self.show()
        
    def closeEvent(self, event):
        self.windows.pop(self.title)
        
        if platform.system() == "Darwin":
            self.dockMenu.remove_window(self.title)
            
        event.accept()
        self.destroy()


class MainWindow(QMainWindow):
    windows = {}
    def __init__(self):
        super().__init__()
        
        self.setCentralWidget(QWidget())
        c_widget = self.centralWidget()
        c_widget.setLayout(QVBoxLayout())
        
        # If using MacOS, create a menubar to view windows.
        add_menu(self)
        
        add_item = QPushButton("Add")
        add_item.clicked.connect(self.add_window)
        c_widget.layout().addWidget(add_item)
        
        print_window = QPushButton("Print Windows")
        print_window.clicked.connect(self.show_windows)
        c_widget.layout().addWidget(print_window)
        
        self.show()
        
    def add_window(self):
        title = str(len(self.windows.keys()))
        self.window = Window(title, self.windows, self.menuBar)
        self.windows[title] = self.window
        
        self.menuBar.add_window(self.window, title)
        
    def show_windows(self):
        print (self.windows.keys())
    
    def closeEvent(self, event):
        QApplication.closeAllWindows()
        event.accept()
        
        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    sys.exit(app.exec_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM