python - Tkinter threading and return to text widget -
i've read post on stack overflow,issues intercepting subprocess output in real time, redirect command line results tkinter gui, know have use threading , queue in tkinter, still can't same thing because beginner in program,please help.
the goal: when press button, getting 'top' command output , realtime display in tkinter text widget
the issue: i've tried follow code, still cannot output, have not idea how make work.
from tkinter import * import tkinter tk import subprocess threading import thread queue import queue window = tk.tk() window.title('realtime') window.geometry('800x400') text = tk.text(window) text.pack() button = tk.button(window, text= 'press') button.pack() window.mainloop() this gui outlook, please help
top refreshes , , i'm guessing that's behavior want capture threading , whatnot. in case easier ask top run once, , have tkinter timing , refreshing:
import tkinter tk sh import top def update_text(): text.delete(0.0, tk.end) text.insert(0.0, top('-b', n=1)) window.after(1000, update_text) # call function again in 1 second window = tk.tk() window.title('realtime') window.geometry('800x400') text = tk.text(window) text.pack() button = tk.button(window, text= 'press', command=update_text) button.pack() window.mainloop() you may need install sh run top did, or use subprocess.check_output if want.
text.insert(0.0, subprocess.check_output(['top', '-b', '-n 1']))
Comments
Post a Comment