问题描述
你好,伙计们。我被困在这里 - 仍在学习OOP和Python。
我如何构建这个程序,以便我能够在启动时生成一个QWidget对象?此外,我如何制作它,以便每次点击系统托盘图标(即mediaIcon.png)时,相同的小部件不会重新实例化自己,而是每隔一次点击显示/隐藏自己?
截至目前,当我点击图标时,程序回声点击。然后关闭我不知道为什么会这样。
非常感谢,
Akratix
Hello again, guys. I'm stuck here -- still learning OOP and Python.
How do I structure this program so that I am able to spawn a QWidget object on start-up? Moreover, how do I make it so that each time I click on the System Tray Icon (namely mediaIcon.png), that same widget will not re-instantiate itself but, rather, show/hide itself with every other click?
As of right now, when I click on the icon, the program echos "Clicked." then closes. I don't know why that is.
Thanks a bunch,
Akratix
#!/usr/bin/python
import sys, os
from PyQt4 import QtGui, QtCore
class MediaFeed(QtGui.QSystemTrayIcon):
def __init__(self, icon, parent=None):
QtGui.QSystemTrayIcon.__init__(self, icon, parent)
menu = QtGui.QMenu()
aboutAction = menu.addAction("About")
aboutAction.triggered.connect(self.aboutMenu)
exitAction = menu.addAction("Exit")
exitAction.triggered.connect(QtGui.qApp.quit)
self.setContextMenu(menu)
self.activated.connect(self.systemIcon)
def aboutMenu(self):
print "This should be some information."
def systemIcon(self, reason):
w = QtGui.QWidget()
if reason == QtGui.QSystemTrayIcon.Trigger:
print "Clicked."
w.show()
def main():
app = QtGui.QApplication(sys.argv)
mediaFeed = MediaFeed(QtGui.QIcon("mediaIcon.png"), None)
mediaFeed.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
推荐答案
这篇关于使用PyQt4 QWidget显示/隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!