[Tkinter] Can´t create a class for frames - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Can´t create a class for frames (/thread-38307.html) |
Can´t create a class for frames - ThomasFab - Sep-27-2022 Hi Guys, I need your help 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 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! RE: Can´t create a class for frames - ThomasFab - Sep-27-2022 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) RE: Can´t create a class for frames - deanhystad - Sep-27-2022 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. RE: Can´t create a class for frames - ThomasFab - Sep-28-2022 (Sep-27-2022, 03:49 PM)deanhystad Wrote: Just a small change to make a much better better solution: perfect. Thank you! RE: Can´t create a class for frames - ThomasFab - Sep-28-2022 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) RE: Can´t create a class for frames - deanhystad - Sep-28-2022 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() |