-1

I'm currently working on a Python Tkinter code which is getting longer and longer. I wanted to implement using multiple classes for some of the frames in my GUI.

Below code is a sample of what I am trying to do. Basically making a class which has a frame, and the frame is using another class as a parent.

But when I run the code, I get this error "'MainProgram' object has no attribute 'FirstFrame'"

Any solutions? I tried searching but I wasn't successful on finding something like this.

import tkinter as tk
from tkinter import ttk

class MainProgram():
    def __init__(self):
        self.mainwin = tk.Tk()
        self.FirstFrame()        


class FirstFrame():
    def __init__(self):
        self.firstframe = ttk.LabelFrame(self.MainProgram.mainwin, text="hi")
        self.firstframe.grid(column=0, row=0)

if __name__ == "__main__":
    main = MainProgram()
    main.mainwin.mainloop
numersoz
  • 77
  • 1
  • 1
  • 8
  • 1
    FirstFrame is not a method of your class MainProgram, that's why you are getting that error. – diegoiva Apr 08 '18 at 20:45
  • possible duplicate of https://stackoverflow.com/questions/47360640/how-to-create-tkinter-widgets-inside-parent-class-from-sub-class – diegoiva Apr 08 '18 at 20:50

2 Answers2

1

Replace:

self.Firstframe()

With:

self.ff=Firstframe()

- Firstframe() is not an attribute of Mainprogram.

Artemis
  • 2,553
  • 7
  • 21
  • 36
0

Look at the link here for a good starting point for code organization into classes

Switch between two frames in tkinter

the example can be used 'as is' for an organizational strategy or as a very good template to modify a piece at a time and learn of other possible structures where one class can be used to create instances of another.

import tkinter as tk
from tkinter import ttk

class MainProgram():
    def __init__(self):
        self.mainwin = tk.Tk()
        self.my_frame = FirstFrame()
        # examine the child parent relationship
        for child in self.mainwin.winfo_children():
            print(child['text'])        


class FirstFrame():
    def __init__(self):
        self.firstframe = ttk.LabelFrame(text="hi")
        self.firstframe.grid(column=0, row=0)
        ttk.Label(master=self.firstframe,text='a widget inside frame').grid()

if __name__ == "__main__":
    main = MainProgram()
    main.mainwin.mainloop
R Spark
  • 323
  • 1
  • 4
  • 11
  • Thank you! If I want to make the frame in the second class a child of the mainwin, I get an error. How would I be able to fix that? This is the error: "type object 'MainProgram' has no attribute 'mainwin'" – numersoz Apr 10 '18 at 03:11
  • The parent child relationship of widgets can be examined with .winfo_children() method. The example above shows the frame in MainProgram has a child with text=hi. Which seems to be what you are aiming at. It is an example of the changes required to make your original code work. – R Spark Apr 10 '18 at 05:17
  • If I want to have FirstFrame.firstframe to be a child of MainProgram.win, I just can't make it work. In the code you provided, they are also not arranged to show how this would work. Can you help with this, please? – numersoz Apr 12 '18 at 01:12