4

So I realized I did some beginner mistakes in my code. So I made a few steps back to the first attemps. I have an small GUI with one window.

Now I have some questions:

  1. why does also greet has to have (self) ?
  2. how do I call (is call the right word here?) greet from another py file?

Thanks in advance.

from tkinter import *

class MyFirstGUI(object):
    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        self.label = Label(master, text="This is our first GUI!").pack()

        self.greet_button = Button(master, text="Greet", command=self.greet).pack()

        self.close_button = Button(master, text="Close", command=self.quit).pack()

    def greet(self):
        print('Hello')

    def quit(self):
        self.master.destroy()


root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
1

2 Answers 2

2

why does also greet has to have (self) ?

Other program languages use @ to define different class attribute however python uses self for this. self is used within a class to let the class know that there is a class attribute or method that can be accessed from anywhere in the class (or from outside the class).

Example benefit: If we have a function that updates a variable and a call to that function we will need to use global and the call to the function must occur after the function has been defined in the code.

like this:

x = 0

def update_x():
    global x
    x+=1
    print(x)

update_x()

However in a class we can avoid the use of global and define all of our methods (functions in a class using self) after the code that calls those methods allowing us to keep things a little cleaner.

Like this:

class MyClass():
    def __init__(self):
        self.x = 0
        self.update_x()

    def update_x(self):
        self.x +=1
        print(self.x)

MyClass()

how do I call (is call the right word here?) greet from another py file?

You will need to import the file just like you would import a library.

For example if say your main program is in main.py and have another py file in the same directory called test.py and you wish to call on something in the test.py file from the main.py file you will need to import the test.py file on the main.py file

For most cases of files that fall under the same directory do this:

import test

Sometimes your program is in a package of some kind and you may need to provide the import like this.

import package_name.test

you can use this as a test example:

test.py file contains:

def plus_one(number):
    x = number + 1
    return x

main.py file contains:

import test

x = test.plus_one(5)
print(x)

Console output should be:

6
Sign up to request clarification or add additional context in comments.

2 Comments

@K-Doe Glad to help. If my answer works for you please make sure to select the check box next to the answer.
Sure! :) Have a nice day!
2

1. why does also greet has to have (self) ?

The function greet() is a function of your MyFirstGUI class, you can see that when you bind this function to the greet_button the function is put after self. It means that the function has a link to self (which is MyFirstGUI). This link is made by putting self in the function definition.

2. how do I call (is call the right word here?) greet from another py file? (I'm not sure I understand what you ask)

Yes call is right ! If you want to call this function from another file, you would have to import the MyFirstGUI in your main file and create an instance of this object.

mainFile.py:

from tkinter import *
from guiFile import MyFirstGUI

root = Tk()
my_gui = MyFirstGUI(root)
my_guy.greet()
root.mainloop()

guiFile.py:

from tkinter import *    
class MyFirstGUI(object):
    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        self.label = Label(master, text="This is our first GUI!").pack()

        self.greet_button = Button(master, text="Greet", command=self.greet).pack()

        self.close_button = Button(master, text="Close", command=self.quit).pack()

    def greet(self):
        print('Hello')

    def quit(self):
        self.master.destroy()

5 Comments

To be clear when a function is in side of a class and uses self it is called a method. On the same note if a variable is being assigned with the use of self. it is called a class attribute.
Hi thanks for your explanation, it worked fine and I understand what's going on! Is there also a way to use greet without having the GUI to start?
The code does what it should, but if MyFirstGUI is showed and I push the Quit button it says : "name 'root' is not defined". But root = Tk() is in the code?
Ho yes, it's probably because when you press quit() in one window, the self.master.destroy() delete the rootinstance... Maybe you should replace it by self.destroy() to quit only the window you want to quit !
@Mike-SMT Just attribute or instance attribute. A class attribute would be on the class. It may be read via self. if the instance has no such attribute, but to assign, you have to go via the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.