python tkinter 打印不同图形的函数
时间: 2023-06-26 17:08:32 浏览: 85
以下是使用Python Tkinter库打印不同图形的函数示例:
1. 打印矩形
```python
from tkinter import *
def draw_rect(canvas, x1, y1, x2, y2):
canvas.create_rectangle(x1, y1, x2, y2)
root = Tk()
canvas = Canvas(root, width=300, height=300)
canvas.pack()
draw_rect(canvas, 50, 50, 150, 100) # 调用函数画矩形
root.mainloop()
```
2. 打印圆形
```python
from tkinter import *
def draw_circle(canvas, x, y, r):
canvas.create_oval(x-r, y-r, x+r, y+r)
root = Tk()
canvas = Canvas(root, width=300, height=300)
canvas.pack()
draw_circle(canvas, 150, 150, 50) # 调用函数画圆形
root.mainloop()
```
3. 打印直线
```python
from tkinter import *
def draw_line(canvas, x1, y1, x2, y2):
canvas.create_line(x1, y1, x2, y2)
root = Tk()
canvas = Canvas(root, width=300, height=300)
canvas.pack()
draw_line(canvas, 50, 50, 150, 150) # 调用函数画直线
root.mainloop()
```
4. 打印文本
```python
from tkinter import *
def draw_text(canvas, x, y, text):
canvas.create_text(x, y, text=text)
root = Tk()
canvas = Canvas(root, width=300, height=300)
canvas.pack()
draw_text(canvas, 150, 150, "Hello, world!") # 调用函数打印文本
root.mainloop()
```
你可以根据自己的需要修改这些函数,来打印出你想要的图形。
阅读全文