Python Forum
How to Locate an Attribute's Parent Object?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Locate an Attribute's Parent Object?
#1
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.
Reply
#2
What is a box? What is a square? Could you provide some code please to help understand the question you are asking?
Reply
#3
(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.
Reply
#4
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.
Reply
#5
(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.
Reply
#6
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Chain object that have parent child relation.. SpongeB0B 10 1,096 Dec-12-2023, 01:01 PM
Last Post: Gribouillis
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,706 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,898 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 739 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,362 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,639 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,919 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,496 Jul-29-2022, 09:19 PM
Last Post: Gribouillis
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,127 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,398 Jun-24-2022, 08:37 AM
Last Post: Anushka00

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020