Show terminal status in a Tkinter widget

2024/10/13 1:23:18

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.

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 ")

Thank you for your help


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))

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)

The output file looks like this:

sending incremental file list
folder/0   0%    0.00kB/s    0:00:00 (xfr#0, to-chk=397/410)
folder/Bigfile.avi32,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

  Transfering3,776   0%    0.00kB/s    0:00:00 16,567,040   1%   13.69MB/s    0:01:15  

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

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 ?

Answer

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, Emptydef read_pipe(widget, queue):interval = 1try:line = queue.get_nowait()except Empty:interval = 5passelse: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 = Truetid.start()root.after(0, lambda: read_pipe(text, queue))root.mainloop()if __name__ == '__main__':main(sys.argv)
https://en.xdnf.cn/q/118135.html

Related Q&A

Python Convert HTML into JSON using Soup

These are the rulesThe HTML tags will start with any of the following <p>, <ol> or <ul> The content of the HTML when any of step 1 tags is found will contain only the following tags: …

I/O Error while saving Excel file - Python

Im using python to open an existing excel file and do some formatting and save and close the file. My code is working good when the file size is small but when excel size is big (apprx. 40MB) Im gettin…

How to stop the python turtle from drawing

Can anyone tell me why this code always has a line on the screen and also how to stop it?Slight problem with this is that every time this happens, I always get a line on my canvas no matter what I try…

Replace values in a string

So the challenge was to replace a specific word in a sentence with asterisks with equivalent length to that word - 3 letters 3 asterisks etc.Section One does not work, but Section Two does - can anyon…

Select n data points from plot

I want to select points by clicking om them in a plot and store the point in an array. I want to stop selecting points after n selections, by for example pressing a key. How can I do this? This is wha…

Python azure uploaded file content type changed to application/octet-stream

I am using python Azure sdk. When the file uploaded its content type changed to application/octet-stream. I want to set its default content type like image/png for PNG image.I am using following method…

the dumps method of itsdangerous throws a TypeError

I am following the guide of 『Flask Web Development』. I want to use itsdangerous to generate a token, but some problems occured. Here is my code:def generate_confirmation_token(self, expiration=3600):…

SP 500 List python script crashes

So I have been following a youtube tutorial on Python finance and since Yahoo has now closed its doors to the financial market, it has caused a few dwelling problems. I run this codeimport bs4 as bs im…

sleekxmpp threaded authentication

so... I have a simple chat client like so:class ChatClient(sleekxmpp.ClientXMPP):def __init__(self, jid, password, server):sleekxmpp.ClientXMPP.__init__(self, jid, password, ssl=True)self.add_event_han…

DoxyPy - Member (variable) of namespace is not documented

I get the error message warning: Member constant1 (variable) of namespace <file_name> is not documented. for my doxygen (doxypy) documentation. I have documented the file and all functions and cl…