【问题标题】:Entering fullscreen mode hides the icon from taskbar and the application runs from background进入全屏模式会隐藏任务栏中的图标,并且应用程序从后台运行
【发布时间】:2020-09-03 23:00:38
【问题描述】:

当我让一个窗口进入全屏模式时,应用程序图标从任务栏中隐藏(在执行 alt-tab 之后)。它也没有出现在alt+tab 窗口中。它只显示了我打开的其他窗口。

当我检查任务管理器时,python 进程在background processes 类别下。我无法切换回窗口。

当我进入全屏模式时,如何阻止应用程序在后台运行?

我的代码:

import sys
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QShortcut
from PyQt5.QtCore import Qt

# Subclass QMainWindow to customise your application's main window
class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle("Hello fullscreen world")

        label = QLabel("Hello fullscreen world")

        # The `Qt` namespace has a lot of attributes to customise
        # widgets. See: http://doc.qt.io/qt-5/qt.html
        label.setAlignment(Qt.AlignCenter)

        # Set the central widget of the Window. Widget will expand
        # to take up all the space in the window by default.
        self.setCentralWidget(label)

        
        self.shortcut_close_window = QShortcut(QKeySequence('F11'), self)
        self.shortcut_close_window.activated.connect(self.goFullscreen)
    def goFullscreen(self):
        if self.isFullScreen():
            self.setWindowFlags(self._flags)
            self.showNormal()
        else:
            self._flags = self.windowFlags()
            self.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowType_Mask)
            self.showFullScreen()


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec_()

注意:即使使用 ctrl+c 终止进程或关闭命令提示符也不起作用,只有任务管理器可以。

【问题讨论】:

    标签: python-3.x pyqt5


    【解决方案1】:

    我不知道你为什么在goFullscreen() 函数中使用self.setWindowFlags。没必要。

    def goFullscreen(self):
            if self.isFullScreen():
                #self.setWindowFlags(self._flags)
                self.showNormal()
            else:
                #self._flags = self.windowFlags()
                #self.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowType_Mask)
                self.showFullScreen()
    

    顺便说一句,原因是您使用了 Qt.WindowType_Mask 标志。

    WindowType_Mask:用于提取窗口类型部分的掩码 窗口标志。

    ~Window Flags~


    我不知道 Ubuntu 中有这么奇怪的错误。所以你所要做的就是编写你自己的 FullScreen 方法:

    (我不使用ubuntu。所以不知道你会不会再遇到bug。)

    import sys
    from PyQt5.QtGui import QKeySequence
    from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QShortcut
    from PyQt5.QtCore import Qt
    
    
    # Subclass QMainWindow to customise your application's main window
    class MainWindow(QMainWindow):
        isfullscreen = False
        def __init__(self, *args, **kwargs):
            super(MainWindow, self).__init__(*args, **kwargs)
    
            self.setWindowTitle("Hello fullscreen world")
    
            self._flags = self.windowFlags()
            self._geometry = ((self.screen().size().width() / 2) - (self.width() / 2),
                              (self.screen().size().height() / 2) - (self.height() / 2), 600, 400)
    
            self.setGeometry(*self._geometry)
    
            label = QLabel("Hello fullscreen world")
    
    
            # The `Qt` namespace has a lot of attributes to customise
            # widgets. See: http://doc.qt.io/qt-5/qt.html
            label.setAlignment(Qt.AlignCenter)
    
            # Set the central widget of the Window. Widget will expand
            # to take up all the space in the window by default.
            self.setCentralWidget(label)
    
            self.shortcut_close_window = QShortcut(QKeySequence('F11'), self)
            self.shortcut_close_window.activated.connect(self.goShowFullScreen)
            self._geometry = self.geometry()
    
        def goShowFullScreen(self):
            if not self.isfullscreen:
                self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
                self.setGeometry(self.screen().geometry())
                self.show()
                self.isfullscreen = True
            else:
                self.setWindowFlags(self.windowFlags() & ~Qt.WindowStaysOnTopHint)
                self.setWindowFlags(self.windowFlags() & ~Qt.FramelessWindowHint)
                self.setGeometry(self._geometry)
                self.show()
                self.isfullscreen = False
    
    
    app = QApplication(sys.argv)
    
    window = MainWindow()
    window.show()
    
    app.exec_()
    

    【讨论】:

    • 我使用 setWindowFlags 是因为 StackOverflow 上有人提到全屏在 ubuntu 上不起作用
    • 和其他设置窗口标志是因为没有它关闭按钮隐藏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    相关资源
    最近更新 更多