Hi Michael,
Thanks for the help,It is useful.

I have a questions :
Cant we use tqdm progress bar in tkinter window ??
In other words, can't we get a terminal window in tkinter window; where
whatever the output of the code that is executed should be shown in the
terminal. like how we use in pycharm or cmd prompt in windows ...etc

Thanks
Bala



On Tue, Jul 30, 2019 at 3:06 PM Michael Lange <klappn...@web.de> wrote:

> Hi,
>
> On Mon, 29 Jul 2019 15:26:30 +0530
> Balasubramani K <bala.purs...@gmail.com> wrote:
>
> > Hi ,
> > I'm using following modules in python 3:
> > import requests
> > import os
> > from tqdm import tqdm
> >
> >
> > a piece of code from my script,
> >
> > requests.get(url, stream=True,allow_redirects=True)
> > total_size = int(r.headers['content-length'])
> > with open(SaveLocation + SaveFilename, 'wb') as f:
> > for data in tqdm(iterable=r.iter_content(chunk_size=Chunk_size),
> > total=total_size / Chunk_size, unit='KB', desc=SaveFilename):
> >                 f.write(data)
> > f.close()
> >
> > when I run the python file in terminal I get output as below :
> >
> > 47%|████▋     | 76442/161258.81970214844 [14:12<16:26, 86.00KB/s]
> >
> > this progress bar I got it from a module called tqdm
> >
> > Now I'm planning to make UI,  using tkinter with start stop button.
> > but when I click start button I need the same tqdm progress bar output
> > in the tkinter window
>
> I have used the ttk.Progressbar with external shell commands; if you
> don't necessarily need to use tqdm maybe you could use the same technique
> for what you are doing.
> I added a quick-and-dirty example how to do this below; in the example I
> used the command "cdrdao disk-info" on linux, which when no disc is in
> the drive will try for about 10 sec. to make sense of the non-existing
> disc before giving up, printing "Unit not ready" to stderr for 10 times in
> the process which I use here to draw the progress bar.
> The gui remains responsive without having to use threads (as you can see
> when you hit the stop button).
> If you don't need any external command but just want to compare file
> sizes, the code may become even simpler.
>
> I hope this helps
>
> Michael
>
> ####################################################################
>
> from tkinter import *
> from tkinter import ttk
> from subprocess import Popen, PIPE
> import os, fcntl
>
> root = Tk()
> root.pipe = None
> f = Frame(root)
> f.pack(fill='both', expand=1)
> p = ttk.Progressbar(f)
> p.pack(padx=100, pady=(100,10))
>
>
> MSG = ''
> def update_progress(msg):
>     global MSG
>     print(msg)
>     MSG += msg
>     perc = MSG.count('Unit not ready') * 10
>     p.configure(value=perc)
>
> def update():
>     sts = root.pipe.poll()
>     try:
>         out = root.pipe.stderr.read()
>     except IOError:
>         out = b''
>     if out:
>         update_progress(out.decode())
>     if sts is None:
>         root.after(200, update)
>     else:
>         del root.pipe
>         root.pipe = None
>         p.configure(value=100)
>         print('Process finished with status %d' % sts)
>
> def kill():
>     if root.pipe:
>         try:
>             os.kill(root.pipe.pid, 15)
>         except:
>             pass
>
> def start():
>     global MSG
>     MSG = ''
>     p.configure(value=0)
>     root.pipe = Popen(('cdrdao', 'disk-info'), stdout=PIPE, stderr=PIPE)
>     fcntl.fcntl(root.pipe.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
>     fcntl.fcntl(root.pipe.stderr.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
>     root.after(200, update)
>
> Button(text='stop', command=kill).pack(side='bottom', pady=20)
> Button(text='start', command=start).pack(side='bottom', pady=20)
>
> root.mainloop()
>
> ####################################################################
>
>
>
>
> .-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.
>
> I am pleased to see that we have differences.  May we together become
> greater than the sum of both of us.
>                 -- Surak of Vulcan, "The Savage Curtain", stardate 5906.4
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss@python.org
> https://mail.python.org/mailman/listinfo/tkinter-discuss
>
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to