I was trying to fix something related to what an object returns when you use an object as an argument.
For example, I have a custom object and I want to use it as a parent for another tkinter widget, so I need to return a tkinter object to place the new tkinter object into my custom object, but my custom object returns that is an object of a custom class.
I can explain this much better on code:
class CustomFrame(object):
def __init__(self,**args):
#code that works
#The next is an external object that have a variable that I need to call externally.
#The variable is "interior"
self.externalobject = anotherobject('random')
cfr1 = CustomFrame()
label1 = Label(cfr1)
Now I want to use the "self.externalobject.interior" as the parent for label1 But I want to make it friendly just calling "cfr1" instead of "self.externalobject.interior"
I know that if I use the call method and I return the value that I need it will work if I pass "cfr1" as a function, but I want to make it the more Pythonic as possible.
So I need to know if there's another special method or something to modify what it returns.
EDIT: So, this is part of the code I am using:
This is the code of the vertical scrolled frame (not my code).
class VerticalScrolledFrame(Frame):
"""A pure Tkinter scrollable frame that actually works!
* Use the 'interior' attribute to place widgets inside the scrollable frame
* Construct and pack/place/grid normally
* This frame only allows vertical scrolling
"""
def __init__(self, parent, bg, *args, **kw):
Frame.__init__(self, parent, *args, **kw)
# create a canvas object and a vertical scrollbar for scrolling it
vscrollbar = Scrollbar(self, orient=VERTICAL)
canvas = Canvas(self, bd=0, highlightthickness=0,
yscrollcommand=vscrollbar.set,bg=bg)
vscrollbar.config(command=canvas.yview)
canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
# reset the view
canvas.xview_moveto(0)
canvas.yview_moveto(0)
# create a frame inside the canvas which will be scrolled with it
self.interior = interior = Frame(canvas,bg=bg)
interior_id = canvas.create_window(0, 0, window=interior,
anchor=NW)
a = Frame(self.interior,height=10,bg=dynamicBackground())
a.pack()
def canvasscroll(event):
canvas.yview('scroll',int(-1*(event.delta/120)), "units")
def _configure_canvas(event):
a.configure(height=10)
a.update()
mylist = interior.winfo_children()
for i in mylist:
lasty=i.winfo_height()+i.winfo_y()
a.configure(height=lasty)
if interior.winfo_reqwidth() != canvas.winfo_width():
# update the inner frame's width to fill the canvas
canvas.itemconfigure(interior_id, width=canvas.winfo_width())
if canvas.winfo_height()<lasty:
vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
canvas.config(scrollregion=(0,0,0,lasty))
canvas.bind_all("<MouseWheel>", canvasscroll)
else:
canvas.unbind_all("<MouseWheel>")
try:
vscrollbar.pack_forget()
except:
pass
canvas.config(scrollregion=(0,0,0,0))
canvas.bind('<Configure>', _configure_canvas)
And this is the code for my custom object:
class UtilityPanel(object):
def __init__(self,parent='*',title='Main Title',state='normal',bg='red'):
super().__init__()
if parent != '*':
self.parent=parent
else:
raise TypeError('You must specify a parent for this widget.')
self.title=title
global subpanels
if len(subpanels) == 0:
self.panelid = 'sp-1'
subpanels.append('sp-1')
else:
self.panelid = 'sp-'+str(int(subpanels[-1].split('-')[1])+1)
subpanels.append(self.panelid)
self.panel = VerticalScrolledFrame(self.parent,bg=bg,width=600,height=200,name=self.panelid)
def __call__(self,name):
return(self.panel.interior)
def pack(self):
self.panel.place(x=300)
global activepanel
activepanel = self.panelid
So, if i pass the argument like label1 = Label(cfr1.panel.interior)
it works, but I want it to make it work just passing cfr1 as an argument.