Woody_99
Posts: 7
Joined: Sat Nov 11, 2017 4:18 pm

Tkinter in while loop

Tue Nov 14, 2017 7:58 pm

Hello,
i am writing a GUI programm with Tkinter. I dont want to use the tk.mainloop(), course it is in an While loop. I want to use tk.update_idletasks() or tk.update(). but when i try it, I get the folloing message:
Traceback (most recent call last):
File "C:\Users\XXX\AppData\Local\Programs\Python\Python36-32\Programme\Hello World.py", line 51, in <module>
tk.update()
AttributeError: module 'tkinter' has no attribute 'update'
What can i do?

Thanks, Woody

User avatar
paddyg
Posts: 2005
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Tkinter in while loop

Tue Nov 14, 2017 8:31 pm

Maybe there's some useful info in the code you run prior to your error message. On the RPi3 if I startx, then a terminal, then python3 I can

Code: Select all

pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tk = tkinter.Tk()
>>> tk.update()
>>> 
and it seems to work ok
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
Paeryn
Posts: 1673
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Tkinter in while loop

Tue Nov 14, 2017 10:45 pm

Woody_99 wrote:
Tue Nov 14, 2017 7:58 pm

Code: Select all

    tk.update()
AttributeError: module 'tkinter' has no attribute 'update'
From the error message it looks like tk is the name you gave when you imported tkinter rather than the name of the top-level widget, e.g. you have something like

Code: Select all

import tkinter as tk

tkroot = tk.Tk()

tk.update()  ## Should be tkroot.update()
She who travels light — forgot something.

Davies
Posts: 140
Joined: Sat Apr 04, 2015 4:24 pm

Re: Tkinter in while loop

Wed Nov 15, 2017 12:51 am

in your other post you use root,

Code: Select all

import tkinter as tk
        root=tk.Tk()
        root.attributes('-fullscreen', True)
        root.wm_title("Glashaus") # Fenster Titel
        root.config(background = "#FFFFFF") # Hintergrundfarbe des Fenster
    root.mainloop()
call root what you want but youll need to call the same name throughout, or dont call a name and have everything referred to as tk as in paddy's example
Last edited by Davies on Wed Nov 15, 2017 12:56 am, edited 1 time in total.

scotty101
Posts: 2337
Joined: Fri Jun 08, 2012 6:03 pm

Re: Tkinter in while loop

Wed Nov 15, 2017 9:29 am

Woody_99 wrote:
Tue Nov 14, 2017 7:58 pm
i am writing a GUI programm with Tkinter. I dont want to use the tk.mainloop(), course it is in an While loop. I want to use tk.update_idletasks() or tk.update(). but when i try it, I get the folloing message:
REALLY BAD IDEA IMO.
I can't think of any reason why you wouldn't want to use mainloop() or why the existing methods of periodically scheduling function calls wouldn't be adequate for your needs.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

B.Goode
Posts: 4630
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Tkinter in while loop

Wed Nov 15, 2017 12:36 pm

scotty101 wrote:
Wed Nov 15, 2017 9:29 am
Woody_99 wrote:
Tue Nov 14, 2017 7:58 pm
i am writing a GUI programm with Tkinter. I dont want to use the tk.mainloop(), course it is in an While loop. I want to use tk.update_idletasks() or tk.update(). but when i try it, I get the folloing message:
REALLY BAD IDEA IMO.
I can't think of any reason why you wouldn't want to use mainloop() or why the existing methods of periodically scheduling function calls wouldn't be adequate for your needs.

Probably my fault...

In this duplicate thread viewtopic.php?f=32&t=197690 I suggested that putting mainloop() in a while: loop might Not Be A Good Idea.

Clearly there are two broad options for resolving that. @Woody_99 has opted for the one that is less appropriate...

User avatar
paddyg
Posts: 2005
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Tkinter in while loop

Wed Nov 15, 2017 4:05 pm

In pi3d there was a decision (before I was involved so don't know all details) that it would be nice to allow tkinter to supply the graphical drawing surface as well as mouse and keyboard stuff. Because this was optional, the main pi3d display loop called tk.update() if required. However in the demo using Qt you will see that the main loop in controlled by the GUI app and the pi3d.Display.loop_running() call is done from a timer. However with the PyGTK version of the same demo I struggled to get this mechanism to work and had to call gtk functions from the pi3d loop. Horses for courses I suppose, but better to use the tkinter looping until you hit a major problem.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”

Who is online

Users browsing this forum: DirkS, KyluAce and 16 guests