使用python wxpython按钮单击事件启动Windows批处理文件
我想使用python wxpython按钮单击事件启动windows批处理文件使用python wxpython按钮单击事件启动Windows批处理文件,python,wxpython,Python,Wxpython,我想使用python wxpython按钮单击事件启动windows批处理文件 import wx import subprocess class MyFrame(wx.Frame): """ Dervice new class of Frame""" def __init__ (self, parent, title) wx.Frame.__init__(self,parent,title=title,size=(410,355)) pane
import wx
import subprocess
class MyFrame(wx.Frame):
""" Dervice new class of Frame"""
def __init__ (self, parent, title)
wx.Frame.__init__(self,parent,title=title,size=(410,355))
panel = wx.Panel(self)
startbutton = wx.Button(panel, label="Start", pos=(200,70), size=(80,25))
self.Bind(wx.EVT_BUTTON,self.StartClicked, startbutton)
def StartClicked(self, event):
print "Session started"
self.filepath="C:\\cygwin64\\Sipp_3.2\\Auto.bat"
subprocess.call(self.filepath)
print "Session ended"
if __name__ == '__main__':
app = wx.App()
frame = MyFrame(None, "CallGenerator")
frame.Show()
app.MainLoop()
下面是每次单击“开始”按钮时的输出
代码未打开批处理文件。但是,当我通过命令提示符执行批处理文件时,批处理文件是通过命令提示符执行的
我正在使用pyscripter,如何将批处理文件作为新窗口打开
类似地,如果批处理文件包含连续ping(ping ipaddress-t),GUI将崩溃
现在,我的两个问题的解决方案是什么。程序可能正在调用批处理文件,但由于您没有重定向进程的stdout/stderr,因此您看不到它在做什么。您可能希望使用
子流程
模块的通信
方法从批处理文件中获取输出,并使用该信息更新GUI
您提到批处理文件运行一个长时间运行的进程。这将阻止GUI的主循环,这就是UI变得无响应的原因。如果改用popen
,您可能可以绕过这个问题,但是正确的解决方案是将子进程调用放在线程内,然后通过wx的线程安全方法之一(如wx.CallAfter
或wx.PostEvent
)将结果从线程返回到UI
有关更多信息,请参阅以下文章:
>Session started
>Session ended
>Session started
>Session ended