欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python TK庫簡單應(yīng)用(實(shí)時(shí)顯示子進(jìn)程輸出)

 更新時(shí)間:2019年10月29日 10:01:28   作者:IT8343  
這篇文章主要介紹了python TK庫簡單應(yīng)用(實(shí)時(shí)顯示子進(jìn)程輸出),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文介紹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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論