在Tkinter中,可以通过以下步骤在画布上配置动态创建的矩形:
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack()
def create_rectangle(x, y, width, height, color):
rectangle = canvas.create_rectangle(x, y, x+width, y+height, fill=color)
# 示例调用
create_rectangle(50, 50, 100, 100, "red")
def create_rectangle_on_click(event):
x = event.x
y = event.y
width = 100
height = 100
color = "blue"
create_rectangle(x, y, width, height, color)
canvas.bind("<Button-1>", create_rectangle_on_click)
root.mainloop()
这样,当你在画布上点击鼠标左键时,将会动态创建一个蓝色的矩形。你可以根据需要调整矩形的位置、大小和颜色。
领取专属 10元无门槛券
手把手带您无忧上云