Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

In Python, I am currently studying Tkinter and I'm trying to make a game that requires creating shapes onto a canvas. For example, I want a red box to appear over my canvas picture. When I execute my code, the box you see is about 1 pixel in dimension, and I'm not certain why and how it got like that. Here's my code:

from tkinter import *

root = Tk()

root.geometry("500x900")

canvas = Canvas(root, width=550, height=820)

canvas.pack()

png = PhotoImage(file = r'example.png') # Just an example

canvas.create_image(0, 0, image = png, anchor = "nw")

a = canvas.create_rectangle(50, 0, 50, 0, fill='red')

canvas.move(a, 20, 20)

1 Answer

0 votes
by (108k points)

Please be informed that your create_rectangle function is taking four parameters:

canvas.create_rectangle(x1, y1, x2, y2, **kwargs)

With (x1,y1) the data points of the top left corner and (x2, y2) as the data points of the bottom right corner. But you gave twice the same data points so your box has a zero width and height, that's why you can only see a pixel. You can try with canvas.create_rectangle(50, 0, 100, 50, fill='red') and this will display a square of side 50 pixels.

Are you interested to learn Python in detail? Sign up for this perfect Python Training course by Intellipaat.

Related questions

+9 votes
1 answer
0 votes
1 answer
0 votes
5 answers

Browse Categories

...