关于从wxPython cod使用py2exe生成的可执行文件

2024-06-25 05:28:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经从我的真实世界的应用程序创建了一个示例应用程序。我使用过wxpython3.0、python2.7和windows7 64位操作系统。 这个应用程序很简单。这是一个带有文本字段的GUI,您可以在其中输入IP地址,然后单击一个按钮,将进行10次nslookup查询。gui.py包含GUI代码,lookup.py将包含nslookup过程代码。在所有这些之后,我使用py2exe来创建一个可执行文件。我也成功地创建了一个可执行文件。在

问题:当我执行新创建的可执行文件时,当我的应用程序正在运行并执行nslookup查询时,cmd控制台也会出现/闪烁!为什么会发生这种情况?如何避免?但是,如果我通过cmd通过输入gui.py并按enter来执行我的应用程序,那么在nslookup过程中不会出现cmd窗口。在

代码:所有代码和新创建的可执行文件可供下载here,只是为了避免任何标识问题。如有任何建议,我们将不胜感激。在

图形用户界面.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()

查找.py

^{pr2}$

设置.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日志enter image description here

编辑:如果您在执行python脚本时以及在执行创建的可执行文件的过程中观察到cmd控制台窗口的任何闪烁/出现,我将非常感谢您的任何人能够测试我的脚本并在这里发布。在

解决方案:我采纳了Mike的建议,最后决定用socket模块代替nslookup命令,现在我的问题解决了!在


Tags: pyimportselfipadd应用程序可执行文件dll
1条回答
网友
1楼 · 发布于 2024-06-25 05:28:30

更改的实例化wx.应用程序至以下地址:

app = wx.App(False)

这告诉wxPython不要将stdout/stderr重定向到窗口。我还认为setup.py文件也有错误。尝试将windows=行设置为以下值:

^{pr2}$

另请参见:

相关问题 更多 >