For example: import tkinter as tkĬanvas.create_oval(*points, fill= 'purple') To draw an oval, you use the create_oval() method. For example: import tkinter as tkĬanvas.create_rectangle(( 100, 100), ( 300, 300), fill= 'green') To draw a rectangle, you use the create_rectangle() method. And the fill argument specifies the color of the line. The width argument specifies the width of the line. The create_line() method connects the dots between these points. In this example, a line consists of two points (50,50) and (100,100). For example, the following creates a red line: canvas.create_line(( 50, 50), ( 100, 100), width= 4, fill= 'red') Code language: Python ( python ) To create a line, you use the create_line() method. These methods allow you to place items on it. Adding items to a canvas using create_* methodsĪ canvas object has a number of add_* methods. The direction of the x-axis is from left to right and the direction of the y-axis is from top to bottom. The origin (0,0) is at the top-left corner. canvas.pack(anchor=tk.CENTER, expand= True) Code language: Python ( python )Ī canvas has a coordinate system like a window. Second, place the canvas object on the root window using the pack() geometry. Root.mainloop() Code language: Python ( python )įirst, create a new Canvas object with the width 600px, height 400px and background white: canvas = tk.Canvas(root, width= 600, height= 400, bg= 'white') Code language: Python ( python ) For example, the following creates a canvas on a window: import tkinter as tkĬanvas = tk.Canvas(root, width= 600, height= 400, bg= 'white')Ĭanvas.pack(anchor=tk.CENTER, expand= True) To create a canvas widget, you create a new instance of the Canvas class from the tkinter module. The canvas widget is a blank area on which you can draw figures, create text, and place images. The Canvas widget allows you to build anything from custom widgets to complete user interfaces. The canvas widget is the most flexible widget in Tkinter. Introduction to the Tkinter canvas widget Summary: in this tutorial, you’ll learn about the Tkinter Canvas widget and how to draw various objects on it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |