Calling functions using buttons

Asked

Viewed 719 times

0

In the code below I created a graphical interface with some buttons, but when I click on the buttons they do not call the function. I didn’t find the error, I ask for help.

# Importando a biblioteca tkinter
from tkinter import *
from functools import partial

def abre(j):
    if (j==1):
        lbr['text'] = 'Clicou no Primeiro'
    elif(j==2):
         lbr['text'] = 'Clicou no Segundo'




# Criando a janela principal
janela = Tk()
janela.geometry('600x600+150+150')
janela.state('zoomed')
janela.title ('DESENVOLVIMENTO DE SISTEMA COMPLETO II')

# para desabilitar botão state=DISABLED

# Gerando os botôes
rotulo =('Primeiro','Segundo','Terceiro','Quarto','Quinto')
k = 180
j=0
for i in rotulo:
        j += 1
        bt = Button(janela, command=(abre(j)), width=20, height=2, anchor='center',font=("Helvetica", 12) , text=(i),bg='#696969', fg='#FFA500')
        bt.place(x=k,y=2)
        k += 200

lbr = Label(janela,text='Teste dos botões')
lbr.place(x=200,y=200)


# Término do sistema.
janela.mainloop()
  • Hello @José Alves, please test my answer. If you agree please mark as right. If you need help I’m available. Hug!

1 answer

0


I tried to keep the most of your code. Basically I created the function exbir_botao it receives as parameters var which is a value of dict rotulo and x which will be used to determine where the button will be displayed. In while I go through the dictionary, increment x and call the function to display the button.

Follows the code:

from tkinter import Tk, Label, Button

# Dicionario com os rótulos dos botões

rotulo = {1:'Primeiro', 2:'Segundo', 3:'Terceiro', 4:'Quarto', 5:'Quinto'}


# Criando a janela principal

janela = Tk()
janela.geometry('600x600+150+150')
janela.state('zoomed')
janela.title ('DESENVOLVIMENTO DE SISTEMA COMPLETO II')


msg = "Teste do botão: "

lbr = Label(janela,text=msg)

lbr.place(x=200,y=200)


# Altera a mensagem no label (var é um value do dict rotulo) 
def exibir_msg(var):    
    lbr.config(text=msg+var)


# Criar o botão com o text retirado do dicionário rotulo e exibi-lo na janela na posição especificada por x
def exbir_botao(var, x):    
    botao = Button(janela, text=var, command=lambda:exibir_msg(var),
                   width=20, height=2, anchor='center',font=("Helvetica", 12),
                   bg='#696969', fg='#FFA500')    
    botao.place(x=x,y=2)


x = 180 # posição inicial de variável x do primeiro botão

for k in rotulo: # note que k é a chave do dicionario rotulo 

    var = rotulo.get(k)

    exbir_botao(var, x)

    x += 200 # incrementando x em 200  


janela.mainloop()
  • I understood and your code became much smarter than mine. Could you indicate where was the error in my code? grateful.

    1. For example, your job abre made a reference to the label lbr before declaring it. 2) rotulo you declared as tuple, while it would be easier to use a dictionary that allows you to retrieve a value indexed to a key. 3) The Button statement you made inside the loop for passed the impression that was correct because the buttons were created in the specified places, but as you were always assigning the declaration of the button the variable bt ...
  • ... (in the same variable) what happened was that at the end of the loop all the buttons were actually an "echo" of the same button (bt) where only property was being updated text, however the property command was always the same. 4) Finally note that in import i make it clear that I am importing Tk, Label, Button from the Tkinter module. I hope I have been clear.

Browser other questions tagged

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