Search code examples
pythonfunctiontkinterdefinitionsquare

Function Definition: Calling create_rectangle to create square


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.


Solution

  • 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()