Python3:Kinter:Button runs alone

Asked

Viewed 86 times

0

good afternoon I was studying graphical interfaces in Kinter and this problem arose My interface has a simple button and its function was simply to print the contents of a variable that was passed in argument, but this when it is created it automatically runs and then when I press it no longer runs Follows the code:

from tkinter import *
class teste():
        def batata(self,T):
                print(T)
        def __init__(self):
                root = Tk()
                root.title("Teste")
                root.geometry("800x600")
                nome='Avocado'
                Frame1 = Frame(root)
                self.listagem=["livro","pedra"]
                Frame1.grid(row = 0, column = 0,rowspan=2,sticky = W+E+N+S)
                Texto1=Label(Frame1,text=self.listagem[0]).grid(row=0,column=0,rowspan=2,pady=10)
                ButtCarderno1=Button(Frame1,text=self.listagem[0],command=self.batata(self.listagem[0])).grid(row=2,column=0,rowspan=2,pady=1)
                root.mainloop()


teste()

If anyone knows what’s going on I thank you pk for looking for no answers

1 answer

2


This is happening because you end up invoking the method batata when creating the button:

ButtCarderno1=Button(Frame1,text=self.listagem[0],command=self.batata(self.listagem[0])).grid(row=2,column=0,rowspan=2,pady=1)

With that the command of your button ends up actually getting the return of the method batata, that is, null, note that if you click this button after the window is opened, nothing will happen.


See this simplified example of a function on the button:

def batata():
    print("livro")

root = Tk()
root.title("Teste")
root.geometry("800x600")
frame = Frame(root)
frame.grid(row = 0, column = 0,rowspan=2,sticky = W+E+N+S)
texto = Label(frame,text="livro").grid(row=0,column=0,rowspan=2,pady=10)
botao = Button(frame,text="livro",command=batata).grid(row=2,column=0,rowspan=2,pady=1)
root.mainloop()

Notice that I assign the function batata in the command, but I did not invoke the same, with that the screen is created without calling the function batata and clicking on the button is displayed book on the console.


How you want to send parameters to command from the button, you can use a lambda, thus the function is not executed immediately, only when the button is clicked:

ButtCarderno1=Button(Frame1,text=self.listagem[0],command=lambda: self.batata(self.listagem[0])).grid(row=2,column=0,rowspan=2,pady=1)

Realize that now the command receive a lambda (command=lambda), which in turn will invoke the potato method.


Your code will then be more or less as follows:

from tkinter import *

class teste():

    def batata(self,T):
        print(T)

    def __init__(self):
        root = Tk()
        root.title("Teste")
        root.geometry("800x600")
        nome='Avocado'
        Frame1 = Frame(root)
        self.listagem=["livro","pedra"]
        Frame1.grid(row = 0, column = 0,rowspan=2,sticky = W+E+N+S)
        Texto1=Label(Frame1,text=self.listagem[0]).grid(row=0,column=0,rowspan=2,pady=10)
        ButtCarderno1=Button(Frame1,text=self.listagem[0],command=lambda: self.batata(self.listagem[0])).grid(row=2,column=0,rowspan=2,pady=1)
        root.mainloop()


teste()

Browser other questions tagged

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