create_rectangle参数详解
时间: 2023-08-18 20:03:52 浏览: 50
在Python中,`create_rectangle`是Tkinter库中的一个函数,用于在Canvas画布上创建矩形。其语法如下:
```python
create_rectangle(x1, y1, x2, y2, **options)
```
其中,`x1`和`y1`是矩形左上角的坐标,`x2`和`y2`是矩形右下角的坐标。`**options`是可选参数,可以包括以下选项:
- `fill`:矩形内部的颜色
- `outline`:矩形边框的颜色
- `width`:矩形边框的宽度
- `tags`:给矩形打上一个标签,方便后续操作
示例代码:
```python
from tkinter import *
root = Tk()
canvas = Canvas(root, width=200, height=200)
canvas.pack()
# 创建一个红色的矩形,左上角坐标为(50, 50),右下角坐标为(150, 150)
canvas.create_rectangle(50, 50, 150, 150, fill='red')
root.mainloop()
```
相关问题
python canvas.create_rectangle位置参数
在Python的tkinter模块中,canvas.create_rectangle()函数的位置参数是四个数字,分别表示矩形左上角和右下角的坐标。具体来说,这四个数字依次是左上角的x坐标、左上角的y坐标、右下角的x坐标、右下角的y坐标。例如下面的代码创建了一个左上角坐标为(10, 10),右下角坐标为(50, 50)的矩形:
```
from tkinter import *
root = Tk()
canvas = Canvas(root, width=200, height=200)
canvas.pack()
canvas.create_rectangle(10, 10, 50, 50, fill='red')
root.mainloop()
```
create_rectangle属性
`create_rectangle` 是 Tkinter 中的一个方法,用于在 Canvas(画布) 上绘制矩形。
它的语法如下:
```python
create_rectangle(x1, y1, x2, y2, **kwargs)
```
其中,`x1` 和 `y1` 是矩形的左上角坐标,`x2` 和 `y2` 是矩形的右下角坐标。`kwargs` 是一系列可选的参数,用于控制矩形的样式、颜色等。
例如,以下代码创建了一个红色的矩形:
```python
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=200, height=200)
canvas.pack()
canvas.create_rectangle(50, 50, 150, 150, fill="red")
root.mainloop()
```
运行后,你会看到一个红色的正方形矩形。