使用PyQt4 QWidget显示/隐藏? [英] Show/Hide With PyQt4 QWidget?

查看:834
本文介绍了使用PyQt4 QWidget显示/隐藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,伙计们。我被困在这里 - 仍在学习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()

推荐答案

修改后的代码与我合作。我正在使用Qt5。你修改为你的



def systemIcon(自我,原因):

w = QDialog()

如果reason == QSystemTrayIcon.Trigger:

print(Clicked。)

w.exec _()
modified code this worked with me. i am using Qt5. you my modify as yours

def systemIcon(self, reason):
w = QDialog()
if reason == QSystemTrayIcon.Trigger:
print ("Clicked.")
w.exec_()


这篇关于使用PyQt4 QWidget显示/隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆