這篇文章給大家介紹TK庫怎么在python項目中使用,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
具體如下:
#!/usr/bin/python3.5
# -*- coding: UTF-8 -*-
import tkinter # 導入 Tkinter 庫
import tkinter.messagebox # 導入消息框庫
import os #導入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:
'''
與子進程通信,給它輸入
data.stdin.write(("abcdf\n").encode())
data.stdin.flush()
'''
t1.config(state='normal')#設置為可編輯
#t1.delete('1.0','end') #清空文本框
t1.insert('end',data.stdout.readline())
t1.see('end')#設置顯示最末尾的數據
t1.update()
t1.config(state='disabled')#設置為無法編輯
def show_ls():
t2.config(state='normal')#設置為可編輯
t2.delete('1.0','end') #清空文本框
t2.insert('end',os.popen('ls').read())
t2.config(state='disabled')#設置為無法編輯
#---創建窗口對象---
root_window = tkinter.Tk()
root_window.title('TEST BY FC')
root_window.geometry('500x500')
#---創建容器---
main_frame = tkinter.Frame(root_window)
main_frame.pack()
#---創建列表---
li = ['C','python','php','html','SQL','java']
#---創建兩個列表組件---
listb = tkinter.Listbox(root_window)
#---給小部件插入數據---
for item in li:
listb.insert(0,item)
listb.pack()
#---創建子容器,在子容器上創建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')
#創建按鈕
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()
#創建滾動條
s1=tkinter.Scrollbar()
s1.pack(side='right',fill='y') # side是滾動條放置的位置,上下左右。fill是將滾動條沿著y軸填充
#創建文本顯示框
t1 = tkinter.Text(bg='lightgreen',width=30, height=10,state='disabled',yscrollcommand=s1.set)#設置為無法編輯
t1.pack()
s1.config(command=t1.yview)
t2 = tkinter.Text(bg='lightblue',width=10, height=2,state='disabled')#設置為無法編輯
t2.pack()
#t2.config(yscrollcommand=s1.set)
#---進入消息循環---
root_window.mainloop()利用TK庫做了一個簡單的界面,很多細節沒有處理,只供學習使用
其中.a.out是一個循環輸出的程序,printf輸出需要加fflush(stdout)清空緩沖區才可以,cout會自動清空
關于TK庫怎么在python項目中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。