I'm just playing around with basic GUI code. I have created two check boxes, and a text field below them. I want it to say, "I like Pizza", 'I like Pasta", "I like nothing", or "I like Pizza and Pasta" in the text field depending upon which checkboxes are ticked.

This is the code for creating the checkboxes and text field:

#Create the pizza check box
        self.likes_pizza = BooleanVar()
        Checkbutton(self, text = "Pizza", variable = self.likes_pizza, command = self.updatePizzaPasta).grid(row = 5, column = 0)

        #Create the pasta check box
        self.likes_pasta = BooleanVar()
        Checkbutton(self, text = "Pasta", variable = self.likes_pasta, command = self.updatePizzaPasta).grid(row = 5, column = 1)

        #Create the pizza and pasta text field
        self.pp_txt = Text(self, width = 30, height = 1, wrap=WORD)
        self.pp_txt.grid(row = 7, column = 0, columnspan = 2)

This is my attempt at what goes into the function:

def updatePizzaPasta(self):
        self.pp_txt = ""
        if self.likes_pizza.get():
            self.pp_txt = "You like Pizza"

Thank you

I generally use a dictionary to store the buttons, but in any case you pass the button number to a callback bound to the specific button. You should also use one function to create a button, and pass the button's text, etc. to the function. Note that this does not use a grid, but you should get the idea.

def one_button(txt, var):
      b = Checkbutton(self.frame, text = txt, variable=var)
      b.pack()
      def handler ( event, self=self, button_num=self.button_num ):
          return self.cb_handler( event, button_num )
      b.bind ( "<Button-1>", handler )
      self.button_num += 1

      return b

   ##-------------------------------------------------------------------
   def cb_handler( self, event, cb_number ):
       print cb_number, "button pressed"

I'm not sure I quite understand this. I only want a function to display text in the field depending upon what is checked. Is there anyway to do this within the function using if/elif/elif/else/

Thank you

I'm not sure I quite understand this. I only want a function to display text in the field depending upon what is checked. Is there anyway to do this within the function using if/elif/elif/else/

Thank you

What you display would depend on the button number sent to the function. A convenient way would use a dictionary with the button number as key pointing to the text, so it would be
self.pp_txt = desc_dict[cb_number]

If you run the code, you will see that a different number prints for each different button checked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.