search
HomeWeb Front-endH5 TutorialTkinter Tutorial Canvas Chapter (4)

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


'''22. Draw an arc'''


# -*- coding: cp936 -*-


# Create an ARC

from Tkinter import *
root = Tk()

# 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()


# 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()


# 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()


# 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()

# 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),

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()


# 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()


# 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()


# 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()


# 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()

# 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()

# 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()


# 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()

# 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()

# 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()


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


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


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

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


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


cv.select_from(txt,2)


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

cv.select_to(txt,5)
cv.pack()
root.mainloop()

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


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


# -*- coding: cp936 -*-


# 使用子组件create_window

from Tkinter import *
root = Tk()

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


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


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


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


#修改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()


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

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


Statement
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
Understanding H5: The Meaning and SignificanceUnderstanding H5: The Meaning and SignificanceMay 11, 2025 am 12:19 AM

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

H5: Accessibility and Web Standards ComplianceH5: Accessibility and Web Standards ComplianceMay 10, 2025 am 12:21 AM

Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

H5 vs. HTML5: Clarifying the Terminology and RelationshipH5 vs. HTML5: Clarifying the Terminology and RelationshipMay 05, 2025 am 12:02 AM

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

HTML5 Features: The Core of H5HTML5 Features: The Core of H5May 04, 2025 am 12:05 AM

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor