显示单选按钮编号而不是计算成本

时间:2015-05-10 00:59:26

标签: python tkinter

我的程序应该计算电话费用。它运行但不计算答案,但显示单选按钮的编号。

# Import tkinter
import tkinter
import tkinter.messagebox

dayVal = 0
eveVal = 0
off_peak = 0

# Create the class
class MyGUI:
    def __init__(self):

        # Create the main window.
        self.main_window = tkinter.Tk()

        # Create the variable objects to display
        self.rb1_value = tkinter.StringVar()
        self.rb2_value = tkinter.StringVar()
        self.rb3_value = tkinter.StringVar()

        # Create two frames. One for the Radiobuttons
        # and another for the regular Button widgets.
        self.info_frame = tkinter.Frame(self.main_window)
        self.button_frame = tkinter.Frame(self.main_window)
        self.fourth_frame = tkinter.Frame(self.main_window)

        # Create the label for the promt frame.
        self.prompt_label = tkinter.Label(self.fourth_frame, \
                                    text='Enter the length of the call (in minutes):')

        # Create the entry box for the top frame.
        self.minute_entry = tkinter.Entry(self.fourth_frame, \
                                             width=10)

        # Placement of the top label, and the entry box.
        self.prompt_label.pack(side='left')
        self.minute_entry.pack(side ='right') 

        # Create an IntVar object to use with the Radiobuttons.
        self.radio_var = tkinter.IntVar()

        # Set the intVar object.
        self.radio_var.set(dayVal)
        self.radio_var.set(eveVal)
        self.radio_var.set(off_peak)

        # Create the Radiobutton widgets in the top_frame.
        self.rb1 = tkinter.Radiobutton(self.info_frame, \
                                       text='Daytime (6:00 am - 5:59 pm), $0.07/minute',
                                       variable=self.radio_var, \
                                       value=1)
        self.rb2 = tkinter.Radiobutton(self.info_frame, \
                                       text='Evening (6:00 pm - 11:59 pm), $0.12/minute',
                                       variable=self.radio_var, \
                                       value=2)
        self.rb3 = tkinter.Radiobutton(self.info_frame, \
                                       text='Off-Peak (midnight - 5:59 pm), $0.05/minute',
                                       variable=self.radio_var, \
                                       value=3)
        # Pack the Radio Buttons
        self.rb1.pack()
        self.rb2.pack()
        self.rb3.pack()

        # Create a Calculate cost button and a Quit button
        self.show_info_button = tkinter.Button(self.button_frame, \
                                        text='Calculate cost', command=self.show)
        self.quit_button = tkinter.Button(self.button_frame, \
                                          text='Quit', command=self.main_window.destroy)

        # Pack the Buttons
        self.show_info_button.pack(side='left')
        self.quit_button.pack(side='right')

        # Pack the frames
        self.info_frame.pack()
        self.fourth_frame.pack()
        self.button_frame.pack()

        # Start the mainloop
        tkinter.mainloop()

    def calculate(self):
        lengthVal = float(self.minute_entry.get())

        dayVal = format(lengthVal * .07, ',.2f')
        eveVal = format(lengthVal * .12, ',.2f')
        off_peak = format(lengthVal * .05, ',.2f')

    # The do something method is the callback function for the Calculate button.

    def show(self):
        tkinter.messagebox.showinfo('The cost of the call is: ', +\
                                    float(self.radio_var.get()))

# Create an instance of the MyGUI class
my_gui = MyGUI()

2 个答案:

答案 0 :(得分:1)

这解决了它。问题是你试图操纵全局变量。一旦设置了全局变量,它通常不会发生变化(更多信息如下)。为了解决这个问题,我将你的全局变为类属性。

另外,我应该进一步提到你的全局变量可以更精确。您可以使用global命令来更改它们,但由于您已经在使用类属性,因此在这里更合适。通常,全局变量设置为在整个程序中保持不变的事物。

此外,我稍微更改了calculate功能,以检查用户当前设置的单选按钮,然后检查所有三个常用cost变量。根据选择的内容,它将根据您设置的成本因素计算呼叫成本。另外,我应该提到format在这里有点多余(imo)。

 import tkinter
 import tkinter.messagebox

 # Create the class
 class MyGUI:
     def __init__(self):

        # Create the main window.
        self.main_window = tkinter.Tk()
        self.main_window.title("Call Calculator")

        # Create the variable objects to display
        self.rb1_value = tkinter.StringVar()
        self.rb2_value = tkinter.StringVar()
        self.rb3_value = tkinter.StringVar()

        # Create two frames. One for the Radiobuttons
        # and another for the regular Button widgets.
        self.info_frame = tkinter.Frame(self.main_window)
        self.button_frame = tkinter.Frame(self.main_window)
        self.fourth_frame = tkinter.Frame(self.main_window)

        # Create the label for the promt frame.
        self.prompt_label = tkinter.Label(self.fourth_frame, \
                                          text='Enter the length of the call (in minutes):')

        # Create the entry box for the top frame.
        self.minute_entry = tkinter.Entry(self.fourth_frame, \
                                          width=10)

        # Placement of the top label, and the entry box.
        self.prompt_label.pack(side='left')
        self.minute_entry.pack(side ='right') 

        # Create an IntVar object to use with the Radiobuttons.
        self.radio_var = tkinter.IntVar()

        #make globals class attributes
        self.dayVal = 0
        self.eveVal = 0 
        self.off_peak = 0

       # Set the intVar object.
       self.radio_var.set(self.dayVal)
       self.radio_var.set(self.eveVal)
       self.radio_var.set(self.off_peak)

       # Create the Radiobutton widgets in the top_frame.
       self.rb1 = tkinter.Radiobutton(self.info_frame, \
                                       text='Daytime (6:00 am - 5:59 pm), $0.07/minute',
                                   variable=self.radio_var, \
                                       value=1)
       self.rb2 = tkinter.Radiobutton(self.info_frame, \
                                       text='Evening (6:00 pm - 11:59 pm), $0.12/minute',
                                   variable=self.radio_var, \
                                       value=2)
       self.rb3 = tkinter.Radiobutton(self.info_frame, \
                                       text='Off-Peak (midnight - 5:59 pm), $0.05/minute',
                                   variable=self.radio_var, \
                                       value=3)
       # Pack the Radio Buttons
       self.rb1.pack()
       self.rb2.pack()
       self.rb3.pack()

       # Create a Calculate cost button and a Quit button
       self.show_info_button = tkinter.Button(self.button_frame, \
                                               text='Calculate cost', command=self.show)
       self.quit_button = tkinter.Button(self.button_frame, \
                                          text='Quit', command=self.main_window.destroy)

       # Pack the Buttons
       self.show_info_button.pack(side='left')
       self.quit_button.pack(side='right')

       # Pack the frames
       self.info_frame.pack()
       self.fourth_frame.pack()
       self.button_frame.pack()



       # Start the mainloop
       tkinter.mainloop()

     def calculate(self):
         lengthVal = float(self.minute_entry.get())
         sel_radio = self.radio_var.get()
         if sel_radio == 1:
             cost = format(lengthVal * .07, ',.2f')
         elif self_radio == 2:
             cost = format(lengthVal * .12, ',.2f')
         else:
             cost =  format(lengthVal * .05, ',.2f')
         return cost

    # The do something method is the callback function for the Calculate button.

     def show(self):
         cost = self.calculate()
         tkinter.messagebox.showinfo('Cost of Call:', "Your call has costed the following amount in USD\n\t\t$ %s" % cost)

 # Create an instance of the MyGUI class
 my_gui = MyGUI()

答案 1 :(得分:0)

您的calculate()方法修改dayVal,eveVal和off_peak的局部变量。这可能不是你想要的。