Davide Des
Davide Des

Reputation: 101

Error when creating a Toplevel widget in Python

I'm coding an application to control serial connected device, right now I'm stuck in a GUI error, here is the simplified code:

    import Tkinter

class PaginaPrincipale(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)

        def connetti():
            pagina_connessione=Tkinter.Toplevel()
            pagina_connessione.title("Gestione connessione")

            pagina_connessione.mainloop()


        self.parent = parent
        self.grid()
        self.gestisci_connessione = Tkinter.Button(self, text="Connetti!", command=connetti)
        self.gestisci_connessione.grid(row=0, column=0, sticky='EW')


if __name__ == "__main__":
    applicazione = PaginaPrincipale(None)
    applicazione.title = 'Pannello di controllo'
    applicazione.mainloop()

When I run the program I get this error:TypeError: 'str' object is not callable

I'm new to Python2.7, I hope someone could tell me what I did wrong!

Upvotes: 0

Views: 384

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

The widget has a method named title, which you can use to set the title. However, you are replacing this function with a string when you do this:

applicazione.title = 'Pannello di controllo'

Once you've done that, any subsequent attempt to call the function title will result in the error you get (ie: you can't "call" a string).

Instead, you need to call title as a function:

applicazione.title('Pannello di controllo')

Upvotes: 1

Related Questions