繁体   English   中英

如何将脚本输出重定向到tkinter窗口?

[英]How do I redirect output of a script to a tkinter window?

我试图点击一个tkinter按钮打开一个新的tkinter窗口,以便在必要时使用滚动条在其中执行一直到最后的脚本。 但是,我只是成功地使它在linux窗口中以多种方式运行而不是在tkinter窗口中运行。 有人可以帮我将这个脚本的输出重定向到顶层窗口吗?

self.button_run = Button(self, text="RUN", width=16, command=self.callpy)
self.button_run.grid(row=25, column=0, columnspan=2, sticky=(W + E + N + S))

def run_robbot(self):
    new = Toplevel(self)
    new.geometry('500x200')
    label = Message(new, textvariable=self.callpy, relief=RAISED)
    label.pack()

def callpy(self):
    pyprog = 'check_asim.robot'
    call(['robot', pyprog])

在上面的代码片段中,如果我在Call中将callpy传递给命令,它会在linux窗口中运行机器人脚本。 如果我替换它来调用run_robbot这是我想要的和期望的,它只是弹出一个带有消息框的新窗口而不运行传递给textvariable的相同脚本。 我也尝试使用Enter代替Message Box。

我希望在点击按钮时在Toplevel tkinter窗口中执行callpy。 我该怎么做? 任何tkinter运算符都可以,只要它限制在tkinter窗口。

如果要捕获命令的输出,则应使用subprocess.run(cmd,capture_output=True) 以下是示例代码:

import subprocess
from tkinter import *

class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        Button(self, text='Run', command=self.run_robot).pack()

    def run_robot(self):
        win = Toplevel(self)
        win.wm_attributes('-topmost', True)
        output = Text(win, width=80, height=20)
        output.pack()
        output.insert(END, 'Running ....')
        output.update()
        result = self.callpy()
        output.delete(1.0, END)
        output.insert(END, result)

    def callpy(self):
        pyprog = 'check_asim.robot'
        return subprocess.run(['robot', pyprog], capture_output=True).stdout

App().mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM