title: Why do the x and y coordinates not line up to where your clicking if I put the tkinter object inside a function?
body: <p>I don't understand why this happens, but the x and y coordinates won't properly line up if I put the tkinter object inside a function.</p>
  <p>here's the code</p>
  <pre><code>from tkinter import *
  import random
  
  root = Tk()
  root.title(&quot;Treasure Hunt&quot;)
  root.geometry(&quot;400x300&quot;)
  root.iconbitmap(&quot;treasure2.ico&quot;)
  
  class Treasure_game:
      count = 0
      search = 1
      
      def __init__(self, main, num1, num2):
          self.num1 = num1
          self.num2 = num2
          
          frame = Frame(main)
          frame.pack()
          
          self.treasure_map = Canvas(frame, bg=&quot;#f3c474&quot;, width=200, height=200)
          self.treasure_map.create_rectangle(2, 2, 201, 201)
          
          self.treasure_map.bind(&quot;&lt;Button-1&gt;&quot;, self.game)
          
          self.top_label = Label(frame, text=&quot;Click on the map to find the treasure&quot;)
          
          self.coord_label = Label(frame)
          
          self.parrot_button = Button(frame, text=&quot;Search&quot;, state=DISABLED, command=self.parrot_search)
          
          self.top_label.pack()
          self.treasure_map.pack()
          self.coord_label.pack()
          self.parrot_button.pack()
      
      def game(self, event):
          text = (f&quot;You clicked at x: {event.x} y: {event.y}. You are {abs(event.x-x_coord)} away from x and {abs(event.y-y_coord)} from y.&quot;)
          self.coord_label.config(text=text)
          self.count += 1
          
          if abs(event.x-x_coord) &lt; 5 and abs(event.y-y_coord) &lt; 5:
              self.coord_label.config(text=&quot;You have found the treasure!&quot;)
              self.top_label.config(text=&quot;You have won!!&quot;)
              self.treasure_map.create_line(self.num1-5, self.num2-5, self.num1+5, self.num2+5, fill=&quot;red&quot;, width=2)
              self.treasure_map.create_line(self.num1-5, self.num2+5, self.num1+5, self.num2-5, fill=&quot;red&quot;, width=2)
              print(f&quot;It took {self.count} tries to find the treasure&quot;)
              self.playing = False
              
          if self.count &gt;= 10 and self.search == 1:
              self.parrot_button[&quot;state&quot;] = NORMAL
      
      def parrot_search(self):
          if self.search == 1:
              self.treasure_map.create_oval(self.num1-random.randint(20, 40), self.num2-random.randint(20, 40), self.num1+random.randint(20, 40), self.num2+random.randint(20, 40))
              self.count += 1
              self.search -= 1
          
  
  
  def main_game():
      pass
  
  x_coord = random.randint(3, 200)
  y_coord = random.randint(3, 200)       
  game = Treasure_game(root, x_coord, y_coord) 
   
  main_game()
  root.mainloop()
  </code></pre>
  <p>If I put the x_coord, y_coord and game variables inside the main_game function, the num1 and num2 of the Treasure_game won't line up to where your clicking.</p>