Home > Web Front-end > H5 Tutorial > Tkinter Tutorial Canvas Chapter (4)

Tkinter Tutorial Canvas Chapter (4)

黄舟
Release: 2017-01-17 10:18:24
Original
1495 people have browsed it

''''Tkinter Tutorial Canvas Chapter (4)'''


'''22. Draw an arc'''


# -*- coding: cp936 -*-


# Create an ARC

from Tkinter import *
root = Tk()
Copy after login

# Create a Canvas and set its background color to white

cv = Canvas(root,bg = 'white')
cv.create_arc((10,10,110,110),)
cv.pack()
root.mainloop()
Copy after login


# Create an ARC using default parameters, the result is a 90-degree sector


'''23. Set the arc style'''


# -*- coding: cp936 -*-


# Create create_arc.


from Tkinter import *
root = Tk()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


# Create a Canvas , set its background color to white


cv = Canvas(root,bg = 'white')
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],
cv.pack()
root.mainloop()
Copy after login


# Use three styles to create fan, bow and arc shapes respectively


'''24. Set the angle of the arc'''


# -*- coding: cp936 -*-


# Use start/extent to specify the starting angle Create a Canvas with the offset angle


from Tkinter import *
root = Tk()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

# and set its background color to white


cv = Canvas(root,bg = 'white')
d = {1:PIESLICE,2:CHORD,3:ARC}
for i in d:
cv.create_arc(
(10,10 + 60*i,110,110 + 60*i),
Copy after login

style = d [i], #Specify style


start = 30, #Specify starting angle


extent = 30 #Specify angle offset


)


cv.pack()


root.mainloop()


# Use three styles, start specifies the start Angle; extent specifies angle offset


'''25. Draw bitmap'''


# -*- coding: cp936 -*-


# Use bitmap to create a bitmap create_bitmap


from Tkinter import *


root = Tk()


# Create A Canvas, set its background color to white

cv = Canvas(root,bg = 'white')
d = {1:'error',2:'info',3:'question',4:'hourglass'}
for i in d:
cv.create_bitmap((20*i,20*i),bitmap = d[i])
cv.pack()
root.mainloop()
Copy after login


# Use the bitmap attribute to specify the name of the bitmap. The first parameter of this function is a point (x, y ) specifies the upper left position of the bitmap storage location.


'''26. Draw GIF image'''


# -*- coding: cp936 -*-


# Create gif image create_image


from Tkinter import *


root = Tk()


# Create a Canvas and set its background color to white

cv = Canvas(root,bg = 'white')
img = PhotoImage(file = 'c:/python.gif')
cv.create_image((150,150),image = img)
cv.pack()
root.mainloop()
Copy after login


# First use PhotoImage to create a GIF image, and then set the image attribute to the newly created img


'''27. Draw Line'''


# -*- coding: cp936 -*-


# Create a straight line with an arrow create_line


from Tkinter import *


root = Tk()


# Create a Canvas and set its background color to white

cv = Canvas(root,bg = 'white')
d = [(0,'none'),(1,'first'),(2,'last'),(3,'both')]
for i in d:
cv.create_line(
(10,10 + i[0]*20,110,110+ i[0] * 20), # 设置直线的起始、终点
arrow = i[1], # 设置直线是否使用箭头
arrowshape = '40 40 10' # 设置箭头的形状(填充长度,箭头长度,箭头宽度
)
cv.pack()
root.mainloop()
Copy after login


# Use the arrow attribute to control whether to display arrows


'''28. The joinstyle attribute of a straight line'''


# -*- coding: cp936 -* -


# Create a straight line, use the joinstyle attribute


from Tkinter import *


root = Tk()


# Create a Canvas and set its background color to white


cv = Canvas(root,bg = 'white')
d = [(0,'none','bevel'),(1,'first','miter'),(2,'last','round'),(3,'both','round')]
for i in d:
cv.create_line(
(10,10 + i[0]*20,110,110+ i[0] * 20), # 设置直线的起始、终点
arrow = i[1], # 设置直线是否使用箭头
arrowshape = '8 10 3', # 设置箭头的形状(填充长度,箭头长度,箭头宽度
joinstyle = i[2],
)
cv.pack()
root.mainloop()
Copy after login


# Set the joinstyle attribute of the straight line to bevel/miter/round respectively, and test it Effect.


'''29. Draw an ellipse'''


# -*- coding: cp936 -*-


# Draw an ellipse , use the create_oval attribute


from Tkinter import *


root = Tk()


# to create a Canvas and set its background color to White


cv = Canvas(root,bg = 'white')


# Create an ellipse with a length of 200 and a width of 100

cv.create_oval((10,10,210,110),fill = 'red')
cv.pack()
root.mainloop()
Copy after login

# specifies the length and width of the ellipse, a circle is a special case where the length and width are equal.


'''30. Create a polygon'''


# -*- coding: cp936 -*-


# Create a polygon (Triangle)


from Tkinter import *


root = Tk()


# Draw a Canvas and set its background color to white


cv = Canvas(root,bg = 'white')


# Create a right triangle


cv.create_polygon((10,10,10,200,100,200),fill = 'red')
cv.pack()
root.mainloop()
Copy after login

# Specify the coordinates of three points. The coordinates of the three points must meet the definition of a triangle.


'''31. Modify graphics'''


# -*- coding: cp936 -*-


# Create polygons create_ploygon (triangle)


from Tkinter import *


root = Tk()


# Create a Canvas and set its background color to White


cv = Canvas(root,bg = 'white')


# Create a right triangle

cv.create_polygon((10,10,10,200,100,200),
#smooth = True, # 平滑处理,但未找到控制此参数的项
splinesteps = 0, # 不明白是控制什么的???
)
cv.pack()
root.mainloop()
Copy after login


# Smooth/splinesteps is used to modify the drawn graphics. I don’t know what other functions these two parameters have.


'''32. Draw text'''


# -*- coding: cp936 -*-


# Use text create_text

from Tkinter import *
root = Tk()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

# Create a Canvas and set its background color to white


cv = Canvas(root,bg = 'white')


# Create a text object, the default setting is center alignment


cv.create_text((10,10),text = 'Hello Text',
anchor = W
)
cv.pack()
root.mainloop()
Copy after login

# Use anchor to control the position of the text, and use justify to control the alignment


'''33. Select text'''


# -*- coding: cp936 -*-


# Use text create_text

from Tkinter import *
root = Tk()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


# 创建一个Canvas,设置其背景色为白色


cv = Canvas(root,bg = 'white')


# 创建一个文字对象,默认设置为居中对齐

txt = cv.create_text((10,10),text = 'Hello Text',
anchor = W
)
Copy after login


# 设置文本的选中起始位置


cv.select_from(txt,2)


# 设置文本的选中结束位置

cv.select_to(txt,5)
cv.pack()
root.mainloop()
Copy after login

# 使用anchor控制文字的位置,使用justify控制对齐方式


'''34.创建组件'''


# -*- coding: cp936 -*-


# 使用子组件create_window

from Tkinter import *
root = Tk()
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

# 创建一个Canvas,设置其背景色为白色


cv = Canvas(root,bg = 'white')


# 创建一个Button对象,默认设置为居中对齐


def printWindow():
print 'window'
bt = Button(cv,text = 'ClickMe',command = printWindow)
Copy after login


#修改button在canvas上的对齐方式


cv.create_window((10,10),window = bt,anchor = W)


# 新创建的line对象与button有重叠


cv.create_line(10,10,20,20)


# 新创建的line不在button之上,即没有重叠


cv.create_line(30,30,100,100)
cv.pack()
root.mainloop()
Copy after login


# 使用anchor组件在Canvas上的位置,默认情况下为居中对齐,这样使用后其它的item将不能再使用button战胜的那块区域

以上就是Tkinter教程之Canvas篇(4)的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template