parameter master=None inside the class in module Tkinter (python)

Asked

Viewed 801 times

5

I’m learning the Tkinter module in the course of Professor Neri Neitzke, No, I don’t understand what the parameter is for ( master=None ) no __init__(self, master=None) of the class below, could someone explain to me what the functionality of this parameter in the class? , follows the code below:

from tkinter import *


class App(Frame):
    def __init__(self, master=None): #esse parametro, para que serve?
        Frame.__init__(self, master)
        self.pack()
        self.criarBotoes()
        self.criarLabels()
        self.entradaDados()

    def criarBotoes(self):
        # primeira forma de criar botões
        self.botao = Button(self)
        self.botao['text'] = 'Bem vindo ao Python'
        self.botao['fg'] = 'red'
        self.botao['bg'] = 'yellow'
        self.botao.pack(side='top')

        # segunda forma de criar botões
        self.botao1 = Button(self, text='Segundo botão', fg='blue', bg='red')
        self.botao1.pack(side='top')

    def criarLabels(self):
        self.label = Label(self)
        self.label['text'] = 'Bem vindo ao Python'
        self.label.pack(side='top')

    def entradaDados(self):
        self.edit = Entry(self)
        self.edit.pack(side='top')


#criando a aplicação
minhaAplicacao = App()

minhaAplicacao.master.title('videoaulas Neri')
minhaAplicacao.master.maxsize(1024, 768)

#inicia a aplicação
minhaAplicacao.mainloop()

2 answers

1

In a simple way. Your class is heiress of a frame and this frame should have a window where it will fit, the master means exactly that, the place where it will be placed.

Leaving the argument master as None means that the frame will be positioned in the main window (known as root). You can also do this with any other widget and point the master to a frame and etc.

Note: First time posting here.

0

In Tkinter widgets are heirs of a frame, for example, I can create a Button which sits within a Label which sits within a Frame which is inside the window ( known as root or root ).

The parameter master is the location where the widget will be positioned, as in this example below:

from tkinter import *

def exit_button(master):
    button = Button(master,text="Sair",bg="red",fg="white")
    button.pack()

root = Tk()
exit_button(root)
root.mainloop()

In this example, I pass as argument an object of Tk ( the program window ), but I could also pass a Frame or anything else to be the place where the exit button would be created.


What happens if I leave the master as None ?

When we do not set a master for the frame, it will by default be created in your window which is the "parent" of all frames.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.