Traper279
Traper279

Reputation: 73

Inherit from a Python tkinter Frame screw up pack functionality

Here is code below:

import tkinter as tk

def click(event):
    print (event.widget.winfo_name())

sq=250
win = tk.Tk()

topFrame = tk.Frame(win)
middleFrame = tk.Frame(win)
bottomFrame = tk.Frame(win)

top_leftFrame = tk.Frame(topFrame, height=sq, width=sq, bg="sienna")
top_middleFrame = tk.Frame(topFrame, height=sq, width=sq, bg="chocolate")
top_rightFrame = tk.Frame(topFrame, height=sq, width=sq, bg="peru")

middle_leftFrame = tk.Frame(middleFrame, height=sq, width=sq, bg="sandy brown")
middle_middleFrame = tk.Frame(middleFrame, height=sq, width=sq, bg="salmon")
middle_rightFrame = tk.Frame(middleFrame, height=sq, width=sq, bg="rosy brown")

bottom_leftFrame = tk.Frame(bottomFrame, height=sq, width=sq, bg="plum")
bottom_middleFrame = tk.Frame(bottomFrame, height=sq, width=sq, bg="violet")
bottom_rightFrame = tk.Frame(bottomFrame, height=sq, width=sq, bg="purple")

topFrame.pack(side=tk.TOP)
middleFrame.pack(side=tk.TOP)
bottomFrame.pack(side=tk.TOP)

top_leftFrame.pack(side=tk.LEFT)
top_middleFrame.pack(side=tk.LEFT)
top_rightFrame.pack(side=tk.LEFT)

middle_leftFrame.pack(side=tk.LEFT)
middle_middleFrame.pack(side=tk.LEFT)
middle_rightFrame.pack(side=tk.LEFT)

bottom_leftFrame.pack(side=tk.LEFT)
bottom_middleFrame.pack(side=tk.LEFT)
bottom_rightFrame.pack(side=tk.LEFT)

win.bind("<Button-1>", click)
win.mainloop()

Because I cannot get a name of clicked widget I would like to sort it out by making new class with new method/attribute. However, everything works except functionality of pack.

import tkinter as tk

class Frame_ext(tk.Frame):
    def __init__(self, master=None, string=None, cnf={}, **kw):
        super().__init__(master=None, cnf={}, **kw)
        self.string = string

def click(event):
    print (event.widget.string)

sq=250
win = tk.Tk()

topFrame = tk.Frame(win)
middleFrame = tk.Frame(win)
bottomFrame = tk.Frame(win)

top_leftFrame = Frame_ext(topFrame, height=sq, width=sq, bg="sienna", string="1")
top_middleFrame = Frame_ext(topFrame, height=sq, width=sq, bg="chocolate", string="2")
top_rightFrame = Frame_ext(topFrame, height=sq, width=sq, bg="peru", string="3")
    
middle_leftFrame = Frame_ext(middleFrame, height=sq, width=sq, bg="sandy brown", string="4")
middle_middleFrame = Frame_ext(middleFrame, height=sq, width=sq, bg="salmon", string="5")
middle_rightFrame = Frame_ext(middleFrame, height=sq, width=sq, bg="rosy brown", string="6")

bottom_leftFrame = Frame_ext(bottomFrame, height=sq, width=sq, bg="plum", string="7")
bottom_middleFrame = Frame_ext(bottomFrame, height=sq, width=sq, bg="violet", string="8")
bottom_rightFrame = Frame_ext(bottomFrame, height=sq, width=sq, bg="purple", string="9")

topFrame.pack(side=tk.TOP)
middleFrame.pack(side=tk.TOP)
bottomFrame.pack(side=tk.TOP)

top_leftFrame.pack(side=tk.LEFT)
top_middleFrame.pack(side=tk.LEFT)
top_rightFrame.pack(side=tk.LEFT)

middle_leftFrame.pack(side=tk.LEFT)
middle_middleFrame.pack(side=tk.LEFT)
middle_rightFrame.pack(side=tk.LEFT)

bottom_leftFrame.pack(side=tk.LEFT)
bottom_middleFrame.pack(side=tk.LEFT)
bottom_rightFrame.pack(side=tk.LEFT)

win.bind("<Button-1>", click)
win.mainloop()

Anyone know what is the reason of it? I am not sure if class inheritance is done correctly. Looks ok to me. Thanks.

Upvotes: 0

Views: 38

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385950

You need to pass the master you receive to the superclass __init__. Also, you don't need to accept or pass in cnf.

class Frame_ext(tk.Frame):
    def __init__(self, master=None, string=None, **kw):
        super().__init__(master=master, **kw)
                                ^^^^^^

Upvotes: 1

Related Questions