I'm trying to make an GUI that runs a function using Tkinter, I'm assigning the function that I want to run to a button, however when I run the .py the function runs automatically and the button doesn't work. I don't know if I'm doing anything wrong. I have the following code:
# ------ Librerias usadas ------
import tkinter as tk
# ------ BackEnd ------
def obtain_number(list1,list2):
len1 = len(list1)
len2 = len(list2)
print(len1,len2)
# ------ FrontEnd ------
root = tk.Tk()
root.title('App')
root.geometry("500x480")
# root.iconbitmap() # Icono de la aplicacion (direccion donde se encuentra)
# Agregar elementos
# ---- Seleccionar las marcas ----
checked_boxes_marcas = ['1','2','3']
lista_marcas = ['1','2','3']
lista_marcas.sort()
# create a function to update the list of checked boxes
def update_checked_boxes_marcas(v_cb,v_r):
if v_cb.get():
checked_boxes_marcas.append(v_r)
else:
checked_boxes_marcas.remove(v_r)
# print(checked_boxes_marcas)
column_marcas = 0
inter = 3
tk.Label(text='Marcas').grid(row=0,column=column_marcas)
for i in lista_marcas:
value = tk.BooleanVar(value=1)
checkbox = tk.Checkbutton(root, text=i, variable=value,
command=lambda v_cb=value, v_r=i: update_checked_boxes_marcas(v_cb,v_r))
checkbox.grid(row=inter, column=column_marcas,sticky='w')
inter += 3
# ---- Seleccionar los medios ----
checked_boxes_medios = ['a','b','c']
lista_medios = ['a','b','c']
lista_medios.sort()
# create a function to update the list of checked boxes
def update_checked_boxes_medios(v_cb,v_r):
if v_cb.get():
checked_boxes_medios.append(v_r)
else:
checked_boxes_medios.remove(v_r)
# print(checked_boxes_medios)
column_medios = 7
tk.Label(text='Medios').grid(row=0,column=column_medios)
inter = 3
for i in lista_medios:
value = tk.BooleanVar(value=1)
checkbox = tk.Checkbutton(root, text=i, variable=value,
command=lambda v_cb=value, v_r=i: update_checked_boxes_medios(v_cb,v_r))
checkbox.grid(row=inter, column=column_medios,sticky='w')
inter += 3
# ---- Botones de validacion, reinicio y ejecutar ----
buttom_ejecutar = tk.Button(root,text='Ejecutar',command=obtain_number(
checked_boxes_medios,checked_boxes_marcas
)).grid(row=55,column=18)
# Correr la aplicacion
root.mainloop()
I would like that the GUI runs the function when I click the button and it should change depending on the selected checkboxes. I appreciate the help.