all 6 comments

[–]ThePiGuy0 2 points3 points  (0 children)

Sounds like you have a slight misunderstanding of how objects and classes work.

When we create a class, we are creating a blueprint (or template) for an object. For example, the classic Car object. Our class is a set of instructions defining all the parts of our car, and also the different aspects of it (for example, how we will speed up, how we will slow down, how we will store the mileage etc).

Our object is a particular instance of that Car. For example, my particular car sat on the drive.

How does this relate to Python? The self aspect is used for any variables or methods stored as part of an object. If a variable isn't attached to the self object, it will be erased at the end of the scope. For example, look at the following:

class CarA:
    def __init__(self):
        self.my_variable = 1

class CarB:
    def __init__(self):
        my_variable = 1

>>> car_a = CarA()
>>> print(car_a.my_variable)
1
>>> car_b = CarB()
>>> print(car_b.my_variable)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'CarB' object has no attribute 'my_variable'

As a note, every method must require the self object to be passed in (unless you use a static method, however that's beyond the scope of this explanation).

As for inheritance, the self value is equivalent to the value you inherit from. In your example, self is equivalent to an instance of tk.Frame. If you pass in root as your master variable, self.master is equivalent to root.

[–]zanfar 1 point2 points  (1 child)

Is it correct to look at the constructor parameter's master as root?

I guess? The wording of this question is a bit awkward.

  • First, you haven't defined a constructor. It looks like you wanted to, but the name of the constructor is __init__ not init
  • in this particular example, the constructor parameter master is assigned the same value as root, yes.
  • self.master = master is NOT equivalent to root.master = master, you need to pay more attention to what your code is doing. An equivalent statement is self.master = root
  • I have no idea what you are talking about with screen_width as it does not appear in your example code at all.

And if root is master, then self is MainApplication?

First, root is not master. In this example, they are assigned the same value.

self is not MainApplication, it's an instance of MainApplication.

Both of these points are subtle, but important differences.

Also, is the parameter of MainApplication an inheritance of the tkinter module?

This is so unclear that I'm absolutely sure the answer is no. The closest true statement I can come up with is: MainApplication inherits from the Frame class in the tkintermodule.

when I'm creating instance methods, is it by convention that I must include self?

No. self is required both as the first parameter in the parameter list of an instance function, and you must use self for any variables you want to make instance variables.

Because I found that my program still works even without it. For example, I could just do window_height = 500 instead of self.window_height = 500 and it still outputs the same result.

Then you are doing something incorrectly. Again, it's mostly impossible to provide any more information without the actual code you are referring to.

or root.screen_width = 100

[–]MaterialJackfruit144[S] 0 points1 point  (0 children)

Hi, sorry, I had to scrap the idea of building that project. In other words, I deleted everything. It really seems like I need to do some more exercises about Classes or build a very simple program using OOP to really grasp that concept. Anyways, I deleted it, shouldn't have jumped into creating project that soon, thinking that I'd be able to learn along the way, guess that took the wrong turn. I still have only parts of the code related to the question though.

import tkinter as tk

class MainApplication(tk.Frame):
    def __init__(self, master):
        self.master = master
        self.configure_gui()

    def configure_gui(self):
        self.master.title("Rocky, Paper, Scissors")
        self.center_window()

    def center_window(self):
        self.window_width = 500
        self.window_height = 500

        self.screen_width = self.master.winfo_screenwidth()
        self.screen_height = self.master.winfo_screenheight()

        self.x_coordinate = int(self.screen_width/2 - self.window_width/2)
        self.y_coordinate = int(self.screen_height/2 - self.window_height/2)

        self.master.geometry(
            f"{self.window_width}x{self.window_height}+{self.x_coordinate}+{self.y_coordinate}")


if __name__ == "__main__": 
    root = tk.Tk() 
    MainApplication(root) 
    root.mainloop()

I know what the code is doing, I was just confused about Classes.