0

I'm trying to make application with Notebook Tabs, but each tab should be described separately as class. For start I gave 2 classnames for 2 tabs as Frame1 and Frame2, but I want to give sensible names. Here is the code that works:

from tkinter import *
from tkinter import ttk

root = Tk()
root.geometry('400x400')
root.title('Title')

notebook = ttk.Notebook(root)

class Frame1(Frame):
    def __init__(self, container):
        super().__init__(container)
        self.Frame1 = Frame(container)
        self.Frame1.config(bg='blue')
        self.Frame1.place(x=0, y=24, relwidth=0.9, relheight=0.9)


class Frame2(Frame):
    def __init__(self, container):
        super().__init__(container)
        self.Frame2 = Frame(container)
        self.Frame2.config(height=200, width=203, bg= 'green')
        self.Frame2.place(x=0, y=24)

Frame1 = Frame1(notebook)
notebook.add(Frame1, text = "Connection")

Frame2 = Frame2(notebook)
notebook.add(Frame2, text = "Transient Response")

notebook.place(x=10, y=10)

root.mainloop()

Result is on screenshot - 2 tabs with blue and green filling. I want to give sensible names. As soon as I change class name, e.g. Frame2 for Frame3, picture spoils (see screenshot).

3
  • 1
    Your screenshot didn't get attached, so I'm not sure what "spoils" is supposed to mean, but there are some obvious problems with this code. In particular, your Frame1 object (for example) is a Frame, but it also creates an entirely separate Frame named self.Frame1. The former is what you actually add to your notebook, but the latter is what you're setting the color of. You either need to get rid of this extra Frame completely, or make it contained by the actual Frame in the notebook (Frame(self) instead of Frame(container)). Dec 2, 2021 at 14:31
  • 1
    I don't see screenshots. I don't understand what you want to do. I don't know what makes problem with "sensible names"
    – furas
    Dec 2, 2021 at 16:46
  • BWT: if I would have to use inner frame then I would use the same name self.frame in both classes instead of self.Frame1 and self.Frame2 - and for me this is "sensible name"
    – furas
    Dec 2, 2021 at 16:56

1 Answer 1

0

You should not be creating a frame inside your frame classes. Instances of Frame1 and Frame2 are already frames. And if you do add a frame inside of Frame1 and Frame2, they need to be children of self, not container.

Also, you're using the name Frame1 both for the name of the class and the instance of the class. You should not do that. You should name the instance with a lowercase first letter according to PEP8.

class Frame1(Frame):
    def __init__(self, container):
        super().__init__(container, bg='blue')

frame1 = Frame1(notebook)

Note that the instance of Frame1 is itself a frame just like any other frame. If you want to configure any of the attributes, you can do so like in my example where I set the background when calling super, but you can also call the configure method on self at any time:

class Frame1(Frame):
    def __init__(self, container):
        super().__init__(container, bg='blue')
        self.configure(width=200, height=200)

You can also configure it outside the class just like you can with any other widget:

frame1 = Frame1(notebook)
...
frame1.configure(background="bisque")
3
  • Thanks for responce! But if I do not create new frames inside frame classes, how can I set frame(tab) size and background colour, preferably different for each frame(tab)?
    – Ivan_
    Dec 2, 2021 at 16:22
  • 1
    @Ivan_ if yo don't create inner frame then you will need self.config(bg='blue') instead of self.Frame1.config(bg='blue'). And self.config(height=200, width=203, bg= 'green') instead of self.Frame2.config(height=200, width=203, bg= 'green')
    – furas
    Dec 2, 2021 at 16:53
  • @Furas, thanks, it works! Just tried.
    – Ivan_
    Dec 2, 2021 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.