Change language

Python Tkinter | Moving objects using the Canvas.move () method

Canvas class Tkinter supports functions that are used to move objects from one position to another on any canvas or tkinter level.

Syntax: Canvas.move (canvas_object, x, y)

Parameters:
canvas_object is any valid image or drawing created with the help of Canvas class. To know how to create object using Canvas class take reference of this .
x is horizontal distance from upper-left corner.
y is vertical distance from upper-left corner.

We will use the class to see the move () method work.

Class parameters

Data members used:
master
x
y
canvas
rectangle-method/">rectangle

Member functions used:
movement ()
left ()
right ()
up ()
down ()

Widgets used: Canvas

Tkinter method used:
Canvas.create_rectangle-method/">rectangle ( )
pack ()
Canvas.move ()
after ()
bind ()

Below is the Python implementation:

# imports all forms of tkinter and tkinter files. ttk

from tkinter import *

from tkinter.ttk import *  

 

class GFG:

def __ init __ ( self , master = None ):

self . master = master

  

  # take care of the movement in the x direction

self . x = 1

# take care of y direction

self . y = 0

 

# canvas object for creating the shape

self .canvas = Canvas (master)

  # create a rectangle-method/">rectangle

  self . rectangle-method/">rectangle = self . canvas.create_rectangle-method/">rectangle (

5 , 5 , 25 , 25 , fill = "black" )

self . canvas.pack ()

 

# call the class move method in

 < / code> # move rectangle-method/">rectangle

self . movement ()

 

def movement ( self ):

 

  # The move () method is called here

# This moves the rectangle-method/">rectangle to the x, y coordinates

self .canvas.move ( self . rectangle-method/">rectangle, self . x, self . y) < / p>

 

self . canvas.after ( 100 , self . movement)

  

# to move in negative x direction

def left ( self , event):

print ( event.keysym)

self . x = - 5

self . y = 0

  

  # to move in the positive x direction

def right ( self , event):

print (event.keysym)

self . x = 5

self . y = 0

  

  # for positive y direction

  def up ( self , event):

print (event.keysym)

self . x = 0

self . y = - 5

 

# to move in negative y direction

d ef down ( self , event):

print (event.keysym)

self . x = 0

  self . y = 5

 

if __ name__ = = "__ main__" :

 

# Tk class object responsible for creating

  # Tkinter top-level window

master = Tk ()

gfg = GFG (master)

 

# This will bind the arrow keys in tkinter

# toplevel that will move around the image or picture

master.bind ( ""KeyPress-Left" " , lambda e: gfg.left (e ))

mas ter.bind ( ""KeyPress-Right"" , lambda e: gfg.right (e))

master.bind ( " "KeyPress-Up" " , lambda e: gfg.up (e))

  master.bind ( ""KeyPress-Down"" , lambda e: gfg.down (e))

  

# Endless loop is interrupted only by interruption

mainloop ()

Exit:

Additional print statements are used in the above code to show the correct operation of the move () method. The Keyword keysym (reserved by Tkinter) is used to print which keyboard key is pressed.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically