Miguel Soler Vicedo
Miguel Soler Vicedo

Reputation: 43

Inserting a tkinter object inside another object created by a dictionary on Python 3.x

I can't assign a root based on an object created by a dictionary. I write down my code to make it simply. The problem comes up at the last row, where I want to set the button in the "Listado" frame. The button always appears at the bottom of the main App widget.

class App (tk.Tk):
def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    tk.Tk.iconbitmap(self, default="codisalogo.ico")
    tk.Tk.wm_title(self, "Codisa")
    tk.Tk.configure(self, background="#ffffff")
    tk.Tk.attributes(self, "-alpha", 1.0)
    w, h = tk.Tk.winfo_screenwidth(self), tk.Tk.winfo_screenheight(self)
    tk.Tk.geometry(self, "%dx%d+100+50"%(w/2, h/2))
    tk.Tk.wm_minsize(self, width=w//2, height=h//2) 

    container = ttk.Frame(self)
    container.pack(side="top", fill="both", expand=True)   
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)

    self.frames = {}
    for F in (PageOne, PageTwo):
        frame = F(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column =0, sticky="nsew")
    self.show_frame(PageOne)

def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class PageOne(tk.Frame):
def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.objects = {"listado": tk.Frame(self, background="#fffaf0",  height=400, width=200, relief="raised").grid(row=1, column=2, columnspan=1, rowspan=2, padx=5),
                   "empresa": ttk.LabelFrame(self, text = "Empresa", height =200, width=200,relief= "sunken").grid(row=1, column=0, columnspan=1, padx=5),
                   "visitas": ttk.LabelFrame(self, text = "Visitas", height =200, width=200, relief= "groove").grid(row=1, column=1, columnspan=1, padx=5),
                   "historico": ttk.LabelFrame(self, text = "Histórico", height =200, width=410, relief= "raised").grid(row=2, column=0, columnspan=2, padx=5)}

    self.objectslistado = {"empresa": ttk.Button(self.objects["listado"], text="Hola").pack()}

Thank you all"

Upvotes: 1

Views: 369

Answers (1)

Jason S
Jason S

Reputation: 119

The problem is that in your dictionary, you are mapping the strings "listado", "empresa", etc. to the result of the .grid() calls, which is None. This causes the Hola button to have a parent of None, which I believe defaults to the root window. You can see this if you add a print statement like so:

self.objects = {"listado": tk.Frame(self, background="#fffaf0",  height=400, width=200, relief="raised").grid(row=1, column=2, columnspan=1, rowspan=2, padx=5),
               "empresa": ttk.LabelFrame(self, text = "Empresa", height =200, width=200,relief= "sunken").grid(row=1, column=0, columnspan=1, padx=5),
               "visitas": ttk.LabelFrame(self, text = "Visitas", height =200, width=200, relief= "groove").grid(row=1, column=1, columnspan=1, padx=5),
               "historico": ttk.LabelFrame(self, text = "Histórico", height =200, width=410, relief= "raised").grid(row=2, column=0, columnspan=2, padx=5)}
print self.objects
self.objectslistado = {"empresa": ttk.Button(self.objects["listado"], text="Hola").pack()}

You should instead move the grid statements outside and do something along the lines of this (it might not be exactly what you want but this fixes the issue you're having):

self.objects = {"listado": tk.Frame(self, background="#fffaf0",  height=400, width=200, relief="raised"),
                   "empresa": ttk.LabelFrame(self, text = "Empresa", height =200, width=200,relief= "sunken"),
                   "visitas": ttk.LabelFrame(self, text = "Visitas", height =200, width=200, relief= "groove"),
                   "historico": ttk.LabelFrame(self, text = "Historico", height =200, width=410, relief= "raised")}
    self.objects["listado"].grid(row=1, column=2, columnspan=1, rowspan=2, padx=5)
    self.objects["empresa"].grid(row=1, column=0, columnspan=1, padx=5)
    self.objects["visitas"].grid(row=1, column=1, columnspan=1, padx=5)
    self.objects["historico"].grid(row=2, column=0, columnspan=2, padx=5)
    self.objectslistado = {"empresa": ttk.Button(self.objects["listado"], text="Hola").pack()}

Upvotes: 2

Related Questions