我的任务是使用单选按钮创建一个程序,该程序将显示在一天的长途通话中的某些时间的费用。在白天,每分钟是0.07,晚上是每分钟0.12,非高峰是每分钟0.05。出于某种原因,我似乎无法弄清楚如何将通话时间乘以特定的时段费用并输出答案。

到目前为止,这是我的代码,输出为$ 0:

import tkinter
import tkinter.messagebox

def main():
    my_gui = TelephoneRatesGUI()

class TelephoneRatesGUI():

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

        #create 3 frames
        self.top_frame = tkinter.Frame(self.main_window)
        self.mid_frame = tkinter.Frame(self.main_window)
        self.bottom_frame = tkinter.Frame(self.main_window)

        #create an intvar object
        self.radio_var = tkinter.IntVar()

        #set the intVar object to 1.
        self.radio_var.set(1)

        #create the radiobutton widgets in the top_frame
        self.rb1 = tkinter.Radiobutton(self.top_frame,
                                       text='Daytime(6:00am - 5:59pm)',
                                       variable=self.radio_var,
                                       value=.07)
        self.rb2 = tkinter.Radiobutton(self.top_frame,
                                       text='Evening(6:00pm - 11:59pm)',
                                       variable=self.radio_var,
                                       value=.12)
        self.rb3 = tkinter.Radiobutton(self.top_frame,
                                       text='Off-Peak(12:00am - 5:59am)',
                                       variable=self.radio_var,
                                       value=.05)

        #pack the radiobuttons
        self.rb1.pack()
        self.rb2.pack()
        self.rb3.pack()

        #create widgets for the middle frame
        self.prompt_label = tkinter.Label(self.mid_frame,
                                          text='Enter the number of minutes:')
        self.num_min = tkinter.Entry(self.mid_frame,
                                     width=10)

        #pack the mid frame's widgets
        self.prompt_label.pack(side='left')
        self.num_min.pack(side='left')

        #create a display charges button and a quit button
        self.dis_button = tkinter.Button(self.bottom_frame,
                                         text='Display Charges',
                                         command=self.show_charges)
        self.quit_button = tkinter.Button(self.bottom_frame,
                                          text='Quit',
                                          command=self.main_window.destroy)

        #pack the buttons
        self.dis_button.pack(side='left')
        self.quit_button.pack(side='left')

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

        #start the mainloop
        tkinter.mainloop()

    def show_charges(self):
        charges = float(self.radio_var.get())

        minutes = float(self.num_min.get())

        tkinter.messagebox.showinfo('Total Charges', 'Your total charges = $' +
                                    format(float(charges*minutes)))

main()

最佳答案

您需要将self.radio_var更改为tkinter.DoubleVar。我进行了以下更改,并使程序正常工作:

self.radio_var = tkinter.DoubleVar()


注意:您无法自行解决此问题,这表明您没有查看程序的控制台输出。下次,请从控制台运行程序,以便可以看到异常回溯。

更新:在注释中,您说您正在控制台中运行程序,但是没有看到错误。当我运行您的程序时,我得到了以下追溯:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
    return self.func(*args)
  File "old.py", line 73, in show_charges
    charges = float(self.radio_var.get())
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 355, in get
    return self._tk.getint(self._tk.globalgetvar(self._name))
TypeError: getint() argument must be str, not float


如果您没有在原始代码中看到这种类型的输出,那么我很难相信您知道我说的“从控制台运行程序”时的意思。

10-07 15:37