- 编程
tkinter画布的综合使用
- 2023-12-10 16:54:22 @
# 使用属性tags设置图形对象标记的例子。
from tkinter import *
root = Tk()
# 创建一个Canvas,设置其背景色为白色
cv = Canvas(root, bg = 'white', width = 200, height = 200)
# 使用tags指定给第一个矩形指定3个tag
rt = cv.create_rectangle(10,10,150,50, tags = ('r1','r2','r3'))
cv.pack()
cv.create_rectangle(20,20,80,80, tags = 'r3')
#使用tags指定给第2个矩形指定1个tag
# 将所有与tag('r3')绑定的item边框颜色设置为蓝色
for item in cv.find_withtag('r1'):
cv.itemconfig(item,outline = 'blue')
root.mainloop()
#使用create_ arc ()方法创建圆弧的例子。
from tkinter import *
root = Tk()
# 创建一个Canvas,设置其背景色为白色
cv = Canvas(root,bg = 'red')
cv.create_arc((10,10,110,110),)# 使用默认参数创建一个圆弧,结果为90度的扇形
d = {1:PIESLICE,2:CHORD,3:ARC}
for i in d:
# 使用三种样式,分别创建了扇形、弓形和弧形
cv.create_arc((10,10 + 60*i,110,110 + 60*i),style = d[i])
print (i,d[i])
#使用start/extent指定圆弧起始角度与偏移角度
cv.create_arc(
(150,150 ,250,250),
start = 10, #指定起始角度
extent = 120 #指定角度偏移量(逆时针)
)
cv.pack()
root.mainloop()
#使用create_line()方法创建线条对象的例子。
from tkinter import *
root = Tk()
cv = Canvas(root, bg = 'white', width = 200, height = 100)
cv.create_line(10, 10, 100, 10, arrow='none') #绘制没有箭头线段
cv.create_line(10, 20, 100, 20, arrow='first') #绘制起点有箭头线段
cv.create_line(10, 30, 100, 30, arrow='last') #绘制终点有箭头线段
cv.create_line(10, 40, 100, 40, arrow='both') #绘制两端有箭头线段
cv. create_line(10,50,100,100,width=3, dash=7) #绘制虚线
cv.pack()
root.mainloop()
from tkinter import *
root = Tk()
# 创建一个Canvas,设置其背景色为白色
cv = Canvas(root, bg = 'white', width = 200, height = 100)
cv.create_rectangle(10,10,110,110, width =2,fill = 'red') #指定矩形的填充色为红色, 宽度为2
cv.create_rectangle(120, 20,180, 80, outline = 'green') #指定矩形的边框颜色为绿色
cv.pack()
root.mainloop()
from tkinter import *
root = Tk()
cv = Canvas(root, bg = 'white', width = 300, height = 100)
cv.create_polygon (35,10,10,60,60,60, outline = 'blue', fill = 'red', width=2) #等腰三角形
cv.create_polygon (70,10,120,10,120,60, outline = 'blue', fill = 'white', width=2) #直角三角形
cv.create_polygon (130,10,180,10,180,60, 130,60, width=4) #黑色填充正方形
cv.create_polygon (190,10,240,10,190,60, 240,60, width=1) #对顶三角形
cv.pack()
root.mainloop()
from tkinter import *
root = Tk()
cv = Canvas(root, bg = 'white', width = 200, height = 100)
cv.create_oval (10,10,100,50, outline = 'blue', fill = 'red', width=2) #椭圆
cv.create_oval (100,10,190,100, outline = 'blue', fill = 'red', width=2) #圆形
cv.pack()
root.mainloop()
#创建文本的例子。
from tkinter import *
root = Tk()
cv = Canvas(root, bg = 'white', width = 200, height = 100)
cv.create_text((10,10), text = 'Hello Python', fill = 'red', anchor='nw')
cv.create_text((200,50), text = '你好,Python', fill = 'blue', anchor='se')
cv.pack()
root.mainloop()
#选中文本的例子。。
from tkinter import *
root = Tk()
cv = Canvas(root, bg = 'white', width = 200, height = 100)
txt = cv.create_text((10,10), text = '中原工学院计算机学院', fill = 'red', anchor='nw')
#设置文本的选中起始位置
cv.select_from(txt,5)
#设置文本的选中结束位置
cv.select_to(txt,9) #选中“计算机学院”
cv.pack()
root.mainloop()
#绘制图像示例
from tkinter import *
root = Tk()
cv = Canvas(root) #r'images\s1.gif'
img1 = PhotoImage(file = 'images\\s1.gif') #笑脸
img2 = PhotoImage(file = 'images\\2-1.gif') #方块A
img3 = PhotoImage(file = 'images\\3-1.gif') #梅花A
cv.create_image((0,0),image=img1) #绘制笑脸
cv.create_image((100,100),image=img2) #绘制方块A
cv.create_image((300,100),image=img3) #绘制梅花A
d = {1:'error',2:'info',3:'question',4:'hourglass',5:'questhead',
6:'warning',7:'gray12',8:'gray25',9:'gray50',10:'gray75'}#字典
#cv.create_bitmap((10,220),bitmap = d[1])
#以下遍历字典绘制Python内置的位图
for i in d:
cv.create_bitmap((20*i,20),bitmap = d[i])
cv.pack()
root.mainloop()
from tkinter import *
root = Tk()
cv = Canvas(root)
img1 = PhotoImage(file = 'C:\\aa.png') #笑脸
img2 = PhotoImage(file = 'C:\\2.gif') #方块A
img3 = PhotoImage(file = 'C:\\3.gif') #梅花A
rt1=cv.create_image((100,100),image=img1) #绘制笑脸
rt2=cv.create_image((200,100),image=img2) #绘制方块A
rt3=cv.create_image((300,100),image=img3) #绘制梅花A
# 重新设置方块A(rt2对象)的坐标
cv.coords(rt2,(200,50)) #调整rt2对象方块A位置
rt4= cv.create_rectangle(20,140,110,220,outline='red', fill='green') #正方形对象
cv.coords(rt4,(100,150,300,200)) #调整rt4对象位置
cv.pack()
root.mainloop()
0 条评论
目前还没有评论...