trying python tkinter creating widgets with classes (frame)

Question:

I am trying to create custom widgets with classes. I have one class, called CustomWidgets inside it, I have method for creating Frame object, then configure its side key with config method, but it gives an error.

code;

from tkinter import *
from tkinter import ttk

class CustomWidgets():
    def __init__(self):
        self.root=Tk()
        self.frm_upp=self.frm()
        self.frm_upp.config(side='top')

    def frm(self):
        self.frm_0=LabelFrame(master=self.root)
        self.frm_0.pack(expand='YES')

error;

    self.frm_upp=self.frm().config(side='top')
AttributeError: 'NoneType' object has no attribute 'config'

I guess, when I assign self.frm_upp variable to method self.frm() it does not assign and thus gives Nonetype object.

Asked By: xlmaster

||

Answers:

Your function self.frm returns None since you don’t explicitly set a return value. If you want to be able to use the return value of the function, you must use the return statement.

However, you have another problem in that side isn’t an attribute of a frame widget. It’s an attribute of the pack command, so you’ll need to modify the line that calls config to instead call pack_configure:

class CustomWidgets():
    def __init__(self):
        self.root=Tk()
        self.frm_upp=self.frm()
        self.frm_upp.pack_configure(side='top')

    def frm(self):
        self.frm_0=LabelFrame(master=self.root)
        self.frm_0.pack(expand='YES')
        return self.frm_0
Answered By: Bryan Oakley
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.