Tkinter Canvas

  • Post category:Tkinter

In this Tkinter tutorial, you will learn how to set the Tkinter Canvas.

# import Tkinter Module
import tkinter as tk

# create main window
var = tk.Tk()

# Set title
var.title("My Tkinter")

# create a canvas
var2 = tk.Canvas(var, width=200, height=100)

# pack canvas
var2.pack()

# create a line
var2.create_line(0, 0, 200, 100, fill="blue")

# create another line
var2.create_line(0, 100, 200, 0, fill="red")

# create a rectangle
var2.create_rectangle(40, 20, 180, 80, fill="yellow")

# start the event loop
var.mainloop()

Output:

Free resources to learn advanced skills: AiHints and CodeAllow