#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Thumbs up Listing Items from Checkboxes


    Hello all,

    I've been search for awhile on how to accomplish this task, but have not been successful.. I'm sure it's been asked, but I must be using wrong keywords.

    I've got a GUI app that allows a user to select none or many checkboxes, and I need to list the selections out.

    I'm not quite sure how to work out the issue with the commas(> 3, insert commas). I know I can set it to count how many selections have been made somehow, but I don't know how to go about it.

    Here's my code:

    Code:
            
            self.cheese = BooleanVar()
            Checkbutton(self,
                        text = "Cheese",
                        variable = self.cheese,
                        command = toppings_list
                        ).grid(row = 1, column = 0, sticky = W)
    
            self.lettuce = BooleanVar()
            Checkbutton(self,
                        text = "Lettuce",
                        variable = self.lettuce,
                        command = toppings_list
                        ).grid(row = 2, column = 0, sticky = W)
    
            self.onion = BooleanVar()
            Checkbutton(self,
                        text = "Onion",
                        variable = self.onion,
                        command = toppings_list
                        ).grid(row = 3, column = 0, sticky = W)
    
            self.tomato = BooleanVar()
            Checkbutton(self,
                        text = "Tomato",
                        variable = self.tomato,
                        command = toppings_list
                        ).grid(row = 4, column = 0, sticky = W)
    I was thinking I could set command = toppings_list, then have toppings_list keep track of how many are selected. But then I'm not sure how to go about printing each out in the correct format. I assume if I go this route, I would need to add them to a list and use a selector or some sort to insert a comma after each one except the last one?

    If the checkbox command is set to run a certain function, does it undo that function if I deselect the checkbox?

    I'm sorry if this does not make sense, I mainly have experience with web dev and am I still trying to wrap my head around this logic...

    Thanks for any help!!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Please submit a complete program. You needn't list every possible pizza topping. Explain what you want it to do that it fails to do.

    I can't begin to understand what you mean by "issue with the commas(> 3, insert commas)"

    nor do I understand "does it undo that function" (python does not execute the function backward if you deselect a checked box)

    Do you want to count the number of selections or actually know what the selections are?

    Try again.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    The goal of the program is to output a grammatically correct list based on the user's selections.

    Here's the entirety of my code thus far:

    Code:
    from Tkinter import *
    
    class Application(Frame):
        """ GUI application which allows a user to plan how a burger will be made. """
        def __init__(self, master):
            Frame.__init__(self,master)
            self.grid()
            self.create_widgets()
    
        def create_widgets(self):
            self.toppings = []
            
            # intro label
            Label(self,
                  text = "Choose how you would like your burger."
                  ).grid(row = 0, column = 0, columnspan = 2, sticky = W)
    
            # toppings checkboxes
            self.cheese = BooleanVar()
            Checkbutton(self,
                        text = "Cheese",
                        variable = self.cheese,
                        command = self.toppings.append('cheese')
                        ).grid(row = 1, column = 0, sticky = W)
    
            self.lettuce = BooleanVar()
            Checkbutton(self,
                        text = "Lettuce",
                        variable = self.lettuce,
                        command = self.toppings.append('lettuce')
                        ).grid(row = 2, column = 0, sticky = W)
    
            """self.onion = BooleanVar()
            Checkbutton(self,
                        text = "Onion",
                        variable = self.onion,
                        command = self.toppings.append('onion')
                        ).grid(row = 3, column = 0, sticky = W)
    
            self.tomato = BooleanVar()
            Checkbutton(self,
                        text = "Tomato",
                        variable = self.tomato,
                        command = self.toppings.append('tomato')
                        ).grid(row = 4, column = 0, sticky = W)
    
            # second column of choices
            self.pickles = BooleanVar()
            Checkbutton(self,
                        text = "Pickles",
                        variable = self.pickles,
                        command = self.toppings.append('pickles')
                        ).grid(row = 1, column = 1, sticky = W)
    
            self.mustard = BooleanVar()
            Checkbutton(self,
                        text = "Mustard",
                        variable = self.mustard,
                        command = self.toppings.append('mustard')
                        ).grid(row = 2, column = 1, sticky = W)
    
            self.mayo = BooleanVar()
            Checkbutton(self,
                        text = "Mayo",
                        variable = self.mayo,
                        command = self.toppings.append('mayo')
                        ).grid(row = 3, column = 1, sticky = W)
    
            self.ketchup = BooleanVar()
            Checkbutton(self,
                        text = "Ketchup",
                        variable = self.ketchup,
                        command = self.toppings.append('ketchup')
                        ).grid(row = 4, column = 1, sticky = W)
    
    
            # bun choice using radio buttons
            Label(self,
                  text = "Choose your bun:"
                  ).grid(row = 1, column = 2, sticky = W)
    
            self.bun_type = StringVar()
            self.bun_type.set(None)
    
            Radiobutton(self,
                        text = "White",
                        variable = self.bun_type,
                        value = "white"
                        ).grid(row = 2, column = 2, sticky = W)
            Radiobutton(self,
                        text = "Wheat",
                        variable = self.bun_type,
                        value = "wheat"
                        ).grid(row = 3, column = 2, sticky = W)"""
    
    
            # submit button
            Button(self,
                   text = "Make my burger!",
                   command = self.compile_order
                   ).grid(row = 5, column = 1, sticky = W)
    
    
            # results text box
            Label(self,
                  text = "Burger Ordered:"
                  ).grid(row = 7, column = 0, sticky = W)
            self.order_txt = Text(self, width = 50, height = 5, wrap = WORD)
            self.order_txt.grid(row = 8, column = 0, columnspan = 3)
            
    
        def compile_order(self):
            print self.toppings
            print self.cheese.get()
            
    
    # main
    root = Tk()
    root.title("Burger Order")
    root.geometry("450x240")
    
    app = Application(root)
    
    root.mainloop()
    I have parts commented out as they are not currently needed.

    I tried setting command for each of the two checkbutton to append a value to a list, but when the list is printed it has both values listed no matter what. I've read up a little about onvalue and offvalue, are those events that happen when the box is checked and not checked?

    My goal, restated:
    If a user selects cheese, lettuce, tomato, onion
    the output will say "cheese, lettuce, tomato, and onion."

    If a user selects cheese, the output will say "cheese."

    Sorry if I'm being unclear, I'm just very confuzzled about all this right now...
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    1) your goal to write grammatically improved sentences is independent of a gui. See the compile_order method.
    2) I installed the order into the Text box.
    3) I clear the Text box when new order is entered.
    4) I've rewritten the buttons so that you could load the toppings and bread choices from a file. (Removed Tomato variable. We have an array of toppings.) Now if you run out of mustard you don't need a python expert to update the menu choices.
    5) Your code didn't clear the list of toppings. I eliminated the list of toppings and changed the logic a little.
    6) you had callbacks for a lot of buttons. they didn't do anything useful that I could tell so I replaced them with the Text erasure functionality.
    Code:
    # reference:   http://effbot.org/tkinterbook/text.htm
    
    from Tkinter import *
    
    class Application(Frame):
        """ GUI application which allows a user to plan how a burger will be made. """
        def __init__(self, master):
            Frame.__init__(self,master)
            self.grid()
            self.create_widgets()
            self.customer_number = 0
    
        def create_widgets(self):
            # centered label would look better to me.
            Label(self,
                  text = "Choose how you would like your burger."
                  ).grid(row = 0, column = 0, columnspan = 2,)
    
            # left-aligned would look better to me.
            Label(self,text='Scrumptious toppings').grid(row=1,column=1,sticky=W)
            OFF = ''
            self.toppings = []
            for (i,ON) in enumerate('Cheese Lettuce Mustard'.split()):
                v = StringVar()
                v.set(OFF)
                self.toppings.append(v)
                Checkbutton(self,text=ON,variable=v,onvalue=ON,offvalue=OFF,command=self.erase).grid(
                    row=(i+2),column=0,sticky=W)
            Label(self,text="Choose your bun:").grid(row=1,column=2,sticky=W)
            self.bun_type = StringVar()
            self.bun_type.set('White')
            for (i,bread) in enumerate('White Wheat'.split()):
                Radiobutton(self,text=bread,variable=self.bun_type,value=bread,command=self.erase).grid(row=(2+i),column=2,sticky=W)
            Button(self,text = "Make my burger!",
                   command = self.compile_order
                   ).grid(row = 5, column = 1, sticky = W)
            Label(self,
                  text = "Burger Ordered:"
                  ).grid(row = 7, column = 0, sticky = W)
            self.order_txt = Text(self, width = 50, height = 5, wrap = WORD)
            self.order_txt.grid(row = 8, column = 0, columnspan = 3)
    
        def erase(self,*args,**kwargs):
            self.order_txt.delete('1.0',END)
    
        def compile_order(self):
            toppings = [topping.get() for topping in self.toppings]
            toppings = tuple(topping.lower() for topping in toppings if topping)
            bread = self.bun_type.get().lower()
            if not toppings:
                order = 'Plain burger'
            elif 1 == len(toppings):
                order = 'Burger with %s'%toppings
            elif 2 == len(toppings):
                order = 'Burger with %s and %s'%toppings
            else:
                order = 'Burger with %s and %s'%(', '.join(toppings[:-1]),toppings[-1])
            order += ' on %s.'%bread
            self.order_txt.insert(END,order)
            self.customer_number += 1
            print(self.customer_number,bread,toppings)
    
    root = Tk()
    root.title("Burger Order")
    root.geometry("450x240")
    
    app = Application(root)
    
    root.mainloop()

    Comments on this post

    • mdiggity agrees : YOU THE MAN!
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Ok. After racking my brain for hours, I finally got it. I ended up individually checking if each topping value was set, and appending it to the list if it was.

    I think I overthrought this one, but boy does it feel good to get this victory.

    Here's the code I got:

    Code:
    from Tkinter import *
    
    class Application(Frame):
        """ GUI application which allows a user to plan how a burger will be made. """
        def __init__(self, master):
            Frame.__init__(self,master)
            self.grid()
            self.create_widgets()
    
        def create_widgets(self):
            self.toppings = []
    
            # intro label
            Label(self,
                  text = "Choose how you would like your burger."
                  ).grid(row = 0, column = 0, columnspan = 2, sticky = W)
    
            # toppings checkboxes
            self.cheese = BooleanVar()
            Checkbutton(self,
                        text = "Cheese",
                        variable = self.cheese
                        ).grid(row = 1, column = 0, sticky = W)
    
            self.lettuce = BooleanVar()
            Checkbutton(self,
                        text = "Lettuce",
                        variable = self.lettuce
                        ).grid(row = 2, column = 0, sticky = W)
    
            self.onion = BooleanVar()
            Checkbutton(self,
                        text = "Onion",
                        variable = self.onion
                        ).grid(row = 3, column = 0, sticky = W)
    
            self.tomato = BooleanVar()
            Checkbutton(self,
                        text = "Tomato",
                        variable = self.tomato
                        ).grid(row = 4, column = 0, sticky = W)
    
            # second column of choices
            self.pickles = BooleanVar()
            Checkbutton(self,
                        text = "Pickles",
                        variable = self.pickles
                        ).grid(row = 1, column = 1, sticky = W)
    
            self.mustard = BooleanVar()
            Checkbutton(self,
                        text = "Mustard",
                        variable = self.mustard
                        ).grid(row = 2, column = 1, sticky = W)
    
            self.mayo = BooleanVar()
            Checkbutton(self,
                        text = "Mayo",
                        variable = self.mayo
                        ).grid(row = 3, column = 1, sticky = W)
    
            self.ketchup = BooleanVar()
            Checkbutton(self,
                        text = "Ketchup",
                        variable = self.ketchup
                        ).grid(row = 4, column = 1, sticky = W)
    
    
            # bun choice using radio buttons
            Label(self,
                  text = "Choose your bun:"
                  ).grid(row = 1, column = 2, sticky = W)
    
            self.bun_type = StringVar()
            self.bun_type.set(None)
    
            Radiobutton(self,
                        text = "White",
                        variable = self.bun_type,
                        value = "white"
                        ).grid(row = 2, column = 2, sticky = W)
            Radiobutton(self,
                        text = "Wheat",
                        variable = self.bun_type,
                        value = "wheat"
                        ).grid(row = 3, column = 2, sticky = W)
    
    
            # submit button
            Button(self,
                   text = "Make my burger!",
                   command = self.compile_order
                   ).grid(row = 5, column = 1, sticky = W)
    
    
            # results text box
            Label(self,
                  text = "Burger Ordered:"
                  ).grid(row = 7, column = 0, sticky = W)
            self.order_txt = Text(self, width = 50, height = 5, wrap = WORD)
            self.order_txt.grid(row = 8, column = 0, columnspan = 3)
            
        # create message to show to user
        def compile_order(self):
            message = "Thanks for your order!\n"
            message += "Looks like you ordered a hamburger"
    
            # check which checkbuttons are selected, append those to toppings list
            if self.cheese.get() == True:
                self.toppings.append("cheese")
            if self.lettuce.get() == True:
                self.toppings.append("lettuce")
            if self.onion.get() == True:
                self.toppings.append("onion")
            if self.tomato.get() == True:
                self.toppings.append("tomato")
            if self.pickles.get() == True:
                self.toppings.append("pickles")
            if self.mustard.get() == True:
                self.toppings.append("mustard")
            if self.mayo.get() == True:
                self.toppings.append("mayo")
            if self.ketchup.get() == True:
                self.toppings.append("ketchup")
    
            # find out how many toppings the user wants
            num = len(self.toppings)
    
            # use num to correctly format the list
            if num == 1:
                message += " with " + str(self.toppings[0])
            elif num == 2:
                message += " with " + str(self.toppings[0]) + " and " + str(self.toppings[1])
            elif num > 2:
                message += " with "
                for topping in self.toppings:
                    num -= 1
                    if num == 0:
                        message += "and " + str(topping)
                    else:
                        message += str(topping) + ", "
            
            # reset the list to hold no values
            self.toppings = []
    
            # for those who are on a lo-carb diet
            if self.bun_type.get() == "None":
                message += " with no bun."
            else:
                message += " on a " + str(self.bun_type.get()) + " bun."
    
            # show user the order
            self.order_txt.delete(0.0, END)
            self.order_txt.insert(0.0, message)
    
    # main
    root = Tk()
    root.title("Burger Order")
    root.geometry("410x240")
    
    app = Application(root)
    
    root.mainloop()
    Woo!

IMN logo majestic logo threadwatch logo seochat tools logo