[英]How do I redirect output of a script to a tkinter window?
I am trying to click a tkinter button to open up a new tkinter window to execute a script within it all the way up to the end with a scroll bar if necessary. 我试图点击一个tkinter按钮打开一个新的tkinter窗口,以便在必要时使用滚动条在其中执行一直到最后的脚本。 However, I have only succeeded this far in getting it to run in multitude of ways in a linux window and not within a tkinter window.
但是,我只是成功地使它在linux窗口中以多种方式运行而不是在tkinter窗口中运行。 Can someone help me with redirecting the output of this script into the toplevel window?
有人可以帮我将这个脚本的输出重定向到顶层窗口吗?
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])
In the snippet above, if I pass callpy to command in Button it runs the robot script in a linux window. 在上面的代码片段中,如果我在Call中将callpy传递给命令,它会在linux窗口中运行机器人脚本。 If I replace it to call run_robbot which is what I want and expect, it just pops up a new window with a Message Box without running the same script passed to textvariable.
如果我替换它来调用run_robbot这是我想要的和期望的,它只是弹出一个带有消息框的新窗口而不运行传递给textvariable的相同脚本。 I have tried Enter in place of Message Box as well.
我也尝试使用Enter代替Message Box。
I want callpy to be executed in Toplevel tkinter window at the click of the button. 我希望在点击按钮时在Toplevel tkinter窗口中执行callpy。 How do I do it?
我该怎么做? Any tkinter operator is fine as long as it confines to the tkinter window.
任何tkinter运算符都可以,只要它限制在tkinter窗口。
If you want to capture the output of the command, you should use subprocess.run(cmd,capture_output=True)
instead. 如果要捕获命令的输出,则应使用
subprocess.run(cmd,capture_output=True)
。 Below is an sample code: 以下是示例代码:
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.