1
I have 3 buttons, two with the same name and one with different name and each with its properties, I would like to know how to capture the text of each button in the same definition. Of course, I want to capture the property of the button that called the event.
For example, in my graphical interface, these three buttons appear:
Facts
rumours
Overcoming?
When I click the button "Facts", i want to capture the Text parameter of this button, i.e., the Facts. If I click on "rumours" sharing the same button name "Facts", I want to capture the rumors. There’s a way I can do this with Tkinter?
My code:
try:
from tkinter import *
except:
from Tkinter import *
tela = Tk()
def clicar():
# Como exibir o texto do botão clicado?
print(btn["text"])
btn = Button(tela,text="Fatos",font=("",20),bg="black",fg="white",command=clicar)
btn.grid(row=1,column=1)
btn = Button(tela,text="boatos",font=("",20),bg="black",fg="white",command=clicar)
btn.grid(row=2,column=1)
btn2 = Button(tela,text="Supertição?",font=("",20),bg="black",fg="white",command=clicar)
btn2.grid(row=3,column=1)
tela.mainloop()
Color palette with the jsbueno, Thank you very much!:
try:
from tkinter import *
except:
from Tkinter import *
# Instância
tela = Tk()
# Lista de cores que vão aparecer na paleta
cores = ["#1bbc9b","#2ecd71","#3598db","#9b58b5","#5c6d7d","#16a086","#27ae61","#2a80b9","#8f44ad","#2d3e50","#f1c40f","#e77e23","#e84c3d","#ecf0f1","#95a5a5","#f39c11","#d55401","#c1392b","#bec3c7","#7e8c8d"]
# Definição que vai apenas exibir a cor escolhida.
def clicar(btn):
print(btn["bg"])
linha = 1
# Variável que vai andar pela lista
quantidade = 0
while linha <=4:
coluna = 1
while coluna<=4:
# Atualiza dados da cor
cor_analise = cores[quantidade]
# Cria um botão
btn = Button(tela,font=(" ",12),bg=cor_analise,fg="white",activebackground=cor_analise,highlightbackground="white",highlightthickness=1,height=1,width=3)
btn["command"] = lambda btn=btn: clicar(btn)
btn.grid(row=linha,column=coluna)
# Atualiza os andantes
coluna=coluna+1
quantidade = quantidade+1
linha = linha+1
tela.mainloop()
Why you need to create the first two buttons with the same variable
btn
? Can’t you create a variable for each button? The way you are doing the variable that creates the "Facts" button is overwritten when you create the "hearsay" button" .– sant0will
I wanted to optimize the creation of a color table with approximately 200 colors, there are more than 400 lines for this and thousands of parameters to pass(If I remember some detail, I have to change in the 200 lines.) Using loops with x and y I create the whole "table" with 30 lines. In this case, each "button" assigns its color and its text according to a list of all 200 colors. And in the program that I’m doing, it’s going to happen basante this. If I can capture the parameter from a specific button, my code will be reduced by more than 95%.
– Gabriel Gregório da silva
I updated my question using a beta version of the palette, runs it there in Python3, the optimization is fantastic.
– Gabriel Gregório da silva
This code is poorly done ok, it was just a test even, the official version of the palette is still in alpha and the code has been optimized at least 3 times more than the one above.
– Gabriel Gregório da silva