0

This is a part of my GUI program that I'm having a problem with and hoping someone could help me. What I'm trying to do is, when you click the check button, it should show the price on the text widget but when I click the check button it gives me an error:

File "E:\Phython\Theater.py", line 147, in update_text if self.matinee_price.get(): AttributeError: 'Checkbutton' object has no attribute 'get'

def matinee_pricing(self):
    #Purchase Label
    self.theater_label = tkinter.Label(text='Purchase Theater Seats Here', font=('Verdana', 15, 'bold'))
    self.theater_label.grid(row=2, column=10)
    #Checkbutton
    self.matinee_price = BooleanVar()
    self.matinee_price = tkinter.Checkbutton(text = '101 \nthru \n105', font=('Verdana', 10), bg='light pink', height=5, width=10,\
                                             variable = self.matinee_price, command = self.update_text)
    self.matinee_price.grid(row=5, column=9)

    self.result = tkinter.Text(width=10, height=1, wrap = WORD)
    self.result.grid(row=20, column=10)

def update_text(self):
    price = ''

    if self.matinee_price.get():
        price += '$50'

    self.result.delete(0.0, END)
    self.result.insert(0.0, price)
Jek
  • 3
  • 2
  • You really shouldn't import tkinter twice like that, it's bad practice. See [this](https://stackoverflow.com/questions/47479965/is-there-a-point-to-import-two-different-ways-in-a-program) for more info. – Nae Dec 03 '17 at 14:10

1 Answers1

0

You're overwriting the boolean variable with the checkbox itself. The declaration of your BoolenaVar needs a different name, and your other function needs to check that name.

Also, checkboxes use 0 and 1 by default to describe their state. If you want to use a boolean you need to change onvalue and offvalue accordingly.

def matinee_pricing(self):
    # [...]
    self.matinee_price_var = BooleanVar() # Different name for the variable.
    self.matinee_price = tkinter.Checkbutton(text = '101 \nthru \n105', font=('Verdana', 10), bg='light pink', height=5, width=10,\
                                             variable = self.matinee_price_var, onvalue=True, offvalue=False, command = self.update_text)
    # Add onvalue, offvalue to explicitly set boolean states.
    # [...]

def update_text(self):
    price = ''

    # Use the variable, not the checkbox
    if self.matinee_price_var.get(): 
        price += '$50'
    #[...]

P.S.: You don't need the \ to break lines inside a bracket. Python assumes everything inside the brackets is part of the preceeding command, in this case the checkbox. Just make sure you're not getting any syntax errors and don't split individual arguments across lines.

ikom
  • 166
  • 6
  • 1
    Don't you also need to change `variable = self.matinee_price` to `variable = self.matinee_price_var`? – Nae Dec 03 '17 at 14:02
  • Yes, of course, I forgot to change that one. Thank you for the correction. – ikom Dec 03 '17 at 14:46
  • Thanks!! Works fine now. I'm new to python and this class that I'm taking is more of a workshop than a lecture so Internet/Youtube is my only source. – Jek Dec 04 '17 at 05:30