Move tkinter label using button command
Question: I am trying to recreate the boardgame monopoly using python and tkinter. I
know how to place a label on a canvas or a frame, but how should I do this
command is being run from another function in the class? I tried it using some
function within the class Board, but then the error rises that the label,
canvas, etc. are not defined as this happens in `__init__(self,parent)`. How
can I solve these errors? Or should I take a different approach to this? Hope
I made my problem clear.

    
    
    import tkFileDialog
    from random import randint
    
    class Board(Frame):
        def __init__(self,parent):
    
    
            ##create the board
            frame = Frame(parent)
            frame.pack()
            Frame.__init__(self,parent)
    
            frame2 = Frame(frame)
            frame2.pack()
    
            c=Canvas(frame2,width=480,height=480)
            c.pack(expand=YES,fill=BOTH)
            c.background=PhotoImage(file='Board.gif')
            c.create_image(0,0,image=c.background,anchor='nw')
    
            ##Add player 1
            player1=PhotoImage(file='plane.gif')
            label_player1 = Label(c,image=player1)
            label_player1.image=player1
            label_player1.place(x=430,y=420)
    
            ##Add player 2
            player2=PhotoImage(file='car.gif')
            label_player2 = Label(c,image=player2)
            label_player2.image=player2
            label_player2.place(x=430,y=450)
    
            button = Button(frame, text="Next turn", command=self.next_turn)
            button.pack()
    
            button = Button(frame, text="Roll the dice", command=self.roll)
            button.pack()
    
        def roll(self):
            number=randint(2,12)
            if b==0:
                self.place_player_down()
            return number
    
        def place_player_down(self):
    
            for i in range(number+1):
                h=int(430-i*30)
                while h>=0:
                    player2=PhotoImage(file='car.gif')
                    label_player2 = Label(c,image=player2)
                    label_player2.image=player2
                    label_player2.place(x=h,y=420)
    
    root = Tk()
    board = Board(root)
    board.pack()
    root.mainloop()
    


Answer: The approach is correct (wrap your Tkinter widgets in a class with the event
handler functions as methods), but you forgot to set the widgets as attributes
of the class using the reference to `self`:

    
    
    class Board(Frame):
        def __init__(self,parent):
            # ...
            self.c = Canvas(frame2,width=480,height=480)
            self.c.pack(expand=YES,fill=BOTH)
            # ...
    
        def place_player_down(self):
            # Use 'self.c', not just 'c'
    

I think you want to do something similar with the value `number`, but it that
case I would send it as an argument to `place_player_down`:

    
    
    def roll(self):
        number=randint(2,12)
        if b==0:
            self.place_player_down(number)
        return number  # Keep in mind that this value is returned but not used anymore
    
    def place_player_down(self, number):
        # Use 'number'
    

