python TK庫簡單應(yīng)用(實(shí)時(shí)顯示子進(jìn)程輸出)
本文介紹python TK庫簡單應(yīng)用(實(shí)時(shí)顯示子進(jìn)程輸出),分享給大家,具體如下:
#!/usr/bin/python3.5 # -*- coding: UTF-8 -*- import tkinter # 導(dǎo)入 Tkinter 庫 import tkinter.messagebox # 導(dǎo)入消息框庫 import os #導(dǎo)入OS庫 import subprocess def show_something(): tkinter.messagebox.showinfo( "Python", "Hello everyone") def show_while(): if button3['text'] == 'WHILE_run': button3['text'] = 'WHILE_close' else: button3['text'] = 'WHILE_run' data = subprocess.Popen('./a.out',stdout = subprocess.PIPE,stdin=subprocess.PIPE,shell=True) while True: ''' 與子進(jìn)程通信,給它輸入 data.stdin.write(("abcdf\n").encode()) data.stdin.flush() ''' t1.config(state='normal')#設(shè)置為可編輯 #t1.delete('1.0','end') #清空文本框 t1.insert('end',data.stdout.readline()) t1.see('end')#設(shè)置顯示最末尾的數(shù)據(jù) t1.update() t1.config(state='disabled')#設(shè)置為無法編輯 def show_ls(): t2.config(state='normal')#設(shè)置為可編輯 t2.delete('1.0','end') #清空文本框 t2.insert('end',os.popen('ls').read()) t2.config(state='disabled')#設(shè)置為無法編輯 #---創(chuàng)建窗口對象--- root_window = tkinter.Tk() root_window.title('TEST BY FC') root_window.geometry('500x500') #---創(chuàng)建容器--- main_frame = tkinter.Frame(root_window) main_frame.pack() #---創(chuàng)建列表--- li = ['C','python','php','html','SQL','java'] #---創(chuàng)建兩個(gè)列表組件--- listb = tkinter.Listbox(root_window) #---給小部件插入數(shù)據(jù)--- for item in li: listb.insert(0,item) listb.pack() #---創(chuàng)建子容器,在子容器上創(chuàng)建Label--- frm1 = tkinter.Frame(main_frame) frm1.pack() tkinter.Label(frm1, text='hello', bg='green', width=10, height=2).pack(side='left') frm2 = tkinter.Frame(main_frame) frm2.pack() tkinter.Label(frm1,text=' world', bg='yellow', width=10, height=2).pack(side='right') #創(chuàng)建按鈕 button_frm = tkinter.Frame(root_window) button_frm.pack() button1 = tkinter.Button(button_frm, text = "確定", bg='red', fg='white', width=10, height=2, command = show_something) button1.pack() button2 = tkinter.Button(button_frm, text = "LS", bg='blue', fg='white', width=10, height=2, command = show_ls) button2.pack() button3 = tkinter.Button(button_frm, text = "WHILE_run", bg='green', fg='white', width=10, height=2, command = show_while) button3.pack() #創(chuàng)建滾動(dòng)條 s1=tkinter.Scrollbar() s1.pack(side='right',fill='y') # side是滾動(dòng)條放置的位置,上下左右。fill是將滾動(dòng)條沿著y軸填充 #創(chuàng)建文本顯示框 t1 = tkinter.Text(bg='lightgreen',width=30, height=10,state='disabled',yscrollcommand=s1.set)#設(shè)置為無法編輯 t1.pack() s1.config(command=t1.yview) t2 = tkinter.Text(bg='lightblue',width=10, height=2,state='disabled')#設(shè)置為無法編輯 t2.pack() #t2.config(yscrollcommand=s1.set) #---進(jìn)入消息循環(huán)--- root_window.mainloop()
利用TK庫做了一個(gè)簡單的界面,很多細(xì)節(jié)沒有處理,只供學(xué)習(xí)使用
其中.a.out是一個(gè)循環(huán)輸出的程序,printf輸出需要加fflush(stdout)清空緩沖區(qū)才可以,cout會自動(dòng)清空
效果如下
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python啟動(dòng)辦公軟件進(jìn)程(word、excel、ppt、以及wps的et、wps、wpp)
- Python子進(jìn)程subpocess原理及用法解析
- python多進(jìn)程 主進(jìn)程和子進(jìn)程間共享和不共享全局變量實(shí)例
- Python進(jìn)程,多進(jìn)程,獲取進(jìn)程id,給子進(jìn)程傳遞參數(shù)操作示例
- 在Python中os.fork()產(chǎn)生子進(jìn)程的例子
- 對Python subprocess.Popen子進(jìn)程管道阻塞詳解
- python清理子進(jìn)程機(jī)制剖析
- python subprocess 殺掉全部派生的子進(jìn)程方法
- 如何用 Python 子進(jìn)程關(guān)閉 Excel 自動(dòng)化中的彈窗
相關(guān)文章
python中round函數(shù)保留兩位小數(shù)的方法
在本篇內(nèi)容里小編給各位分享的是一篇關(guān)于python中round函數(shù)保留兩位小數(shù)的方法及相關(guān)知識點(diǎn),有興趣的朋友們可以學(xué)習(xí)下。2020-12-12python實(shí)現(xiàn)類之間的方法互相調(diào)用
下面小編就為大家分享一篇python實(shí)現(xiàn)類之間的方法互相調(diào)用,具有很的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python實(shí)現(xiàn)類的創(chuàng)建與使用方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)類的創(chuàng)建與使用方法,結(jié)合簡單計(jì)算器功能實(shí)例分析了Python類的定義與使用方法,需要的朋友可以參考下2017-07-07python2.7無法使用pip的解決方法(安裝easy_install)
下面小編就為大家分享一篇python2.7無法使用pip的解決方法(安裝easy_install),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04利用Python計(jì)算質(zhì)數(shù)與完全數(shù)的方法實(shí)例
這篇文章主要介紹了利用Python計(jì)算質(zhì)數(shù)與完全數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03python的pytest框架之命令行參數(shù)詳解(上)
這篇文章主要介紹了python的pytest框架之命令行參數(shù)詳解,pytest是一款強(qiáng)大的python自動(dòng)化測試工具,可以勝任各種類型或者級別的軟件測試工作。pytest提供了豐富的功能,包括assert重寫,第三方插件,需要的朋友可以參考下2019-06-06用python實(shí)現(xiàn)的可以拷貝或剪切一個(gè)文件列表中的所有文件
python 實(shí)現(xiàn)剪切或是拷貝一個(gè)文件列表中的所有文件2009-04-04