简体   繁体   English

从任务栏中隐藏 PyQt 应用程序

[英]Hide PyQt app from taskbar

I'm a beginner in PyQt.我是 PyQt 的初学者。 I was trying to create a simple app to try some of the toolkit's many features.我试图创建一个简单的应用程序来尝试工具包的许多功能。 My question is, how can I hide the app icon from the taskbar?我的问题是,如何从任务栏中隐藏应用程序图标? I don't want the user to be able to see the icon in taskbar and to minimize it using this icon.我不希望用户能够看到任务栏中的图标并使用此图标将其最小化。 Is there any window flags that I can use to achieve this?是否有任何窗口标志可用于实现此目的?

这应该可以解决问题:

myApp.setWindowFlags(QtCore.Qt.Tool)

This drove me nuts for days.这让我疯狂了好几天。 Complete app code to implement below.完整的应用程序代码在下面实现。

Key bits:关键位:

  • override closeEvent(), enabling it to do either of just hiding window or true exit覆盖 closeEvent(),使其能够只隐藏窗口或真正退出
  • create some facility for user to choose either hide or exit behavior为用户创建一些工具来选择隐藏或退出行为
  • don't show() main window on instantiation, just exec_() the App不要在实例化时显示()主窗口,只是 exec_() 应用程序
import sys from PyQt4.QtGui import QAction, QApplication, QFrame, QIcon, \\ QMainWindow, QMenu, QSystemTrayIcon from PyQt4.QtCore import SIGNAL class MyApp(QMainWindow): def __init__(self, parent, title): super(QMainWindow, self).__init__(parent) self.exitOnClose = False exit = QAction(QIcon(), "Exit", self) self.connect(exit, SIGNAL("triggered()"), self.exitEvent) self.trayIcon = QSystemTrayIcon(QIcon(), self) menu = QMenu(self) menu.addAction(exit) self.trayIcon.setContextMenu(menu) self.connect(self.trayIcon, \\ SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), \\ self.trayIconActivated) self.trayIcon.show() self.trayIcon.showMessage("MyApp is running!", "Click to open window\\nRight click for menu" ) def trayIconActivated(self, reason): if reason == QSystemTrayIcon.Context: self.trayIcon.contextMenu().show() elif reason == QSystemTrayIcon.Trigger: self.show() self.raise_() def closeEvent(self, event): if self.exitOnClose: self.trayIcon.hide() del self.trayIcon event.accept() else: self.hide() event.setAccepted(True) event.ignore() def exitEvent(self): self.exitOnClose = True self.close() if __name__ == "__main__": app = QApplication(sys.argv) myapp = MyApp(None, "My System Tray App") app.exec_()

Adapted from this thread :改编自此线程

import sys
from PyQt4.QtGui import *

if __name__ == '__main__':
    app = QApplication(sys.argv)

    widget = QWidget()

    mainWindow = QMainWindow(widget)
    mainWindow.show()

    sys.exit(app.exec_())

If you are on Ubuntu with Unity and want to hide an application's icon from the launcher on the left-hand-side, you will probably need Qt.SplashScreen .如果你在使用 Unity 的 Ubuntu并且想要从左侧的启动器中隐藏应用程序的图标,你可能需要Qt.SplashScreen This worked for me but I don't remember if I also needed Qt.Tool , which is enough on Windows.这对我Qt.Tool ,但我不记得我是否还需要Qt.Tool ,这在 Windows 上就足够了。 For the SplashScreen attempt you may have to reimplement the resize functionality as it disables this feature of a QStatusBar (that has a SizeGrip) for example.对于 SplashScreen 尝试,您可能必须重新实现调整大小功能,因为它禁用了 QStatusBar(具有 SizeGrip)的此功能。

Here is a little example to try out window flags. 是一个尝试窗口标志的小例子。

I wouldn't recommend trying to hide an application's taskbar presence, especially if the application is visible.我不建议尝试隐藏应用程序的任务栏存在,尤其是在应用程序可见的情况下。 If you are only trying to prevent minimizing from the taskbar then you can achieve this by creating your top level widget with the following window flags like this:如果您只是想防止从任务栏最小化,那么您可以通过使用以下窗口标志创建顶级小部件来实现此目的:

QWidget *mainWindow = new QWidget(0, Qt::CustomizeWindowHint 
    | Qt::WindowTitleHint | Qt::WindowSystemMenuHint 
    | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint);

If you don't want a maximize flag, you can leave that one out of the list too.如果您不想要最大化标志,您也可以将该标志排除在列表之外。

The various window flags that Qt can use are documented here (Qt::WindowFlags) . Qt 可以使用的各种窗口标志记录在此处 (Qt::WindowFlags)

像这样初始化你的主窗口self.setWindowFlags(Qt.ToolTip)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM