Search code examples
pythontkintertkinter-entry

Issue with messagebox tkinter


basically my problem is with the self.asses_val button widget. The button is being automatically pressed. I have a try/except statement that prevents it from crashing.

Why is the message box automatically running before any values are inputted into the Entry widget?

If I don't have the try/except statement, I get this error:

val = float(self.property_value_entry.get()) ValueError: could not convert string to float:

I am interpreting this as the value from the entry widget does not have a chance to get a number input and ends up returning a blank " " string input because nothing has been entered!!! What is going on?

import tkinter
import tkinter.messagebox


class taxGUI:
    def __init__(self):
        self.main_window = tkinter.Tk()

        # initialize frames
        self.top_frame = tkinter.Frame(self.main_window)
        self.mid_frame = tkinter.Frame(self.main_window)

        # top frame widgets
        self.property_value = tkinter.Label(self.top_frame, text='Enter Property Value')
        self.property_value_entry = tkinter.Entry(self.top_frame, width=10)

        # pack top_frame widgets
        self.property_value.pack(side='left')
        self.property_value_entry.pack(side='right')

        # mid frame widgets
        self.asses_val = tkinter.Button(self.mid_frame, text='Assesment Value', command=self.getAssessment())
        self.tax_val = tkinter.Button(self.mid_frame, text='Tax Value')
        self.quit = tkinter.Button(self.mid_frame, text='Quit', command=self.main_window.destroy)

        # pack mid frame widgets
        self.asses_val.pack(side='left')
        self.tax_val.pack(side='left')
        self.quit.pack(side='right')

        # pack frames
        self.top_frame.pack()
        self.mid_frame.pack()


        tkinter.mainloop()

    def getAssessment(self):
        try:
            val = float(self.property_value_entry.get())
        except ValueError:
            val = 0.00
        assessment = val * 0.60
        tkinter.messagebox.showinfo('The property value is assessed at ' + str(assessment) + ' dollars.')

    def getTaxes(self):
        val = float(self.property_value_entry.get())
        tax = val * 0.75
        tkinter.messagebox.showinfo('The tax on this property is ' + str(tax) + ' dollars.')


# instantiate GUI
t = taxGUI()

Solution

  • In the line 22 within self.asses_val, change command=self.getAssessment() to command=self.getAssessment and it will work.

    The code would become:

    import tkinter
    import tkinter.messagebox
    
    
    class taxGUI:
        def __init__(self):
            self.main_window = tkinter.Tk()
    
            # initialize frames
            self.top_frame = tkinter.Frame(self.main_window)
            self.mid_frame = tkinter.Frame(self.main_window)
    
            # top frame widgets
            self.property_value = tkinter.Label(self.top_frame, text='Enter Property Value')
            self.property_value_entry = tkinter.Entry(self.top_frame, width=10)
    
            # pack top_frame widgets
            self.property_value.pack(side='left')
            self.property_value_entry.pack(side='right')
    
            # mid frame widgets
            self.asses_val = tkinter.Button(self.mid_frame, text='Assesment Value', command=self.getAssessment)
            # _______________________________________________________________________________________________^
            self.tax_val = tkinter.Button(self.mid_frame, text='Tax Value', command=self.getTaxes)
            self.quit = tkinter.Button(self.mid_frame, text='Quit', command=self.main_window.destroy)
    
            # pack mid frame widgets
            self.asses_val.pack(side='left')
            self.tax_val.pack(side='left')
            self.quit.pack(side='right')
    
            # pack frames
            self.top_frame.pack()
            self.mid_frame.pack()
    
    
            tkinter.mainloop()
    
        def getTaxes(self):
            val = float(self.property_value_entry.get())
            tax = val * 0.75
            tkinter.messagebox.showinfo('The tax on this property is ' + str(tax) + ' dollars.')
    
        def getAssessment(self):
            val = float(self.property_value_entry.get())
            assessment = val * 0.60
            tkinter.messagebox.showinfo('The property value is assessed at ' + str(assessment) + ' dollars.')
    
    
    # instantiate GUI
    t = taxGUI()