Python GUI with Tkinter and Multiple Files Python GUI with Tkinter and Multiple Files tkinter tkinter

Python GUI with Tkinter and Multiple Files


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 MyFirstGUIroot = 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()


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 = 0def 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 testx = test.plus_one(5)print(x)

Console output should be:

6