1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import subprocess
import threading
import sys
class WorkerTread (threading.Thread):
def __init__ (self, window):
self.window = window
threading.Thread.__init__(self)
self.start()
def run (self):
self.reply = subprocess.Popen(["ping", "google.com"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ligne = self.reply.stdout.readline()
while ligne:
wx.CallAfter(self.window.textCtrl_append, ligne)
sys.stdout.flush()
ligne = self.reply.stdout.readline()
def stop (self):
self.reply.kill()
class Gui (wx.Frame):
def __init__ (self, titre):
self.titre = titre
wx.Frame.__init__(self, None, wx.ID_ANY, title=self.titre)
sizer = wx.GridBagSizer()
# ----------------------------------------------------------------------------------------------------------- Champ de texte
self.textCtrl = wx.TextCtrl(self, wx.ID_ANY, size=(800, 600), style=wx.TE_MULTILINE|wx.TE_READONLY)
self.textCtrl.SetBackgroundColour('#000000')
self.textCtrl.SetForegroundColour('#ffffff')
sizer.Add(self.textCtrl, (0, 0), flag=wx.EXPAND|wx.ALL, border=10)
# ----------------------------------------------------------------------------------------------------------- Boutons Quitter...
buttonExit = wx.Button(self, wx.ID_EXIT)
sizer.Add(buttonExit, (1, 0), flag=wx.ALIGN_CENTRE|wx.BOTTOM, border=10)
# ----------------------------------------------------------------------------------------------------------- Divers
# Initialisation
self.thread = WorkerTread(self)
# Sizer + Fit
self.SetSizerAndFit(sizer)
self.Fit()
# Non redimentionnable
self.SetMinSize(self.GetSize())
sizer.AddGrowableCol(0)
sizer.AddGrowableRow(0)
# Evénements
self.Bind(wx.EVT_BUTTON, self.exit, buttonExit)
self.Bind(wx.EVT_CLOSE, self.post_exit)
# ----------------------------------------------------------------------------------------------------------- Fonctions Générales
def textCtrl_append (self, texte):
self.textCtrl.AppendText(texte)
def post_exit (self, event):
self.thread.stop()
self.Destroy()
def exit (self, event):
self.Close()
class Ihm (wx.App):
def OnInit (self):
root = Gui(u'stdout')
root.Show(True)
self.SetTopWindow(root)
return True
if __name__ == '__main__':
app = Ihm()
app.MainLoop() |
Partager