class GuiOutputBox(Toplevel): def __init__(self, parent=None): Toplevel.__init__(self, parent) # single window model self.box = GuiOutput(self) # embed an output frame def runShellCmd(self, cmd, mode=THREAD): redirectGuiShellCmd(cmd, mode, self.box) def runFunction(self, func, pargs, kargs, mode=BLOCK): redirectGuiFunc(func, pargs, kargs, mode, self.box) def write(self, line): self.box.write(line)
def existingBox1(): win = Toplevel() box = GuiOutput(win) box.pack() Button(win, text='OK', command=win.destroy).pack() # erase window box.write('--Hello GuiStreams world--\n') redirectGuiShellCmd('ls *.py', BLOCK, box) box.write('--Second command--\n') redirectGuiShellCmd('ls *.txt', BLOCK, box) box.write('--Command finished--\n')
def redirectGuiShellCmd(command, mode=THREAD, currBox=None): input = os.popen(command, 'r') output = currBox or GuiOutput(Toplevel()) def reader(input, output): # show shell command while 1: # output in a popup line = input.readline() # text box widget if not line: break output.write(line) if mode == BLOCK: reader(input, output) elif mode == ONIDLE: output.after_idle(reader, input, output) elif mode == THREAD: thread.start_new_thread(reader, (input, output)) else: assert 0, 'Bad run mode'
def redirectGuiFunc(func, pargs, kargs, mode=BLOCK, currBox=None): streams = sys.stdin, sys.stdout sys.stdin = GuiInput() # run func with its sys.stdout = currBox or GuiOutput(Toplevel()) # stdin/out streams if mode == BLOCK: # set to gui devices apply(func, pargs, kargs) sys.stdin, sys.stdout = streams elif mode == ONIDLE: def runner(func, pargs, kargs, streams=streams): apply(func, pargs, kargs) sys.stdin, sys,stdout = streams sys.stdout.after_idle(apply, runner, pargs, kargs) elif mode == THREAD: def runner(func, pargs, kargs, streams=streams): apply(func, pargs, kargs) sys.stdin, sys,stdout = streams thread.start_new_thread(runner, (func, pargs, kargs)) else: assert 0, 'Bad run mode'
def redirectedGuiShellCmd(command, root): input = os.popen(command, 'r') output = GuiOutput(root) thread.start_new_thread(producer, (input, )) consumer(output, root)
from guiStreams import GuiOutput myport = 50008 sockobj = socket(AF_INET, SOCK_STREAM) sockobj.bind(('', myport)) sockobj.listen(5) print('starting') PortableLauncher('nongui', 'socket-nongui.py -gui')() print('accepting') conn, addr = sockobj.accept() conn.setblocking(False) print('accepted') def checkdata(): try: message = conn.recv(1024) # output.write(message + '\n') print(message, file=output) except error: print('no data') root.after(1000, checkdata) root = Tk() output = GuiOutput(root) checkdata() root.mainloop()
def __init__(self, parent=None): Toplevel.__init__(self, parent) # single window model self.box = GuiOutput(self) # embed an output frame
def redirectedGuiShellCmd(command, root): input = os.popen(command, 'r') # pipes shell command output = GuiOutput(root) # redirects output to Tk window thread.start_new_thread(producer, (input,)) # output to queue consumer(output, root) # tries to retrieve from queue