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

Alfred Milgrom fredm at smartypantsco.com
Thu Oct 30 19:44:26 EST 2003


Hi:

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

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()

#---------------------------------------------------------------------------





More information about the Tutor mailing list