-1

Implement a function called create_square that takes three arguments—the x-coordinate and the y-coordinate of the upper-left corner and the length of a side. calling the predefined tkinter function create_rectangle.

import tkinter
def create_square (x: int, y: int, s: int):
    '''Return a square on tkinter given the x-coordinate and
    y-coordinate of the upper-left corner and length of a side'''
    return(create_rectangle(x, y, s))

It comes out as an error, but I don't know how else to do this.

Deer530
  • 73
  • 1
  • 1
  • 10
  • Are you using Python 2 or 3? The parameter list in your function definition is invalid syntax in Python 2 (which does not support parameter annotations). – Dan Lowe Oct 10 '15 at 14:37

1 Answers1

1

Try this:

from tkinter import Tk, Canvas

tk = Tk()
canvas = Canvas(tk, width=500, height=500)
canvas.pack()

def create_square(x1,y1,side):
    x2 = x1 + side
    y2 = y1 + side
    canvas.create_rectangle(x1, y1, x2, y2)

create_square(100, 100, 200)
tk.mainloop()
MrPedru22
  • 1,324
  • 1
  • 13
  • 28
  • It works, thanks! Although, I was wondering why the Python shells gives me an integer output before it draws the square? For example, when I typed create_square(200, 200, 300), the shell gives me an output of 1 before the square is displayed on the tkinter window. – Deer530 Oct 10 '15 at 15:35
  • Whenever you use a create_ function from the canvas, such as create_rectangle in this case, an identifier is returned, it identifies the object you created. If you keep typing create_square in the shell it will give you a different identifier everytime you run it: 1,2,3,etc. This identifying number can be useful if stored in a variable so that you can track your objects. – MrPedru22 Oct 10 '15 at 16:15
  • For example: `square = create_square(100,100,200)` now you can tell this square to do anything you want because you've stored in a variable. Let's say you want the square to move in the canvas, you can type `canvas.move(square,5,0)` which moves the square in the x-axis. – MrPedru22 Oct 10 '15 at 16:18