消息框tkinter的问题

时间:2019-04-27 00:25:04

标签: python tkinter tkinter-entry

基本上,我的问题是使用self.asses_val按钮小部件。该按钮被自动按下。我有一个try / except语句来防止崩溃。

为什么在将任何值输入到Entry小部件之前,消息框会自动运行?

如果我没有try / except语句,则会收到此错误:

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

我将其解释为输入小部件中的值没有机会获得数字输入,并且由于未输入任何内容而最终返回了空白的字符串输入!!!发生了什么事?

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()

1 个答案:

答案 0 :(得分:1)

self.asses_val的第22行中,将command=self.getAssessment()更改为command=self.getAssessment,它将起作用。

代码将变为:

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()