Nic*_*k T 3 python user-interface wxpython tkinter pyqt
我有一个命令行Python脚本,可以很好地将一种文件转换为另一种给定的参数,现在可以将其部署到一些可能不知道命令行是什么的同事身上.
我可以工作几个小时试图确定哪个Python GUI工具包是"最好的",然后学习如何做我需要的东西,但似乎这之前就已经完成了.
GUIify我的程序有相对的烹饪书方法吗?指向某种课程/教程或现有的,有文档记录的简明程序的方向非常好.
只是一个快速而简单的例子,可以教你足够的wxPython开始:
import wx
from subprocess import Popen, PIPE
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.button = wx.Button(self.panel, label="Run!")
self.command = wx.TextCtrl(self.panel)
self.result = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.command, 0, wx.EXPAND)
self.sizer.Add(self.button, 0, wx.EXPAND)
self.sizer.Add(self.result, 1, wx.EXPAND)
self.command.SetValue("dir")
self.button.Bind(wx.EVT_BUTTON, self.CallCommand)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
def CallCommand(self, e):
p = Popen(self.command.GetValue(), shell=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
r = p.communicate()
self.result.SetValue(r[0])
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Run Code Online (Sandbox Code Playgroud)
要完成该示例,以下代码在另一个线程中运行命令行并逐行显示结果:
import wx
from wx.lib.delayedresult import startWorker
from subprocess import Popen, PIPE
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.button = wx.Button(self.panel, label="Run!")
self.command = wx.TextCtrl(self.panel)
self.result = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.command, 0, wx.EXPAND)
self.sizer.Add(self.button, 0, wx.EXPAND)
self.sizer.Add(self.result, 1, wx.EXPAND)
self.command.SetValue("dir")
self.button.Bind(wx.EVT_BUTTON, self.CallCommand)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
def CallCommand(self, e):
startWorker(self.WorkCommandDone, self.WorkCommand)
def WorkCommand(self):
self.button.Disable()
p = Popen(self.command.GetValue(), shell=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
while True:
line = p.stdout.readline()
if line != '':
wx.CallAfter(self.result.AppendText, line)
else:
break
def WorkCommandDone(self, result):
self.button.Enable()
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Run Code Online (Sandbox Code Playgroud)