在Tkinter Widge中显示终端状态

2024-09-30 08:34:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在macossierra上使用python2.7.10,并用我的raspberrypi创建了一个rsync over ssh连接。这个想法是在raspberrypi上同步本地文件夹和远程文件夹。在

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

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

谢谢你的帮助


2017年9月5日: 我设法用“| tee”把结果写到一个文件中,我的命令现在是这样的

^{pr2}$

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

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

但是文本框显示了所有这些行

  Transfering

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

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

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

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


Tags: 文件to文件夹kbtkinter状态mbfolder
1条回答
网友
1楼 · 发布于 2024-09-30 08:34:45

这里有一个使用python3的例子(因为我有这个例子)。其本质与引用示例中一样,使用线程将管道逐行读入队列,然后使用after事件读取队列。增强将是每次从队列中读取所有项,并在子进程停止后停止事件序列。使用python3 tkinter_rsync.py rsync://servername/sharename或类似的方法进行测试。在

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)

相关问题 更多 >

    热门问题