How can I avoid repeating the same buttons?

4

I am developing an application with Python, using the graphic library .

Question

I created a button with and I link it to a function that unfolds 3 more buttons If I press the same button again, there are 3 buttons and the 3 buttons are repeated to give a total of 6 buttons.

And how can I know if the button was pressed, I thought it was with status but I see no?

This is what I mention:

Code

(there are still several things that I still do not know how to do, for example, to draw lots)

### Domino Center
### Autor: Enrique Jesus Mora Nieves
### Estado: En desarrollo
import tkinter as tk
from math import *
class App(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.Interfaz()

    def Interfaz(self):
        """El objetivo de la funcion Interfaz, es la de contener los botones 
        principales de la aplicacion, sin embargo planeo cambiar esto por otra 
        forma, mientras trabajare con esto.
        """
        ########################################################      
        #####################logo###############################
        self.logo = tk.PhotoImage(file="img/2.png")
        etiquetalogo = tk.Label(self,
                            image= self.logo).pack()
        ####################Titulo Modalidad####################
        self.modalidad = tk.Label(self,
                                  text="Modalidad",
                                  fg="red",).pack(side="top",
                                                  pady=10,
                                                  padx=20)
        ############Boton Individual#############################
        self.boton_individual = tk.Button(self,
                                          text="Individual",
                                          command=self.Individual,
                                          relief="flat",
                                          overrelief="raised").pack(side="left",
                                                                    pady=10,
                                                                    padx=20)
        ##############Boton Pareja###############################
        self.boton_pareja = tk.Button(self,
                                      text="Pareja",
                                      command=None,
                                      relief="flat",
                                      overrelief="raised").pack(side="left",
                                                                pady=10,
                                                                padx=20)
        ##############Boton Equipo################################
        self.boton_equipo = tk.Button(self,
                                      text="Equipo",
                                      command=None,
                                      relief="flat",
                                      overrelief="raised").pack(side="left",
                                                                pady=10,
                                                                padx=20)
        ##############Boton Sorteo################################
        self.boton_sorteo = tk.Button(self,
                                      text="Sorteo",
                                      command=None,
                                      relief="flat",
                                      overrelief="raised").pack(side="left",
                                                                pady=10,
                                                                padx=20)
        ##################################################################

    def Individual(self):
        """La funcion Individual, Es para pedir al usuario la cantidad de 
        personas que van a participar en la modalidad Individual, datos 
        requeridos para la funcion "Distribucion" y la funcion "Sorteo"
        """
        ####################################################################
        #########Titulo: "Introduzca el numero de personas (Minimo 4)######
        self.titulo_numero_de_personas= tk.Label(self,
                                             text="Introduzca el \n numero de personas (minimo 4)").pack()
        #########Input de tipo INT, inicializado a 4 que es lo minimo, no quise usar condicionales en este caso######
        self.IntVar_individual = tk.IntVar()
        self.IntVar_individual.set(4)
        self.numero_de_personas = tk.Entry(self,
                                       textvariable=self.IntVar_individual,
                                       width=4,
                                       relief="flat").pack()
        #########Listbox para servir como salida#############################
        self.lst_individual = tk.Listbox(self,
                                         width=50,
                                         height = 5,
                                         relief="raised")
        self.lst_individual.pack()
        #########Boton: Distribucion#########################################
        self.boton_distribucion= tk.Button(self,
                                           text="Calcular Distribucion",
                                           fg='white',
                                           command=self.Distribucion)
        self.boton_distribucion.config({'background': '#000'})
        self.boton_distribucion.pack(side="left",
                                     padx=20,
                                     pady=10)
        ############Boton: Limpiar############################################
        self.boton_limpiar = tk.Button(self,
                                       text= "Limpiar pantalla",
                                       fg="white",
                                       command=self.Limpiar)
        self.boton_limpiar.config({"background":"#000"})
        self.boton_limpiar.pack(side="right",
                                padx=20,
                                pady=10)
        ##########Boton: Siguiente############################################
        self.boton_siguiente = tk.Button(self,
                                         text="Siguiente",
                                         fg="white",
                                         command=None)
        self.boton_siguiente.config({"background":"#000"})
        self.boton_siguiente.pack(side="right",
                                  padx=20,
                                  pady=10)
        #######################################################################
    def Distribucion(self):
        """La funcion Distribucion, tiene como objetivo determinar la 
        cantidad de mesas que se van a necesitar en base a la cantidad de 
        personas que se han introducido anteriormente, y indicar cuantas 
        personas deben esperar (1, 2 o 3), de esta manera se facilita al 
        organizador la tarea de calcular la cantidad de mesas y cuantas 
        personas quedan en espera (algo simple, pero bueno, hay que 
        facilitar las cosas xD)

        En una mesa solo pueden haber 4, para calcular cuantas mesas hay 
        disponibles, se debe dividir entre 4 si el organizador introduce un 
        numero que no vaya de 4 en 4, la variable quedaria con un valor decimal
        se utiliza la funcion trunc() para eliminar ese decimal y dejar la 
        variable con un numero entero

        self.resto_mesas, tiene 4 resultados posibles (0, 0.25, 0.50 y 0.75) 
        esta variable es para determinar si quedan personas por fuera, es decir 
        personas que deben esperar, 0 = 0, 0.25 = 1, 0.50 = 2 y 0.75 = 3
        """

        ######variable que toma el valor de un IntVar() que se pide mediante un .get()#################
        self.n_personas = self.IntVar_individual.get()

        self.mesas = trunc(self.n_personas/4)###Mesas necesarias para el numero de personas/4

        self.resto_mesas = (self.n_personas/4)-self.mesas
        if self.resto_mesas == self.resto_mesas:
            if self.resto_mesas == 0.25:
                self.personas_extras = 1
            elif self.resto_mesas == 0.50:
                self.personas_extras = 2
            elif self.resto_mesas == 0.75:
                self.personas_extras = 3
        ### En este bloque de codigo se le muesta al usuario la distribucion de las mesas y cuantas personas
        ### extras hay, mediante un insert() en la lista self.lst_individual
        if self.resto_mesas>0 and self.resto_mesas<1:
            self.n_personas = self.n_personas-self.personas_extras
            texto = "Cantidad de mesas disponibles: %d, para las %d personas" %(self.mesas,self.n_personas)
            texto2 = "Y hay %d personas esperando" % (self.personas_extras)
            self.lst_individual.insert(tk.END, texto, texto2)
        else:
            texto = "Cantidad de mesas disponibles %d para las %d personas" %(self.mesas,self.n_personas)
            self.lst_individual.insert(tk.END,texto)

    def Limpiar(self):
        """La funcion Limpiar, Borra la informacion contenida en los indices de 
        la lista self.lst_individual
        """    
        self.lst_individual.delete(0,tk.END)

myapp = App()
myapp.master.title("Domino Center")
myapp = tk.mainloop()

Sorry that I did not finish devising it, stackoverflow, for what I see it does not allow the use of the tabulator and good to align all those lines of code is something late, idente the ones that I could sorry.

    
asked by Enrique Mora 29.08.2016 в 09:33
source

1 answer

2

Why not create a Boolean value that becomes True when you press? With adding an If on the button, you could already check if it is necessary to create the extra buttons or not.

    
answered by 29.08.2016 в 12:57