在 Tkinter 小部件中显示终端状态 [英] Show terminal status in a Tkinter widget

查看:59
本文介绍了在 Tkinter 小部件中显示终端状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MacOs Sierra 上使用 python2.7.10,并通过 ssh 连接与我的 raspberrypi 创建了 rsync.想法是将我的本地文件夹与 raspberrypi 上的远程文件夹同步.

I am using python2.7.10 on MacOs Sierra and have created a rsync over ssh connection with my raspberrypi. The Idea is to synchronize my local folder with my remote folder on the raspberrypi.

我的函数 syncroFolder() 工作正常,但如何在我的 tkinter 窗口上显示实时状态?有什么简单的解决方案吗?我无法理解发布的问题:将命令行结果重定向到 tkinter GUI"

My function syncroFolder() is working fine, but how can I show the real time status on my tkinter window ? is there any easy solution ? I can't understand the question posted: "Redirect command line results to a tkinter GUI"

def syncroFolder():
    os.system("rsync -avz -e \"ssh -p port\" /source/path /folder/to/rcync ")

感谢您的帮助

2017-09-05:我设法用| tee"将结果写入文件中,我的命令现在看起来像这样

2017-09-05: I managed to write the result in a file with the "| tee " my command looks like this now

os.system("rsync -avzr -P --info=progress2 -e \"ssh -p %s\" %s %s | tee %s & "  %(port,dossier,destination,exampleOutputTxtAdd))

我在 TKinter 中创建了一个文本框,显示文件的最后一行

I have created a textbox in TKinter that display the last line of the file

status, lastLineInFile = commands.getstatusoutput("tail -n 1 "+ exampleOutputTxtAdd)
T.insert(END,'Transfering: \n'+lastLineInFile)

输出文件如下所示:

sending incremental file list
folder/

          0   0%    0.00kB/s    0:00:00 (xfr#0, to-chk=397/410)
folder/Bigfile.avi

     32,768   0%    0.00kB/s    0:00:00  
  2,555,904   0%    2.39MB/s    0:07:18  
  3,112,960   0%    1.41MB/s    0:12:23  
  3,637,248   0%    1.11MB/s    0:15:44

但文本框显示所有这些行

but the textbox shows all those lines

  Transfering

      3,776   0%    0.00kB/s    0:00:00 

 16,567,040   1%   13.69MB/s    0:01:15  

并继续添加行,即使我阅读了输出文件的 -n = 1 行.

and keep adding lines, even if I read the -n = 1 line of the output file.

我一直在尝试使用 --out-format=\"%t %o %f %b\",但是我只有在传输后才有状态(如果文件很大)......我尝试了很多选项,但没有一个对我有用......我不明白为什么它不只显示输出文件的最后一行.你有什么建议吗?

I have been trying to use the --out-format=\"%t %o %f %b\", but then I have the status only after transfer (in case of a big file)... I tried many options and none worked for me... I don't understand why it doesn't display only the last line of the output file. Do you have any suggestions ?

推荐答案

这里是一个使用 python3 的例子(因为这就是我所拥有的).实质是如参考例子中使用线程将管道逐行读取到队列中,然后使用after事件读取队列.增强功能是每次从队列中读取所有项目,并在子进程停止后停止 after 事件序列.使用 python3 tkinter_rsync.py rsync://servername/sharename 或类似的东西进行测试.

Here is an example using python3 (because that's what I have). The essence is as in the references example to use a thread to read the pipe line by line into a queue and then use an after event to read the queue. Enhancements would be to read all items from the queue each time and stop the after event sequence once the subprocess has halted. Test using python3 tkinter_rsync.py rsync://servername/sharename or something like that.

import sys
import tkinter as tk
import tkinter.ttk as ttk
from subprocess import Popen, PIPE
from threading import Thread
from queue import Queue, Empty

def read_pipe(widget, queue):
    interval = 1
    try:
        line = queue.get_nowait()
    except Empty:
        interval = 5
        pass
    else:
        widget.insert('end', line)
    widget.after(interval, lambda: read_pipe(widget, queue))

def queue_line(pipe, queue):
    for line in iter(pipe.readline, b''):
        queue.put(line)
    pipe.close()

def main(argv):
    root = tk.Tk()
    text = tk.Text(root)
    vs = ttk.Scrollbar(root, orient='vertical', command=text.yview)
    text.configure(yscrollcommand=vs.set)
    text.grid(row=0, column=0, sticky='news')
    vs.grid(row=0, column=1, sticky='ns')
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)

    process = Popen(['rsync', '--list-only', argv[1]], stdout=PIPE)
    queue = Queue()
    tid = Thread(target=queue_line, args=(process.stdout, queue))
    tid.daemon = True
    tid.start()

    root.after(0, lambda: read_pipe(text, queue))
    root.mainloop()

if __name__ == '__main__':
    main(sys.argv)

这篇关于在 Tkinter 小部件中显示终端状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆