# -*- coding: gbk -*-
from __future__ import print_function
##from __future__ import unicode_literals
import os
import sys
import io
import time
from fltk import *
from subprocess import Popen, PIPE, STARTUPINFO, STARTF_USESHOWWINDOW, SW_HIDE
PY2 = sys.version_info[0]==2
_size = 20
def _utf8(s):
try:
return unicode(s).encode('utf-8') # python2
except:
return s
class __redirection__:
def __init__(self, ctrl):
self.ctrl = ctrl
## self.__console__ = sys.stdout
def write(self, output_stream):
buff = self.ctrl.buffer()
buff.append(_utf8(output_stream))
Fl.flush()
self.ctrl.scroll(buff.length(), 1)
class MyApp(object):
def __init__(self):
self.mainform = Fl_Window(400,200,800,600)
self.mainform.label('Test subprocess')
self.mainform.color(FL_LIGHT2)
self.button_run = Fl_Button(440,20,100,40)
self.button_run.labelsize(_size)
self.button_run.label(_utf8("执行"))
self.button_run.callback(self.button_callback)
self.button_stop = Fl_Button(560,20,100,40)
self.button_stop.labelsize(_size)
self.button_stop.label(_utf8("停止"))
self.button_stop.callback(self.button_callback)
self.button_test = Fl_Button(680,20,100,40)
self.button_test.labelsize(_size)
self.button_test.label("Test")
self.button_test.callback(self.button_callback, [(9, 8), 'button_test'])
self.input_cmd = Fl_Input(20, 20, 400, 40)
self.input_cmd.labelsize(_size)
self.input_cmd.textsize(_size)
self.input_cmd.value('ping 127.0.0.1')
self.text_log_disp = Fl_Text_Editor(0, 80, self.mainform.w(), self.mainform.h()-80)
self.text_log_disp.textsize(17)
self.text_log_disp.textfont(FL_COURIER) # 等宽字体
self.text_log_buff = Fl_Text_Buffer()
self.text_log_disp.buffer(self.text_log_buff)
self.popup_menu = Fl_Menu_Button(0, 80, self.mainform.w(), self.mainform.h()-80)
## self.mnu_edit = (
## ("Undo", 0, self.undo_cb, self.text_log_buff, FL_MENU_DIVIDER ),
## ("Cu&t", FL_CTRL + ord('x'), self.cut_cb, self.text_log_disp ),
## ("&Copy", FL_CTRL + ord('c'), self.copy_cb, self.text_log_disp),
## ("&Paste", FL_CTRL + ord('v'), self.paste_cb, self.text_log_disp ),
## ("&Delete", 0, self.delete_cb, self.text_log_disp ),
## (None,) )
self.mnu_edit = (
(_utf8("撤消"), 0, 0, 0, FL_MENU_DIVIDER ),
(_utf8("剪切(&t)"), FL_CTRL + ord('x') ),
("&Copy", FL_CTRL + ord('c')),
("&Paste", FL_CTRL + ord('v'), 0),
("&Delete", 0, 0),
(None,) )
self.popup_menu.type(Fl_Menu_Button.POPUP3)
self.popup_menu.copy(self.mnu_edit)
self.popup_menu.textsize(16)
self.popup_menu.callback(self.menu_callback, self.text_log_disp)
self.mainform.resizable(self.mainform.this)
self.mainform.size_range(300, 400, 1024, 768)
self.mainform.end()
ouput_fltk = __redirection__(self.text_log_disp)
sys.stdout = ouput_fltk
sys.stderr = ouput_fltk
def print_process(self, process):
sout = io.open(process.stdout.fileno(), 'rb', closefd=False)
buf = sout.read1(1024)
if len(buf) == 0:
print('\n----------End.----------\n')
Fl.remove_idle(self.print_process, process)
return
print(buf.decode('gbk'), end='') # python 2,3 都能用
def button_callback(self, this, data=0):
if this is self.button_run:
if PY2:
st=STARTUPINFO
st.dwFlags=STARTF_USESHOWWINDOW
st.wShowWindow=SW_HIDE
else:
st = STARTUPINFO(dwFlags=STARTF_USESHOWWINDOW, wShowWindow=SW_HIDE)
cmd = self.input_cmd.value()
try:
self.cmd_proc = Popen(cmd, shell=True, startupinfo=st, stdout=PIPE, stdin=PIPE, stderr=PIPE)
except Exception as err:
print(err)
## quit()
Fl.add_idle(self.print_process, self.cmd_proc)
## self.sout = io.open(self.cmd_proc.stdout.fileno(), 'rb', closefd=False)
## while True:
## buf = self.sout.read1(1024)
## if len(buf) == 0: break
## print(buf.decode('gbk'), end='')
## print('\n----------END----------\n')
## self.cmd_proc.wait() # 这种方式是阻塞式的
## for line in self.cmd_proc.stdout.readlines():
## print(line.decode('gbk'))
## print('\n----------END----------\n')
elif this is self.button_stop:
try:
Fl.remove_idle(self.print_process, self.cmd_proc)
print('\n\n----------Stop!----------\n')
except:
pass
elif this is self.button_test:
print(self.text_log_disp.textfont(), 'data=', data)
test1()
def menu_callback(self, this, editor):
## print this
## print this.mvalue() # Fl_menu_Item
## print this.mvalue().label()
## print this.text()
menu_index = this.value()
## print menu_index
if menu_index==0: # undo
editor.buffer().undo()
elif menu_index==1: # cut
editor.kf_cut(0, editor)
elif menu_index==2: # copy
## Fl_Text_Editor_kf_copy(0, editor)
editor.kf_copy(0, editor)
elif menu_index==3: # paste
## Fl_paste(editor) # Fl_Text_Buffer用
editor.kf_paste(0, editor)
elif menu_index==4: # delete
editor.kf_delete(0, editor)
## editor.remove_selection() # Fl_Text_Buffer用
def run(self):
self.mainform.show()
## Fl.run()
Fl.mt_run(self.mainform)
# 以下在多线程中使用
## while self.mainform.visible():
## time.sleep(0.01)
## Fl.check()
def test1():
from prettytable import PrettyTable
x = PrettyTable(["City name中国", "Area", "Population", "Annual Rainfall"], encoding='gbk')
x.sortby = "Population"
x.reversesort = True
x.int_format["Area"] = "04d"
x.float_format = "6.1f"
x.align["City name"] = "l" # Left align city names
x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane中国", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])
print(x)
if __name__ == '__main__':
app = MyApp()
app.run()
python27练习代码之一:pyfltk,subprocess,io,prettytable, 输出重定向
最新推荐文章于 2024-07-30 16:25:12 发布