tkinter库绘图
简时刻 人气:0一、椭圆绘制
实例代码:
import tkinter as tk # 导入tkinter库,并重命名为tk from tkinter import messagebox # 导入messagebox模块 mywindow = tk.Tk() # 创建一个窗体 mywindow.title("绘制椭圆") # 设置窗体的标题 # 创建画布并布局 mycanvas = tk.Canvas(mywindow,width=350,height=350,bg="white") mycanvas.pack() # 绘制椭圆 myoval = mycanvas.create_oval(10,20,340,300) # create_oval()方法绘制椭圆 # 设置椭圆边框的宽度和颜色 myova2 = mycanvas.create_oval(30,50,320,280,width=5) myova3 = mycanvas.create_oval(50,70,320,260,width=5,outline="red") # 设置椭圆的边框是否是虚线 myova4 = mycanvas.create_oval(70,90,300,240,width=8,outline="red",dash=(5,3)) # 设置椭圆的填充色 myova5 = mycanvas.create_oval(100,120,260,200,width=8,outline="blue",dash=(5,3),fill="purple") # 绘制圆 myova6 = mycanvas.create_oval(140,140,180,180,width=5,outline="red") mywindow.mainloop() # 显示画布
运行结果:
二、矩形绘制
实例代码:
import tkinter as tk # 导入tkinter库,并重命名为tk from tkinter import messagebox # 导入messagebox模块 mywindow = tk.Tk() # 创建一个窗体 mywindow.title("绘制矩形") # 设置窗体的标题 # 创建画布并布局 mycanvas = tk.Canvas(mywindow, width=350, height=350, bg="purple") mycanvas.pack() # 绘制矩形 a = 10 # 定义两个整型变量 b = 340 # 利用for循环结制多个矩形 for i in range(a, b, 20): mycanvas.create_rectangle(a+i, a+i, b-i, b-i) # .create_rectangle()方法可以绘制矩形 mywindow.mainloop() # 显示画布
运行结果:
三、多边形绘制
实例代码:
import tkinter as tk # 导入tkinter库,并重命名为tk mywindow = tk.Tk() # 创建一个窗体 mywindow.title("绘制多边形") # 设置窗体的标题 # 创建画布并布局 mycanvas = tk.Canvas(mywindow, width=250, height=250, bg="white") mycanvas.pack() # 绘制多边形 mycanvas.create_polygon(80,80,80,180,130,220,180,180,180,80,130,40, fill="purple",width=5,outline="red") mywindow.mainloop() # 显示画布
运行结果:
加载全部内容