Canvas in Tkinter tutorial (1)

Source: Internet
Author: User
Tags tkinter tutorial
# Provides a iner that can be used for drawing. It supports basic geometric elements. When using Canvas for drawing, all operations are performed through Canvas, canvas (1) ''' not based on its element ''tkinter )'''


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


# Handle or tag can be used for element representation.


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


#-*-Coding: cp936 -*-


# 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')cv.pack()root.mainloop()


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




'''2. Create an item '''


#-*-Coding: cp936 -*-


# Create a rectangle and specify the color of the canvas 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()


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


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


#-*-Coding: cp936 -*-


# Create a rectangle and specify that the background color of the canvas is white.


# Use the property 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()

# Specify the filling color of the rectangle to red


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


#-*-Coding: cp936 -*-


# Create a rectangle and specify that the background color of the canvas is 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()


# The border color of the specified rectangle is red.


'''5. Specify the Border Width '''


#-*-Coding: cp936 -*-


# Specify that the background color of the canvas is white.


# Use the attribute width to specify the line width


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


# Specify the border color of the rectangle as red and set the line width to 5. Note that the width of the rectangle is different from that of the Canvas.


'''6. Draw the dotted line '''


#-*-Coding: cp936 -*-


# Specify that the background color of the canvas is white.


# Use the property dash. The value can only be an odd number.


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


# Specify the border color of the rectangle as red and draw a dotted line


'''7. Fill in with a paint brush '''


#-*-Coding: cp936 -*-


# Specify that the background color of the canvas is white.


# Use Attribute stipple


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',stipple = 'gray12',fill = 'green')cv.pack()root.mainloop()

# Specify the border color of the rectangle as red and customize the image brush


'''8. Modify the coordinates of items '''


#-*-Coding: cp936 -*-


# Specify that the background color of the canvas is white.


# Use the Canvas method to reset the coordinates of items


from Tkinter import *root = Tk()


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

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

cv.coords(rt,(40,40,80,80))root.mainloop()

# Dynamically modify the coordinates of an item


The above is the content of Canvas (1) in the Tkinter tutorial. For more information, see PHP (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.