YouTip LogoYouTip

Python Tk Canvas

# Python2.x Python Tkinter Canvas [![Image 4: Python GUI Programming](#) Python GUI Programming](#) The Python Tkinter Canvas component is similar to the canvas in HTML5, both are used for drawing. You can place graphics, text, widgets, or frames on the canvas. ### Syntax The syntax format is as follows: w = Canvas ( master, option=value, ... ) * master: The parent container of the button. * options: Optional parameters, i.e., the settable properties of the button. These options can be set in the form key = value, separated by commas. | No. | Option & Description | | --- | --- | | 1 | **bd** Border width in pixels, default is 2 pixels. | | 2 | **bg** Background color | | 3 | **confine** If true (default), the canvas cannot be scrolled beyond the scrollable region. | | 4 | **cursor** Cursor shape setting, such as arrow, circle, cross, plus, etc. | | 5 | **height** Height | | 6 | **highlightcolor** Color to highlight | | 7 | **relief** Border style, optional values are FLAT, SUNKEN, RAISED, GROOVE, RIDGE. Default is FLAT. | | 8 | **scrollregion** A tuple (w, n, e, s) that defines the maximum scrollable area of the canvas, where w is left, n is top, e is right, s is bottom. | | 9 | **width** The size of the canvas on the X-axis. | | 10 | **xscrollincrement** The amount of horizontal scrolling for scroll requests. | | 11 | **xscrollcommand** Horizontal scrollbar. If the canvas is scrollable, this attribute is the .set() method of the horizontal scrollbar. | | 12 | **yscrollincrement** Similar to xscrollincrement, but for the vertical direction. | | 13 | **yscrollcommand** Vertical scrollbar. If the canvas is scrollable, this attribute is the .set() method of the vertical scrollbar. | **The Canvas component supports the following standard options:** arc βˆ’ Creates a sector coord = 10, 50, 240, 210 arc = canvas.create_arc(coord, start=0, extent=150, fill="blue") image βˆ’ Creates an image filename = PhotoImage(file = "sunshine.gif") image = canvas.create_image(50, 50, anchor=NE, image=filename) line βˆ’ Creates a line line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options) oval βˆ’ Creates a circle oval = canvas.create_oval(x0, y0, x1, y1, options) polygon βˆ’ Creates a polygon with at least three vertices oval = canvas.create_polygon(x0, y0, x1, y1,...xn, yn, options) ### Example The example shows a message displayed when a button is clicked: ## Example #!/usr/bin/python# -*- coding: UTF-8 -*-import Tkinter import tkMessageBox# -*- coding: cp936 -*-# Create a rectangle, set the canvas color to white from Tkinter import * root = Tk()# Create a Canvas, 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 clarity, set the background color to white to distinguish from root top.mainloop() The test output is as follows: !(#) [![Image 6: Python GUI Programming](#) Python GUI Programming](#)
← Python Mongodb Insert DocumentBootstrap4 Makeawebsite β†’