How to show multiple Canvases at the same time in Tkinter?


The Canvas widget is one of the versatile widgets in Tkinter which is used to create illustrations, draw shapes, arcs, images, and other complex layouts in an application. To create a Canvas widget, you'll need to create a constructor of canvas(root, **options).

You can use the factory functions to create text, images, arcs and define other shapes in the canvas. In some cases, if you want to create another canvas using the same canvas to keep the application workflow consistent, then you can create a button to call an event that creates another canvas.

To understand this, let us create a canvas and a button to open another canvas to update the primary canvas widget.

Example

# Import required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter window
win = Tk()
win.geometry("700x350")

# Create an instance of style class
style=ttk.Style(win)

def open_new_win():
   top=Toplevel(win)
   canvas1=Canvas(canvas, height=180, width=100, bg="#aaaffe")
   canvas1.pack()
   Label(canvas1, text="You can modify this text", font='Helvetica 18 bold').pack()

# Create a canvas widget
canvas=Canvas(win, height=400, width=300)
canvas.pack()

# Create a button widget
button=ttk.Button(canvas, text="Open Window", command=open_new_win)
button.pack(pady=30)

win.mainloop()

Output

Running the above code will display a window with a button to open another canvas window.

When you click the button, it will display a message on the primary canvas window.

Updated on: 22-Dec-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements