我从我的真实应用程序中创建了一个示例应用程序。我使用过wxPython 3.0,python 2.7和windows 7 64位操作系统。
该应用程序很简单。它是一个带有文本字段的GUI,您可以在其中输入IP地址,然后在单击按钮时,将进行10次nslookup查询。 gui.py
包含GUI代码,lookup.py
将包含nslookup进程代码。毕竟,我使用py2exe
创建了一个可执行文件。我也成功创建了一个可执行文件
问题:当我执行新创建的可执行文件时,cmd控制台在我的应用程序运行并执行nslookup查询时也会出现/闪烁!为什么会发生这种情况以及如何避免这种情况?但是,如果我通过键入gui.py
并按Enter键来通过cmd执行我的应用程序,则在nslookup过程中不会出现cmd窗口。
代码:所有代码和新创建的可执行文件都可供下载here,以避免任何身份验证问题。任何建议将不胜感激。
gui.py :
# -*- coding: utf-8 -*-
import wx
from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub as Publisher
from threading import Thread
import threading
import socket
import os
import sys
from lookup import checker
class GUI(wx.Frame):
def __init__(self):
filePath = ''
wx.Frame.__init__(self, None, wx.ID_ANY, "test v1.0", style = wx.DEFAULT_FRAME_STYLE,size=(550,250))
self.Center()
self.CreateStatusBar()
self.mainPanel = mainPanel = wx.Panel(self, -1)
self.mainPanel.SetBackgroundColour('#EEEEEE')
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.myFont = myFont = wx.Font(11, wx.MODERN, wx.NORMAL, wx.NORMAL, False, "Arial")
boldFont = wx.Font(11, wx.MODERN, wx.NORMAL, wx.BOLD, False, "Arial")
ipPanel = wx.Panel(mainPanel, -1, style=wx.BORDER)
iptextSizer = wx.BoxSizer(wx.VERTICAL)
iptitleText = wx.StaticText(ipPanel, -1, 'Please enter the IP and click the Check IP button to proceed!')
iptitleText.SetFont(myFont)
iptitleText.SetForegroundColour(wx.BLUE)
iptextSizer.Add(iptitleText, 0, wx.TOP|wx.EXPAND, 10)
ipText = wx.StaticText(ipPanel, -1, 'IP adddress: ')
ipText.SetFont(myFont)
ipTextCtrl = self.ipTextCtrl = wx.TextCtrl(ipPanel, -1, style = wx.TE_RICH, size=(303,24), name='ipTextCtrl')
ipTextCtrl.SetFont(boldFont)
ipTextCtrl.SetBackgroundColour('#FBFE99')
ipTextCtrl.SetMaxLength(15)
ipTextCtrl.Bind(wx.EVT_LEFT_DOWN, self.onClick)
ipTextCtrl.SetToolTip(wx.ToolTip("Please enter the IP address here."))
self.ipButton = ipButton = wx.Button(ipPanel, -1, 'Check IP', size=(98,35), name='ipButton')
ipButton.Bind(wx.EVT_LEFT_DOWN, self.onClick)
ipButton.SetFont(myFont)
ipButton.SetForegroundColour('#D6FDE2')
ipButton.SetBackgroundColour('#05C354')
ipButton.SetToolTip(wx.ToolTip("Click to start the lookup process."))
ipSizer = wx.BoxSizer(wx.HORIZONTAL)
ipSizer.AddSpacer(10)
ipSizer.Add(ipText, 0, wx.TOP, 11)
ipSizer.Add(ipTextCtrl, 0, wx.TOP, 9)
ipSizer.AddSpacer(40)
ipSizer.Add(ipButton, 0)
ipSizerMain = wx.BoxSizer(wx.VERTICAL)
ipSizerMain.Add(iptextSizer, 0, wx.ALL|wx.EXPAND, 5)
ipSizerMain.Add(ipSizer, 0, wx.ALL|wx.EXPAND, 5)
ipPanel.SetSizer(ipSizerMain)
mainsizer = wx.BoxSizer(wx.VERTICAL)
mainSizerA = wx.BoxSizer(wx.HORIZONTAL)
mainSizerA.Add(ipPanel, 1, wx.ALL, 2)
mainSizer.AddSpacer(10)
mainSizer.Add(mainSizerA, 0, wx.EXPAND)
mainPanel.SetSizer(mainSizer)
mainPanel.Layout()
def logger(self, result):
if result:
pass
else:
self.ipButton.SetLabel('Check IP')
self.ipButton.SetForegroundColour('#D6FDE2')
self.ipButton.SetBackgroundColour('#05C354')
self.iplistButton.SetLabel('Check IP List')
self.browseButton.SetLabel('Browse IP List')
self.browseButton.SetBackgroundColour('#05C354')
self.browseButton.SetForegroundColour('#D6FDE2')
self.ipButton.Enable()
self.browseButton.Enable()
self.stopButton.Disable()
def onClick(self, e):
widget = e.GetEventObject()
widgetName = widget.GetName()
if widgetName == 'ipButton':
self.IP = self.ipTextCtrl.GetLineText(0)
threadgetResults = getResults(self.IP, 'IP', None)
class getResults(Thread):
def __init__(self, IP, code, filePath):
Thread.__init__(self)
self.IP = IP
self.code = code
self.filePath = filePath
self.daemon = True
self.start()
def run(self):
if self.code == 'IP':
blCheckerObj = checker()
output = blCheckerObj.lookup(self.IP)
else:
pass
#--------------------------- MAIN -------------------------------
if __name__=='__main__':
app = wx.App()
frame = GUI().Show()
app.MainLoop()
lookup.py
import subprocess
import time
import os
import sys
import random
class checker:
def lookup(self, IP):
self.IP = IP
cmd = 'nslookup ' +self.IP
print ' QUERY: '+cmd+'\n'
for i in range(0,10):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.out, self.err = p.communicate()
print self.out
time.sleep(3)
用于构建可执行文件的setup.py 。
from distutils.core import setup
import py2exe
dll_excludes = ['OLEAUT32.dll','USER32.dll','COMCTL32.dll','SHELL32.dll',
'ole32.dll','MSVCP90.dll','WINMM.dll','WSOCK32.dll',
'COMDLG32.dll','ADVAPI32.dll','NETAPI32.dll','WS2_32.dll',
'WINSPOOL.DRV','GDI32.dll','RPCRT4.dll','VERSION.dll',
'KERNEL32.dll','ntdll.dll']
setup (
name='Test',
description="Script to test py2exe for packaging",
version="0.1",
windows=['gui.py'],#windows=[{'script': 'gui.py'}],
platforms=["windows"],
options={ 'py2exe': {
'packages': 'encodings, wx.lib.pubsub',
"excludes": dll_excludes,
}
},
)
py2exe日志:
编辑:我真的很感激如果任何一个可以测试我的脚本并在此处发布,如果您观察到cmd控制台窗口的任何闪烁/出现,同时执行我的python脚本以及在执行可执行文件时执行。
答案 0 :(得分:1)
将wx.App的实例化更改为以下内容:
app = wx.App(False)
这告诉wxPython不要将stdout / stderr重定向到窗口。另外我相信setup.py
文件也有错误。尝试将windows=
行设置为以下内容:
windows=['gui.py']
另见: