2 python linux tkinter python-3.x
我正在尝试为我的程序制作一个非常简单的 GUI(这里我用测试函数替换了它)。我可以通过 gui 运行我的代码,但程序的输出显示在终端中。我希望输出在我的原始窗口中可见。
这是我的代码:
import tkinter as tk
from test import *
from tkinter import *
import os
root = tk.Tk()
canvas = tk.Canvas(root, height=400, width =550, bg ="#202020")
canvas.pack()
#frame = tk.Frame(root, bg="white");
#frame.place(relwidth=0.8, relheight = 0.8, relx=0.1, rely=0.1)
topFrame = tk.Frame(root, bg="#202020")
topFrame.place(relwidth=1, relheight = 0.75)
bottomFrame = tk.Frame(root, bg="#202020")
bottomFrame.place(rely=0.75, relwidth=1, relheight = 0.25)
launch = tk.Button(bottomFrame, text="Launch", bg="white", fg="#202020", font="noah 10 bold", padx=20, command=test)
launch.place(in_=bottomFrame, rely=0.5, relx=0.5, anchor=CENTER)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
这是我调用该函数的另一个文件:
import os
def test():
print("Hello World")
os.system("ping 192.168.0.1 -c 4")
Run Code Online (Sandbox Code Playgroud)
有没有办法捕获这个函数的输出并将其实时显示在我的图形用户界面的顶部框架中?
您可以创建write()带有插入文本tkinter.Text并将其实例分配给的函数的类sys.stdout- 然后print()将发送到tkinter.Text
class Redirect():
def __init__(self, widget):
self.widget = widget
def write(self, text):
self.widget.insert('end', text)
#self.widget.see('end') # autoscroll
# some widget may need it
#def flush(self):
# pass
Run Code Online (Sandbox Code Playgroud)
和
text = tk.Text(root)
text.pack()
# keep original stdout
old_stdout = sys.stdout
# assing Redirect with widget Text
sys.stdout = Redirect(text)
root.mainloop()
# assign back original stdout (if you need it)
sys.stdout = old_stdout
Run Code Online (Sandbox Code Playgroud)
但os.system你必须更换为ie。subprocess.run()捕获输出和print()它。
def test():
print("Hello World")
p = subprocess.run("ping -c 4 stackoverflow.com", shell=True, stdout=subprocess.PIPE)
print(p.stdout.decode())
Run Code Online (Sandbox Code Playgroud)
最少的工作代码
import tkinter as tk
import os
import sys
import subprocess
# --- functions ---
def test():
print("Hello World")
p = subprocess.run("ping -c 4 stackoverflow.com", shell=True, stdout=subprocess.PIPE)
print(p.stdout.decode())
# --- classes ---
class Redirect():
def __init__(self, widget):
self.widget = widget
def write(self, text):
self.widget.insert('end', text)
#self.widget.see('end') # autoscroll
#def flush(self):
# pass
# --- main ---
root = tk.Tk()
text = tk.Text(root)
text.pack()
button = tk.Button(root, text='TEST', command=test)
button.pack()
old_stdout = sys.stdout
sys.stdout = Redirect(text)
root.mainloop()
sys.stdout = old_stdout
Run Code Online (Sandbox Code Playgroud)
两个问题:
从获取所有文本后,它将文本从 发送subprocess到小部件。逐行显示可能需要更多工作。Textping
test()需要一些时间来完成工作并且它会阻塞tkinter,因此无法更新小部件并且会冻结。它可能需要test()单独运行thread,但我不知道这是否会带来其他问题,因为在许多 GUI 框架中,您不能在单独的威胁中使用小部件。
编辑:
版本与threading. 它解决了以前的问题。
但它可能会出现新的问题:)
您可以单击按钮两次,它将pings同时运行两个并混合来自两个的字符串pings
代码没有停止线程的方法(即,当您运行时ping没有-c 4)
代码:
import tkinter as tk
import sys
import subprocess
import threading
# --- functions ---
def run():
threading.Thread(target=test).start()
def test():
print("Hello World")
p = subprocess.Popen("ping -c 4 stackoverflow.com".split(), stdout=subprocess.PIPE, bufsize=1, text=True)
while p.poll() is None:
msg = p.stdout.readline().strip() # read a line from the process output
if msg:
print(msg)
print("Finished")
# --- classes ---
class Redirect():
def __init__(self, widget):
self.widget = widget
def write(self, text):
self.widget.insert('end', text)
#self.widget.see('end') # autoscroll
#def flush(self):
# pass
# --- main ---
root = tk.Tk()
text = tk.Text(root)
text.pack()
button = tk.Button(root, text='TEST', command=run)
button.pack()
old_stdout = sys.stdout
sys.stdout = Redirect(text)
root.mainloop()
sys.stdout = old_stdout
Run Code Online (Sandbox Code Playgroud)
编辑:2021.08.19
Scrollbar带有和 的版本autoscroll
import tkinter as tk
import sys
import subprocess
import threading
# --- classes ---
class Redirect():
def __init__(self, widget, autoscroll=True):
self.widget = widget
self.autoscroll = autoscroll
def write(self, text):
self.widget.insert('end', text)
if self.autoscroll:
self.widget.see("end") # autoscroll
#def flush(self):
# pass
# --- functions ---
def run():
threading.Thread(target=test).start()
def test():
print("Thread: start")
p = subprocess.Popen("ping -c 4 stackoverflow.com".split(), stdout=subprocess.PIPE, bufsize=1, text=True)
while p.poll() is None:
msg = p.stdout.readline().strip() # read a line from the process output
if msg:
print(msg)
print("Thread: end")
# --- main ---
root = tk.Tk()
# - Frame with Text and Scrollbar -
frame = tk.Frame(root)
frame.pack(expand=True, fill='both')
text = tk.Text(frame)
text.pack(side='left', fill='both', expand=True)
scrollbar = tk.Scrollbar(frame)
scrollbar.pack(side='right', fill='y')
text['yscrollcommand'] = scrollbar.set
scrollbar['command'] = text.yview
old_stdout = sys.stdout
sys.stdout = Redirect(text)
# - rest -
button = tk.Button(root, text='TEST', command=run)
button.pack()
root.mainloop()
# - after close window -
sys.stdout = old_stdout
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10773 次 |
| 最近记录: |