Home > Web Front-end > JS Tutorial > body text

Tkinter Tutorial Canvas Chapter (1)

黄舟
Release: 2017-01-17 10:08:54
Original
1032 people have browsed it

''''Tkinter Tutorial Canvas Chapter (1)'''


# Provides a Container that can be used for drawing and supports basic geometric elements. When using Canvas for drawing, all Operations are all done through Canvas, not through its elements


# Elements can be represented using handle or tag.


'''1. The first Canvas program'''


# -*- coding: cp936 -*-


# Specify the color of the canvas as white

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


# Create a Canvas and set its background color to white


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


# For the sake of clarity, set the background color to white to distinguish the root



##'''2. Create an item'''


# -*- coding: cp936 -*-


# Create a rectangle and specify the canvas color as white


from Tkinter import *


root = Tk()


# Create a Canvas and set its background color to white


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


# Create a rectangle with coordinates (10,10,110,110)


cv.create_rectangle(10,10,110,110)
cv.pack()
root.mainloop()
Copy after login

# For obviousness, set the background color to white , to distinguish the root


'''3. Specify the fill color of the item'''


# -*- coding: cp936 -*-


# Create a rectangle and specify the background color of the canvas to be white


# Use the attribute fill to set its fill color


from Tkinter import *


root = Tk()


# Create a Canvas and set its background color to white


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

# Specify the filling of the rectangle The color is red



'''4. Specify the border color of the item'''


# -*- coding: cp936 -*-


# Create a rectangle and specify the background color of the canvas to be white


# Use the attribute outline to set its border color


from Tkinter import *


root = Tk()


# Create a Canvas and set its background color to white



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

# Specify the border color of the rectangle as red


'''5. Specify the width of the border'''


# -*- coding: cp936 -*-


# Specify the background color of the canvas to be white


# Use the attribute width to specify the width of the line


from Tkinter import *


root = Tk()


# Create a Canvas and set its background color to white


cv = Canvas(root,bg = 'white')
cv.create_rectangle(10,10,110,110,outline = 'red',width = 5)
cv.pack()
root.mainloop()
Copy after login

# Specify the border color of the rectangle is red, set the line width to 5, note that it is different from the width of Canvas.


'''6. Draw a dotted line'''


# -*- coding: cp936 -*-


# Specify canvas The background color is white


# Use attribute dash, this value can only be odd


from Tkinter import *


root = Tk ()


# Create a Canvas and set its background color to white



cv = Canvas(root,bg = 'white')
cv.create_rectangle(10,10,110,110,
outline = 'red',
dash = 10,
fill = 'green')
cv.pack()
root.mainloop()
Copy after login

# Specify the border color of the rectangle to be red, Draw a dotted line


'''7. Use the brush to fill'''


# -*- coding: cp936 -*-


# Specify the background color of the canvas to be white


# Use the attribute stipple



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

# to create a Canvas and set its background color Is white



cv = Canvas(root,bg = 'white')
cv.create_rectangle(10,10,110,110,
outline = 'red',
stipple = 'gray12',
fill = 'green')
cv.pack()
root.mainloop()
Copy after login

# Specify the border color of the rectangle as red, customize the brush



'''8. Modify the coordinates of the item' ''


# -*- coding: cp936 -*-


# Specify the background color of the canvas as white


# Use Canvas Method to reset the coordinates of the item



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

# Create a Canvas and set its background color to white



cv = Canvas(root,bg = 'white')
rt = cv.create_rectangle(10,10,110,110,
outline = 'red',
stipple = 'gray12',
fill = 'green')
cv.pack()
Copy after login

# Reset the coordinates of rt (equivalent to moving an item)



cv.coords(rt,(40,40,80,80))
root.mainloop()
Copy after login
# Dynamically modify the coordinates of the item



The above is the content of the Canvas chapter (1) of the Tkinter tutorial. For more related content, please pay attention to the PHP Chinese website (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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!