Tkinter 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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Tkinter,apopularGUItoolkitforPython,offersaplethoraoftoolstodesignintuitiveandinteractiveinterfaces,amongthese,thePlace_forget()methodstandsoutasapowerfultoolfordynamicGUIlayoutmanipulation.Thismethodenablesdeveloperstoeffortlesslyhideorremovewidgets

The canvas arrow plug-ins include: 1. Fabric.js, which has a simple and easy-to-use API and can create custom arrow effects; 2. Konva.js, which provides the function of drawing arrows and can create various arrow styles; 3. Pixi.js , which provides rich graphics processing functions and can achieve various arrow effects; 4. Two.js, which can easily create and control arrow styles and animations; 5. Arrow.js, which can create various arrow effects; 6. Rough .js, you can create hand-drawn arrows, etc.

A brief introduction to python GUI programming GUI (Graphical User Interface, graphical user interface) is a way that allows users to interact with computers graphically. GUI programming refers to the use of programming languages to create graphical user interfaces. Python is a popular programming language that provides a rich GUI library, making Python GUI programming very simple. Introduction to Python GUI library There are many GUI libraries in Python, the most commonly used of which are: Tkinter: Tkinter is the GUI library that comes with the Python standard library. It is simple and easy to use, but has limited functions. PyQt: PyQt is a cross-platform GUI library with powerful functions.

The details of the canvas clock include clock appearance, tick marks, digital clock, hour, minute and second hands, center point, animation effects, other styles, etc. Detailed introduction: 1. Clock appearance, you can use Canvas to draw a circular dial as the appearance of the clock, and you can set the size, color, border and other styles of the dial; 2. Scale lines, draw scale lines on the dial to represent hours or minutes. Position; 3. Digital clock, you can draw a digital clock on the dial to indicate the current hour and minute; 4. Hour hand, minute hand, second hand, etc.

The versions of html2canvas include html2canvas v0.x, html2canvas v1.x, etc. Detailed introduction: 1. html2canvas v0.x, which is an early version of html2canvas. The latest stable version is v0.5.0-alpha1. It is a mature version that has been widely used and verified in many projects; 2. html2canvas v1.x, this is a new version of html2canvas.

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

How to use canvas to draw charts and animation effects in uniapp requires specific code examples 1. Introduction With the popularity of mobile devices, more and more applications need to display various charts and animation effects on the mobile terminal. As a cross-platform development framework based on Vue.js, uniapp provides the ability to use canvas to draw charts and animation effects. This article will introduce how uniapp uses canvas to achieve chart and animation effects, and give specific code examples. 2. canvas

The tkinter canvas attributes include bg, bd, relief, width, height, cursor, highlightbackground, highlightcolor, highlightthickness, insertbackground, insertwidth, selectbackground, selectforeground, xscrollcommand attributes, etc. Detailed introduction
