-1

This is a part of code from a tkinter game Random and tkinter module have been imported. The questions I have are -

1. The Struct class has only been initialized but not defined and simply passed yet it is used.

def run(rows, cols): # create the root and the canvas global canvas root = Tk() margin = 5 cellSize = 15 canvasWidth = 2*margin + cols*cellSize canvasHeight = 2*margin + rows*cellSize+100 canvas = Canvas(root, width=canvasWidth, height=canvasHeight) canvas.pack() # Store canvas in root and in canvas itself for callbacks root.canvas = canvas.canvas = canvas # Set up canvas data and call init class Struct: pass canvas.data = Struct() canvas.data.margin = margin canvas.data.cellSize = cellSize canvas.data.canvasWidth = canvasWidth canvas.data.canvasHeight = canvasHeight canvas.data.rows = rows canvas.data.cols = cols canvas.data.canvasWidth = canvasWidth canvas.data.canvasHeight = canvasHeight canvas.data.player1Score = 0 canvas.data.player2Score = 0 canvas.data.inGame = False init() # set up events root.bind("<Key>", keyPressed) timerFired() # and launch the app root.mainloop() # This call BLOCKS (so your program waits until you close the window!) run(40,40) 

2. Also what is happening in this line of code:

root.canvas = canvas.canvas = canvas class Struct: pass canvas.data = Struct() 
  1. How are the canvas.data._______ being used since no class has been defined?
1
  • Please fix the indentation of the posted code. Commented Dec 10, 2019 at 1:34

1 Answer 1

0

Question: Struct Class undefined yet used?

You are wrong, the class Struct is defined.
This line defines a valid object:

class Struct: pass 

The following two examples are equivalent!

  1. Empty object Struct becomes a canvas attribute and then extend the object with attributes.

    margin = 5 # Define the object class Struct: pass # Instantiate the object `Struct` # Assign the reference to the object `Struct` to the attribute `.data` canvas.data = Struct() # Assing the value `margin` to the attribute `.margin` of the object `Struct` canvas.data.margin = margin # Access the attribute `.margin` in the object `Struct` print(canvas.data.margin) # >>> 5 
  2. Object Struct using a predefind class Struct.__init__

    margin = 5 # Define the object class Struct: def __init__(self, margin): self.margin = margin # Instantiate the object `Struct` # Pass the value `margin` to it # Assign the reference to the object `Struct` to the attribute `.data` canvas.data = Struct(margin) # Access the attribute `.margin` in the object `Struct` print(canvas.data.margin) # >>> 5 
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your answer, but how did we access the attribute '.data' of the canvas?
@AppleKrumble: "access the attribute .data of the canvas"*: canvas.data. Take the tour Python - Object Oriented
No what I meant is 'canvas' is an object of the 'Canvas' class which is a part of the 'Tkinter' module. So 'canvas.data' is an attribute of the object 'canvas' which in turn becomes the object of 'Struct' class?
@AppleKrumble: Are you asking how would it be possible to add a attribute, here .data, to a tkinter class Canvas instance object, here canvas?
Yes, kind of , is this what is happening in this case ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.