[Tutor] What am I doing wrong here in Tkinter?

Gregor Lingl glingl at aon.at
Thu Oct 30 20:24:18 EST 2003



Alfred Milgrom schrieb:

> Hi:
>
> I am using Tkinter in Windows, but I am obviously doing something 
> wrong in my code.

I think you are doing nothing wrong - except that you are not calling 
mainloop()
which is correct if you are working with IDLE (version < 1.0).

If you run your program from the commandline you will observe: no window 
appears,
but getccords outputs 0 twice. So this are the values before the window 
manager manages
to paint your window.
After a certain delay (or something, which causes painting of your 
window) you get
the values of the painted window.

Try the following experiment: changhe your program like this:

from Tkinter import *
from time import sleep

class GUI (Frame):
    top = Tk()

    def __init__(self, parent=top):
        global cols
        Frame.__init__(self,parent)
        self.master.title('Test of winfo_rootx()')
        self.buildUI()

    def buildUI(self):
        self.goBoard = Canvas(self, width=400 , height=400 , border = 2, 
bg="#FFE14B")
        self.goBoard.pack(side=TOP, padx=10, pady=10)

        fButtons = Frame(self)
        self.bQuit = Button(fButtons, text="get coords", 
command=self.getcoords)   ### <=== bind getccords
        self.bQuit.pack(side=LEFT, anchor=W, padx=10, pady=2)
        fButtons.pack(side=BOTTOM, fill=X)
        self.pack()

    def getcoords(self):
        print "winfo_rootx() says %s %s starts at %d" 
%(self.top.winfo_name(), self.top.winfo_id(), self.top.winfo_rootx()) 
       ###  a = self
        print "But now it says %s %s starts at %d" 
%(self.top.winfo_name(), self.top.winfo_id(), self.top.winfo_rootx())

board = GUI()
board.getcoords()
mainloop()                   ### <======

and start it from the commandline. Observe the output, push the button 
and observe the output again.
Here the first time board.getcoords() is called  before mainloop() is 
invoked, i.e. before the window is painted.
The next time - when you push the butten - the window is already here ;-)
(You will get the same results when working with IDLE 1.0 under Python 2.3)

When working from IDLE (old version, Python 2.2 or less) IDLE's own 
mainloop governs all events
(especially painting events) - IDLE's own events and your app's events. 
So it's hard to predict
when what happens in your application.

HTH
Gregor




>
> The following code creates a TKinter window and then tries to get the 
> x,y coordinates using winfo_rootx(), etc.
> When I run the code, I get correct answers for winfo_name()  and 
> winfo_id() but not for winfo_rootx() (it always returns 0!)
>
> This incorrect behaviour disappears if I insert a line to say "a = 
> self" (see code below)
> This seems to say to me that I have not set something correctly in the 
> program, but I can't see what.
>
> Running the following code gives me the following output:
>
> winfo_rootx() says tk 3808 starts at 0
> But now it says tk 3808 starts at 112
>
> Can someone point me in the right direction?
> Thanks in advance,
> Fred Milgrom
>
> #--------------------------------------------------------------------------- 
>
>
> # Test of winfo_rootx()
>
> from Tkinter import *
>
> class GUI (Frame):
>     top = Tk()
>
>     def __init__(self, parent=top):
>         global cols
>         Frame.__init__(self,parent)
>         self.master.title('Test of winfo_rootx()')
>         self.buildUI()
>
>     def buildUI(self):
>         self.goBoard = Canvas(self, width=400 , height=400 , border = 
> 2, bg="#FFE14B")
>         self.goBoard.pack(side=TOP, padx=10, pady=10)
>
>         fButtons = Frame(self)
>         self.bQuit = Button(fButtons, text="Quit", 
> command=self.top.destroy)
>         self.bQuit.pack(side=LEFT, anchor=W, padx=10, pady=2)
>         fButtons.pack(side=BOTTOM, fill=X)
>         self.pack()
>
>     def getcoords(self):
>         print "winfo_rootx() says %s %s starts at %d" 
> %(self.top.winfo_name(), self.top.winfo_id(), self.top.winfo_rootx())
>         a = self
>         print "But now it says %s %s starts at %d" 
> %(self.top.winfo_name(), self.top.winfo_id(), self.top.winfo_rootx())
>
> board = GUI()
> board.getcoords()
>
> #--------------------------------------------------------------------------- 
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>




More information about the Tutor mailing list