我想创建一个tkinter Python按钮,当我按下一个按钮时,它会激活多个命令

时间:2013-12-01 20:05:56

标签: python user-interface tkinter

我想要一个激活两个功能的按钮来计算一个属性的税收评估值,然后点击一下按钮就可以对该属性的评估值征税。

按钮应该说'计算'并且应该激活.6 *属性值函数和.0064 *评估值。

import tkinter

class PropertyTaxGUI:

    def __init__(self):

        self.main_window = tkinter.Tk()

        self.top_frame = tkinter.Frame()
        self.mid_frame = tkinter.Frame()
        self.third_frame = tkinter.Frame()
        self.bottom_frame = tkinter.Frame()

        self.prompt_label = tkinter.Label(self.top_frame, \
                                    text='Enter the property value: $')
        self.property_entry = tkinter.Entry(self.top_frame, \
                                             width=10)

        self.prompt_label.pack(side='left')
        self.property_entry.pack(side ='right')        

        self.calc_button = tkinter.Button(self.bottom_frame, \
                                          text = 'Calculate', \
                                          command = self.calculate)
        self.quit_button = tkinter.Button(self.bottom_frame, \
                                          text = 'Quit', \
                                          command = self.main_window.destroy)

        self.value = tkinter.StringVar()

        self.calc_button.pack(side='right')
        self.quit_button.pack(side='right')

        self.assess_label= tkinter.Label(self.mid_frame, \
                                        text='Assessment Value: ')


        self.assess_label.pack(side='left')

        self.value_label = tkinter.Label(self.mid_frame, \
                                         textvariable=self.value)

        self.value_label.pack(side='right')

        self.prop_label= tkinter.Label(self.third_frame, \
                                        text='Property Tax: ')


        self.prop_label.pack(side='left')

        self.propTax_label = tkinter.Label(self.mid_frame, \
                                         textvariable=self.value)

        self.propTax_label.pack(side='right')

        self.top_frame.pack()
        self.mid_frame.pack()
        self.third_frame.pack()
        self.bottom_frame.pack()

        tkinter.mainloop()

    def calculate(self):

        propVal = float(self.property_entry.get())

        assessVal = str(format(float(((.6)*propVal)), '.2f'))

        self.value.set(assessVal)

    def taxCalculate(self):

        propVal = float(self.property_entry.get())

        assessVal = str(format(float(((.6)*propVal)), '.2f'))

        assessTax = str(format(float(((.64)*assessVal)), '.2f'))

        self.value.set(assessTax)

my_gui = PropertyTaxGUI()

1 个答案:

答案 0 :(得分:1)

def __init__(self):
    ...
    self.calc_button = tkinter.Button(self.bottom_frame, \
                                              text = 'Calculate', \
                                              command = self.calc_button_event)
    ...

def calc_button_event(self):
    self.taxCalculate()
    self.calculate()

还会改变taxCalculate中的行:

assessVal = str(format(float(((.6)*propVal)), '.2f'))

为:

assessVal = propVal*0.6