-1

实现一个名为 create_square 的函数,该函数接受三个参数——左上角的 x 坐标和 y 坐标以及边长。调用预定义的 tkinter 函数 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))

它作为一个错误出现,但我不知道该怎么做。

4

1 回答 1

1

尝试这个:

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()
于 2015-10-10T14:46:12.953 回答