Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter & Canvas
#2
Are you trying to do something like this?
import tkinter as tk 
from tkinter import ttk 


class Container(tk.Canvas):
    ''' Container creates an image on a canvas '''
    def __init__(self, parent, img, x, y, *args, side='left', **kwargs):
        super().__init__()

        # Create the canvas
        self.canvas = tk.Canvas(parent, width=100, height=100)
        self.canvas.pack(side=side, fill='x')

        # Create the image
        bg = tk.PhotoImage(file=img)
        bg.bak = bg 
        self.canvas.create_image(x, y, image=bg)



class Window:
    def __init__(self, parent):

        # Create the notebook
        notebook = ttk.Notebook(parent)
        notebook.pack(expand=True, fill='both')

        # Create three frames
        frame1 = ttk.Frame(notebook, width=400, height=400)
        frame1.pack()

        frame2 = ttk.Frame(notebook, width=400, height=400)
        frame2.pack()

        frame3 = ttk.Frame(notebook, width=400, height=400)
        frame3.pack()

        # Create the image instances using the Container class
        img1 = Container(frame1, 'On Off/green.png', 50, 50, side='top')
        img2 = Container(frame2, 'On Off/red.png', 50, 50, side='top')

        img4 = Container(frame3, 'On Off/green.png', 50, 50, side='top')
        img5 = Container(frame3, 'On Off/red.png', 50, 50, side='top')

        # Add tabs to notebook
        notebook.add(frame1, text='Frame 1')
        notebook.add(frame2, text='Frame 2')
        notebook.add(frame3, text='Frame 3')


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('1600x1000+400+200')
    Window(root)
    root.mainloop()
I would probably use labels for the image unless, there was a need for the canvas.

Attached Files

Thumbnail(s)
       
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


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,990 Dec-11-2020, 03:05 PM
Last Post: deanhystad
  use classes in tkinter canvas fardin 2 2,685 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