Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19304 > unrolled thread

problems with tkinter updates

Started byyves@zioup.com
First post2012-01-23 18:09 -0700
Last post2012-01-29 10:34 -0700
Articles 8 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  problems with tkinter updates yves@zioup.com - 2012-01-23 18:09 -0700
    Re: problems with tkinter updates Dave Angel <d@davea.name> - 2012-01-23 22:57 -0500
      Re: problems with tkinter updates yves@zioup.com - 2012-01-23 23:47 -0700
    Re: problems with tkinter updates Peter Otten <__peter__@web.de> - 2012-01-24 10:52 +0100
      Re: problems with tkinter updates woooee <woooee@gmail.com> - 2012-01-24 09:50 -0800
        Re: problems with tkinter updates Peter Otten <__peter__@web.de> - 2012-01-26 14:16 +0100
      Re: problems with tkinter updates yves@zioup.com - 2012-01-27 07:02 -0700
        Re: problems with tkinter updates yves@zioup.com - 2012-01-29 10:34 -0700

#19304 — problems with tkinter updates

Fromyves@zioup.com
Date2012-01-23 18:09 -0700
Subjectproblems with tkinter updates
Message-ID<FxnTq.4267$5r2.3946@newsfe11.iad>
I'm missing something about tkinter updates. How can I give tkinter a chance 
to run?

Here's some code:

import time
import tkinter
import tkinter.scrolledtext

tk = tkinter.Tk()
f = tkinter.Toplevel(tk)
st = tkinter.scrolledtext.ScrolledText(f)
st.pack()



def update():
     print('updating')
     st.see(tkinter.END)
     tk.after(1000, update)


input('hit enter to start')
update()
f = open('/etc/services')

for line in f:
   st.insert(tkinter.END, line + '\n')
   print('got it')
   #time.sleep(5)
   input('more?')

input('finished?')




When I do this (input('more?'), it works as expected. If I comment that line 
out, then the program reads the entire file, then update the window right at 
the end, even if I put a sleep in there. What can I do inside the loop to give 
tk a chance?

Thanks.

-- 
Yves.                                                  http://www.SollerS.ca/
                                                        http://ipv6.SollerS.ca
                                                        http://blog.zioup.org/

[toc] | [next] | [standalone]


#19311

FromDave Angel <d@davea.name>
Date2012-01-23 22:57 -0500
Message-ID<mailman.5005.1327377494.27778.python-list@python.org>
In reply to#19304
On 01/23/2012 08:09 PM, yves@zioup.com wrote:
>
> I'm missing something about tkinter updates. How can I give tkinter a 
> chance to run?
>
> Here's some code:
>
> import time
> import tkinter
> import tkinter.scrolledtext
>
> tk = tkinter.Tk()
> f = tkinter.Toplevel(tk)
> st = tkinter.scrolledtext.ScrolledText(f)
> st.pack()
>
>
>
> def update():
>     print('updating')
>     st.see(tkinter.END)
>     tk.after(1000, update)
>
>
> input('hit enter to start')
> update()
> f = open('/etc/services')
>
> for line in f:
>   st.insert(tkinter.END, line + '\n')
>   print('got it')
>   #time.sleep(5)
>   input('more?')
>
> input('finished?')
>
>
>
>
> When I do this (input('more?'), it works as expected. If I comment 
> that line out, then the program reads the entire file, then update the 
> window right at the end, even if I put a sleep in there. What can I do 
> inside the loop to give tk a chance?
>
You have it backward.  The question is not what you do inside your loop 
to give tk a chance, but rather what do you do to make tk give you a 
chance.  tk doesn't "start" till you make the mainloop() method call, 
and once you call that method, it won't return till the program is exiting.

So, forget about input statements inside some loop.  Input isn't a gui 
concept, it's for console apps.  Gui apps use dialog boxes and such.  
Similarly sleep().  mainloop() will sleep, when there are no events in 
its queue.  If you want to do work, break it into manageable chunks, and 
attach each chunk to some event that tk will fire.

Beyond that, I cannot help, for I don't know tkinter.  But all gui's are 
similar at this level of detail.

-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#19319

Fromyves@zioup.com
Date2012-01-23 23:47 -0700
Message-ID<yusTq.5987$ep4.4165@newsfe16.iad>
In reply to#19311
On 2012-01-23 20:57, Dave Angel wrote:
>>
>>
> You have it backward. The question is not what you do inside your loop to give
> tk a chance, but rather what do you do to make tk give you a chance. tk
> doesn't "start" till you make the mainloop() method call, and once you call
> that method, it won't return till the program is exiting.
>
> So, forget about input statements inside some loop. Input isn't a gui concept,
> it's for console apps. Gui apps use dialog boxes and such. Similarly sleep().
> mainloop() will sleep, when there are no events in its queue. If you want to
> do work, break it into manageable chunks, and attach each chunk to some event
> that tk will fire.

The input statements were there for debugging purpose... I now have got it 
running without any sleep or input, I simply added a tk.update() in the loop. 
It works for updating the window, but when I add buttons to that frame, they 
are quite unresponsive. I'm starting to think I need to split off the reading 
part into a different thread.

-- 
Yves.                                                  http://www.SollerS.ca/
                                                        http://ipv6.SollerS.ca
                                                        http://blog.zioup.org/

[toc] | [prev] | [next] | [standalone]


#19335

FromPeter Otten <__peter__@web.de>
Date2012-01-24 10:52 +0100
Message-ID<mailman.5022.1327398783.27778.python-list@python.org>
In reply to#19304
yves@zioup.com wrote:

> 
> I'm missing something about tkinter updates. How can I give tkinter a
> chance to run?
> 
> Here's some code:
> 
> import time
> import tkinter
> import tkinter.scrolledtext
> 
> tk = tkinter.Tk()
> f = tkinter.Toplevel(tk)
> st = tkinter.scrolledtext.ScrolledText(f)
> st.pack()
> 
> 
> 
> def update():
>      print('updating')
>      st.see(tkinter.END)
>      tk.after(1000, update)
> 
> 
> input('hit enter to start')
> update()
> f = open('/etc/services')
> 
> for line in f:
>    st.insert(tkinter.END, line + '\n')
>    print('got it')
>    #time.sleep(5)
>    input('more?')
> 
> input('finished?')
> 
> 
> 
> 
> When I do this (input('more?'), it works as expected. If I comment that
> line out, then the program reads the entire file, then update the window
> right at the end, even if I put a sleep in there. What can I do inside the
> loop to give tk a chance?

Have update() (renamed to read_more() in my code) do the reading:

import sys
import tkinter
import tkinter.scrolledtext

root = tkinter.Tk()

text_window = tkinter.Toplevel()
text = tkinter.scrolledtext.ScrolledText(text_window)
text.pack()

infile = open(sys.argv[1])

def read_more():
    line = next(infile, None)
    if line is not None:
        text.insert(tkinter.END, line)
        root.after(100, read_more)
    else:
        text.insert(tkinter.END, "\nThat's all folks", "looney")
        text.tag_configure("looney", foreground="RED")
    text.see(tkinter.END)

read_more()
root.mainloop()

[toc] | [prev] | [next] | [standalone]


#19369

Fromwoooee <woooee@gmail.com>
Date2012-01-24 09:50 -0800
Message-ID<27f5c74f-83b1-4f2e-b5be-eca42bfaedb7@t2g2000yqk.googlegroups.com>
In reply to#19335
if line is not None: probably does not work the way you expect.  You
might try
if line.strip():
Take a look at this quick example

test_lines = ["Number 1\n", "\n", ""]
for ctr, line in enumerate(test_lines):
    print ctr, line
    if line is not None:
         print "     not None"

[toc] | [prev] | [next] | [standalone]


#19486

FromPeter Otten <__peter__@web.de>
Date2012-01-26 14:16 +0100
Message-ID<mailman.5123.1327583788.27778.python-list@python.org>
In reply to#19369
woooee wrote:

[Peter Otten]
>>     line = next(infile, None)
>>     if line is not None:

> if line is not None: probably does not work the way you expect.  

It does what I expect.

> You might try
> if line.strip():
> Take a look at this quick example
> 
> test_lines = ["Number 1\n", "\n", ""]
> for ctr, line in enumerate(test_lines):
>     print ctr, line
>     if line is not None:
>          print "     not None"

Modify your example to

>>> test_lines = ["Number 1\n", "\n", ""]
>>> test_lines = iter(test_lines)
>>> while True:
...     line = next(test_lines, None)
...     if line is None:
...             print "we're done"
...             break
...     print repr(line)
...
'Number 1\n'
'\n'
''
we're done

and be enlightened ;)

[toc] | [prev] | [next] | [standalone]


#19523

Fromyves@zioup.com
Date2012-01-27 07:02 -0700
Message-ID<D7yUq.460$0C4.417@newsfe05.iad>
In reply to#19335
On 2012-01-24 02:52, Peter Otten wrote:
>
> Have update() (renamed to read_more() in my code) do the reading:
>
> import sys
> import tkinter
> import tkinter.scrolledtext
>
> root = tkinter.Tk()
>
> text_window = tkinter.Toplevel()
> text = tkinter.scrolledtext.ScrolledText(text_window)
> text.pack()
>
> infile = open(sys.argv[1])
>
> def read_more():
>      line = next(infile, None)
>      if line is not None:
>          text.insert(tkinter.END, line)
>          root.after(100, read_more)
>      else:
>          text.insert(tkinter.END, "\nThat's all folks", "looney")
>          text.tag_configure("looney", foreground="RED")
>      text.see(tkinter.END)
>
> read_more()
> root.mainloop()
>
>

Thank you, this was very useful!

-- 
Yves.                                                  http://www.SollerS.ca/
                                                        http://ipv6.SollerS.ca
                                                        http://blog.zioup.org/

[toc] | [prev] | [next] | [standalone]


#19588

Fromyves@zioup.com
Date2012-01-29 10:34 -0700
Message-ID<hqfVq.4796$ak.4341@newsfe19.iad>
In reply to#19523
In case somebody else is trying to do the same thing, this is what I ended up 
with to get the concept, that I can now integrate in other scripts:

http://projects.zioup.org/scratchpad/python/tkrun.py

-- 
Yves.                                                  http://www.SollerS.ca/
                                                        http://ipv6.SollerS.ca
                                                        http://blog.zioup.org/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web