I have created a sample app from my real world app. I have used wxPython 3.0, python 2.7 and windows 7 64-bit OS.
The app is simple. It is a GUI with a text field where you can enter an IP address and then upon clicking a button, 10 nslookup queries will be made. The gui.py
contains the GUI code and the lookup.py
will contain the nslookup process code. After all this I used py2exe
to create an executable. I successfully created an executable too.
Problem: When I execute my newly created executable the cmd console also appears/blinks while my app is running and performing nslookup queries! Why is this happening and how to avoid this? However if I execute my app via cmd by typing gui.py
and hitting enter, no cmd windows appear during the nslookup process.
Code: All the code and the newly created executable is available for download here, just to avoid any identation problems. Any suggestions will be appreciated.
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 for building the executable.
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 log:
EDIT: I would really appreciate it if any one of could test my script and post here if you observed any blinking/appearing of cmd console window, while the execution of my python script and as well as during the execution of the executable created.
Solution: I took the suggestion from Mike and finally decided to use socket module instead of nslookup
command, and now my problem is solved!