Python Forum
[Tkinter] Can´t create a class for frames
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Can´t create a class for frames
#6
I don't understand the "Correction". It looks like *args and **kwargs are crossed out.

You want to pass *args and **kwargs as arguments to super().__init__(). This lets you pass arguments that are understood by Frame.

In this example I use **kwargs to set the background color for the frame. I also show you you can subclass classes that you make, not just tkinter classes. I wrote a custom class for each page I add to the notebook.
import tkinter as tk
import tkinter.ttk as ttk

class NotebookPage(tk.Frame):
    def __init__(self, parent, name, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        parent.add(self, text=name)

class Page1(NotebookPage):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, "Page 1", *args, **kwargs)
        tk.Label(self, text="This is page 1", bg=self["bg"]).pack(padx=10, pady=10)

class Page2(NotebookPage):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, "Page 2", *args, **kwargs)
        tk.Label(self, text="This is page 2", bg=self["bg"]).pack(padx=10, pady=10)

class MyWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        notebook = ttk.Notebook(self)
        notebook.pack(padx=5, pady=5)
        Page1(notebook, bg="yellow")
        Page2(notebook, bg="light blue")

MyWindow().mainloop()
Reply


Messages In This Thread
Can´t create a class for frames - by ThomasFab - Sep-27-2022, 12:30 PM
RE: Can´t create a class for frames - by ThomasFab - Sep-27-2022, 01:50 PM
RE: Can´t create a class for frames - by ThomasFab - Sep-28-2022, 05:46 AM
RE: Can´t create a class for frames - by ThomasFab - Sep-28-2022, 09:02 AM
RE: Can´t create a class for frames - by deanhystad - Sep-28-2022, 08:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Class function does not create command button Heyjoe 2 2,295 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  Using a class to create instances of Tkinter Toplevel() windows nortski 2 11,017 Mar-27-2018, 11:44 AM
Last Post: nortski

Forum Jump:

User Panel Messages

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