1 year ago

#228217

test-img

nemunas

How do you get a second canvas to appear on a tkinter Toplevel window when inside a class?

I'm using Tkinter to create an application, and with this Dashboard class, I'm trying to get a pop out window to show another canvas, so I can use create_image and tag_bind on the pop out. The result I'm currently getting is that the second canvas appears over the first canvas instead of in the pop out window.

import tkinter as tk
from tkinter import *

class Dashboard(tk.Tk):
    """
    Configures, and displays the Dashboard
    """

    def __init__(self):
        tk.Tk.__init__(self)
        self.config(width=1440, height=1024)

        canvas = tk.Canvas(self, bg="#343333", height=1024, width=1440, bd=0, highlightthickness=0, relief="ridge")
        canvas.place(x=0, y=0)

        # Captures the background image for the canvas
        image_path = "dashboard_background.png"
        self.background_img = tk.PhotoImage(file=image_path)
        canvas.create_image(0, 0, anchor='nw', image=self.background_img)

        def logoutbuttonClicker():
            pop = Toplevel(self)
            pop.geometry('537x273')
            pop.config(height=273, width=537)

            logout_canvas = tk.Canvas(canvas, bg="#ffffff", height=273, width=537, bd=0, highlightthickness=0, relief="ridge")
            logout_canvas.place(x=0, y=0)

            self.logout_background_img = PhotoImage(file=f"logout_background.png")
            logout_canvas.create_image(268.5, 136.5, anchor='nw', image=self.logout_background_img)
            
            self.logout_yes_img = PhotoImage(file=f"logout_yes.png")
            self.logout_no_img = PhotoImage(file=f"logout_no.png")

        logout_image_path = "dashboard_logout.png"
        self.logout_image = tk.PhotoImage(file=logout_image_path)
        logoutButton = canvas.create_image(45, 950, anchor='nw', image=self.logout_image)
        canvas.tag_bind(logoutButton, "<ButtonRelease-1>", lambda event: logoutbuttonClicker())

def main():
    app = Dashboard()
    app.mainloop()

if __name__ == '__main__':
    main()

This is how it currently appears in the UI

python

class

tkinter

canvas

toplevel

0 Answers

Your Answer

Accepted video resources