Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter & Canvas
#1
Hello, everyone!

I'm trying to create several tk.Canvas() for each ttk.Frame() to display different figures and images on my TabWidget (ttk.Notebook).
But When i try to create two instance of tk.Canvas() on two different ttk.Frame(), NOTHING displays at all!!!

You can use any png. They don't displays!

I tried it to do smth like this:

import tkinter as tk
from tkinter import ttk

class Guess():
    def __init__(self, parent, canvas):
        # Creating Frame
        # super().__init__(parent)
        self.window = parent

        # Creating Canvas for frame Guess:
        # self.canvas = tk.Canvas(self.window, width=1600, height=1000)
        self.canvas = canvas
        self.canvas.pack()

        ###-------------------- Using canvas here.......
        # Add image file
        bg = tk.PhotoImage(file="background1.png")
        # Display image
        self.canvas.create_image(0, 0, image=bg, anchor="nw")


class Full():
    def __init__(self, parent, canvas):
        # Creating Frame
        # super().__init__(parent)
        self.window = parent

        # Creating Canvas for frame Full:
        # self.canvas = tk.Canvas(self.window, width=1600, height=1000)
        self.canvas = canvas
        self.canvas.pack()

        ###-------------------- Using canvas here.......
        # Add image file
        bg = tk.PhotoImage(file="background2.png")
        # Display image
        self.canvas.create_image(0, 0, image=bg, anchor="nw")


class Main:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("Analysis")
        self.window.geometry('1600x1000')

        # create a notebook
        notebook = ttk.Notebook(self.window)

        # Create and add tabs
        frame_guess = ttk.Frame(notebook, width=1600, height=1000)
        frame_note_full = ttk.Frame(notebook, width=1600, height=1000)
        frame_note_shorted = ttk.Frame(notebook, width=1600, height=1000)

        # Create Canvas for frames
        frame_guess_canvas = tk.Canvas(frame_guess, width=1600, height=1000)
        frame_note_canvas = tk.Canvas(frame_note_full, width=1600, height=1000)

        # Add ttk.Frame to ttk.Notebook
        notebook.add(frame_guess, text="Guess")
        notebook.add(frame_note_full, text="Note_Full")
        notebook.add(frame_note_shorted, text="Note_Shorted")

        Guess(frame_guess, frame_guess_canvas)
        Full(frame_note_full, frame_note_canvas)

        notebook.pack(padx=5, pady=5, fill="both", expand=True)

        # frame_guess.pack(fill="both", expand=True)
        # frame_note_full.pack(fill="both", expand=True)
        # frame_note_shorted.pack(fill="both", expand=True)
        # tab1 = ttk.Frame(notebook, width=1600, height=800)

        # Connecting Destructor
        self.window.protocol('WM_DELETE_WINDOW', self.__dell__)

        # Add image file
        # bg = tk.PhotoImage(file="background.png")
        # Display image
        # self.canvas.create_image(0, 0, image=bg, anchor="nw")

        # Launch mainwindow
        self.window.mainloop()

    def __dell__(self):
        # Destroy mainwindow
        self.window.destroy()


Main()
Gribouillis write Mar-23-2024, 12:24 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Tkinter & Canvas - by Peter_Emp1 - Mar-23-2024, 01:11 PM
RE: Tkinter & Canvas - by menator01 - Mar-23-2024, 09:41 PM
RE: Tkinter & Canvas - by deanhystad - Mar-24-2024, 02:42 AM
RE: Tkinter & Canvas - by Peter_Emp1 - Mar-24-2024, 07:13 PM
RE: Tkinter & Canvas - by Pedroski55 - Mar-24-2024, 06:25 PM
RE: Tkinter & Canvas - by deanhystad - Mar-24-2024, 07:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to positon a tkinter canvas Tyrel 3 1,998 Dec-11-2020, 03:05 PM
Last Post: deanhystad
  use classes in tkinter canvas fardin 2 2,718 Jan-06-2019, 04:23 AM
Last Post: fardin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020