How do i hide the console window for my app? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-11 05:12:10

问题


Possible Duplicate:
How can I hide the console window in a PyQt app running on Windows?

I've made a simple app that opens a QWebView but in addition to the app window Windows console window is opened to.

What could be the reason for this?

import sys
import socket
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebView

class AppWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)        

        self.setGeometry(300, 300, 200, 25)  
        self.setWindowTitle('TrayIP')
        self.create_sys_tray()      

        self.hostname = socket.gethostname()
        self.ip = socket.gethostbyname(self.hostname)

        self.label = QLabel("IP: " + self.ip, self)
        self.label.setFont(QtGui.QFont('Helvetica', 14))

        self.timer = QTimer()
        self.connect( self.timer, QtCore.SIGNAL('timeout()'), self.timerupdate)
        self.timer.start(10000)

    def timerupdate(self):
        self.web = QWebView()
        self.web.load( QUrl('http://xxx') )

    def create_sys_tray(self):
        self.sysTray = QtGui.QSystemTrayIcon(self)
        self.sysTray.setIcon( QtGui.QIcon('ico.ico') )
        self.sysTray.setVisible(True)
        self.connect(self.sysTray, QtCore.SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), self.on_sys_tray_activated)

        self.sysTrayMenu = QtGui.QMenu(self)
        act = self.sysTrayMenu.addAction("FOO")

    def on_sys_tray_activated(self, reason):        
        if self.isVisible ():
            self.setVisible(False)
        else:
            self.setVisible(True)

app = QtGui.QApplication(sys.argv)
window = AppWindow()
window.show()
sys.exit(app.exec_())

回答1:


Use pythonw.exe instead of python.exe




回答2:


name your file .pyw instead of .py




回答3:


On default Windows installations, .pyw files are opened without the console window (because they're loaded with pythonw), and .py files load in a console. So yeah, renaming to .pyw is probably easiest solution.

Also - if you're compiling with py2exe, you'll have to change "console=" to "window="



来源:https://stackoverflow.com/questions/4872523/how-do-i-hide-the-console-window-for-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!