Python Forum

Full Version: How to Locate an Attribute's Parent Object?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey! So I'm using Tkinter to make a GUI for Sudoku; just as a project to get better. Throughout my functions I am passing a variable for the sudoku square on the board/GUI, but that square belongs to one of 9 boxes (I didn't do the standard 9x9 matrix).

For example:
boxes[0].squares[4] would be the top left box and the left/center square. If I passed that object into a function simply as 'square,' could I figure out which box it belongs to within that function?? I was passing in both box and square but I'm trying to clean things up to make solving problems clearer.

Thanks so much. I feel like this is a simple thing but I don't know the proper wording so I couldn't find an answer by searching. I'm not even sure if attribute is the right word.
What is a box? What is a square? Could you provide some code please to help understand the question you are asking?
(Nov-12-2020, 10:11 PM)deanhystad Wrote: [ -> ]What is a box? What is a square? Could you provide some code please to help understand the question you are asking?

Omg.. I tried to shorten my post and I guess I deleted an important part...

A Box is a class and so is a Square. The parents are tk.Frame and tk.Button, respectively.

So the __init__ for the Box is to create a list:

self.square[0] = Square(grid coords)
self.square[1] = Square(grid coords)
etc...

Sorry about that and thank you.
Don't worry about being too wordy. Too much is better than not enough.

From what I can see there is no way to find out which Box contains the square. If this is important to know you could pass the box to the square and keep a reference in square. Something kind of like this:
class Square(superclassTBD):
    def __init__(self, parent, other_stuff):
        super().__init__(whatever)
        self.parent = parent
        ... other init stuff...

class Box(superclassTBD):
    def __init__(self, parent, other_stuff):
        super().__init__(whatever)
        self.boxes = [Box(self, x, y) for x in range(3) for y in range(3)]
If I knew more about boxes and squares I wouldn't have all these TBD's and whatever's.
(Nov-12-2020, 11:11 PM)deanhystad Wrote: [ -> ]Don't worry about being too wordy. Too much is better than not enough.

From what I can see there is no way to find out which Box contains the square. If this is important to know you could pass the box to the square and keep a reference in square. Something kind of like this:
class Square(superclassTBD):
    def __init__(self, parent, other_stuff):
        super().__init__(whatever)
        self.parent = parent
        ... other init stuff...

class Box(superclassTBD):
    def __init__(self, parent, other_stuff):
        super().__init__(whatever)
        self.boxes = [Box(self, x, y) for x in range(3) for y in range(3)]
If I knew more about boxes and squares I wouldn't have all these TBD's and whatever's.

Keeping track of which box the square is in through the Square __init__ is a good idea. Thanks!

If I print the object, which is the instance of a Square, it prints out, e.g. ".!box9.!square5", which is interesting because I don't have any objects named "box" or "square" with all lowercase. There are the classes Box and Square, then the global list boxes[x] and the Box-instance.squares[x].

Anyway, thanks again. This will do.
Okay, I figured it out. I ran help(Button), which I just learned about.

The answer is specific to Tkinter, but because the Square class is a Button, you can do square_instance.master to reference whatever the master object is.

That's not exactly what I wanted to know, as I feel like there probably is a way to find out which object another object belongs to that's built into python. It's not really important I know this right now, but I feel like if your working with other peoples' code there may be times when you need to trace things like this and it's probably a good tool to have.

Anyway, thank you!