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
#1
Bug 
Hi Guys,

I need your help Angel


This Stuff is working well:
import tkinter as tk
from tkinter import *
from tkinter import ttk, filedialog

## window
    win = Tk()  # Create an instance of tkinter frame
    win.title('       F04    universal GUI     ') #set title of window
    myscreenwidth= win.winfo_screenwidth() #get screenwidth
    myscreenheight= win.winfo_screenheight() #get screenheight
    win.geometry("%dx%d+0+0" %(myscreenwidth,myscreenheight)) #set window size to full screen[b][/b]
But now i want to create a new class for my frames ( -> same attributes (like background color) for all instances), which does not really work:

#define a notebook
    notebook1=ttk.Notebook(win) #initialise
    notebook1.pack(fill="both", expand=1) #most simple placement for only 1 object



    class CustomFrames(tk.Frame): 
        def __init__(self,notebook1):
            super().__init__(notebook1)
            super().notebook1.add(self.frame, text="Start   ")            
    
frame01Start=CustomFrames(notebook1) #create instance
Error:
super().notebook1.add(self.frame, text="Start ") AttributeError: 'super' object has no attribute 'notebook1'
Any ideas whats wrong here?





I also found out that this (below) works well, but i find it not an elegant style to create an object "Frame01.frame" , and also I would like to understand the issue about my original code...
    class CustomFrames:
        def __init__(self):
            self.frame=tk.Frame(notebook1)
Thank you for your help! Shy
Reply
#2
Finally I found a solution:

   class CustomFrames(tk.Frame):
        def __init__(self,parent):
            super().__init__(parent)
            #self.parent.add(self.frame, text="Start   ")
            notebook1.add(self, text="Start   ")  # add a tab to the notebook


    frame01Start=CustomFrames(notebook1) 
Reply
#3
Just a small change to make a much better better solution:
class CustomFrames(tk.Frame):
     def __init__(self, parent, name, *args, **kwargs):
         super().__init__(parent, *args, **kwargs)
         parent.add(self, text=name)
 
 frame01Start=CustomFrames(notebook1, "Start")
Your frame class should not have to know about frame01Start and the tab name should not be limited to "Start". These should be passed as arguments to the __init__() method.

*args and **kwargs allow passing additional arguments to Frame.__init__(). CustomFrames doesn't have to know anything about these arguments, it just passes them along to the Frame.__init__() method.
ThomasFab likes this post
Reply
#4
(Sep-27-2022, 03:49 PM)deanhystad Wrote: Just a small change to make a much better better solution:
class CustomFrames(tk.Frame):
     def __init__(self, parent, name, *args, **kwargs):
         super().__init__(parent, *args, **kwargs)
         parent.add(self, text=name)
 
 frame01Start=CustomFrames(notebook1, "Start")
Your frame class should not have to know about frame01Start and the tab name should not be limited to "Start". These should be passed as arguments to the __init__() method.

*args and **kwargs allow passing additional arguments to Frame.__init__(). CustomFrames doesn't have to know anything about these arguments, it just passes them along to the Frame.__init__() method.


perfect. Thank you!
Reply
#5
Correction:

[quote="deanhystad" pid='162134' dateline='1664293788']
Just a small change to make a much better better solution:
[python]class CustomFrames(tk.Frame):
def __init__(self, parent, name, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
parent.add(self, text=name)
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class function does not create command button Heyjoe 2 2,223 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  Using a class to create instances of Tkinter Toplevel() windows nortski 2 10,872 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