Re: [Tkinter-discuss] Help needed
Hi, On Tue, 30 Jul 2019 15:43:53 +0530 Balasubramani K wrote: > 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 actually this can be done, at least with X11; here with debian I can do something like: from tkinter import * from subprocess import Popen root=Tk() f=Frame(root, container=1, width=300, height=300) f.pack(fill='both', expand=1) pipe = Popen(('urxvt', '-embed', str(f.winfo_id( root.mainloop() Still, I am not so sure if this is a good idea. First, those embedded windows can be tricky by themselves; there may also be DE/WM dependent issues (no idea if this works on windows or OSX at all). Then you will have to consider user interactions with the terminal, for example what if the user decides to simply type "exit" at the command prompt? Or hits Ctrl+\ while the process is running? Of course I do not know what you want to achieve in detail, but maybe if you want to use a gui with tqdm it would be a better approach to catch the string (I guess the "data" in your code snippet?) and instead of printing it to stdout just putting it on a tkinter Label. I don't know about tqdm, maybe you could change the output format a bit to omit the progress bar at all, calculate the value for the ttk.Progressbar with the percent value and put an output string like 47% 76442/161258.81970214844 [14:12<16:26, 86.00KB/s] on a Label next to the progress bar. That would give the user all the information they need, would be easier for you to handle, be more reliable and surely make a better looking gui. Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. "Life and death are seldom logical." "But attaining a desired goal always is." -- McCoy and Spock, "The Galileo Seven", stardate 2821.7 ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] Help needed
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 wrote: > Hi, > > On Mon, 29 Jul 2019 15:26:30 +0530 > Balasubramani K 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
Re: [Tkinter-discuss] Help needed
Hi, On Mon, 29 Jul 2019 15:26:30 +0530 Balasubramani K 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] Help needed
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 could you please help me how to do it Thanks Bala ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] help needed with radiobutton
Sang Park wrote: how do I select radionbutton by default? for my school project, I need to have 10 radio buttons and have half of them selected I have for i in range(10): x = IntVar() if i 5: rb = Radiobutton(buttonFrame, variable=x, value=1,state=DISABLED) else: rb = Radiobutton(buttonFrame, variable=x, value=0,state=DISABLED) Your problem is that you throw away your IntVar-s after each round in the loop. Instead keep references to them around as in this example, which is a simple rewrite of your example. from Tkinter import * root=Tk() x=[] for i in range(10): x.append(IntVar()) if i 5: rb = Radiobutton(root, variable=x[-1], value=1, state=DISABLED) else: rb = Radiobutton(root, variable=x[-1], value=0, state=DISABLED) rb.pack() root.mainloop() By the way, are you sure you really want state=DISABLED? That makes the radiobuttons disabled... /MiO ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss