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

2024-04-18 20:17:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: thetextselfinfovaluemaintkintervar
2条回答

calculate()方法修改dayVal、eveVal和off-peak的局部变量。这可能不是你想要的。你知道吗

这就解决了问题。问题是您试图操纵全局变量。一旦您设置了一个全局变量,它通常不受更改的影响(详见下文)。为了解决这个问题,我把你的全局变量改为类属性。你知道吗

另外,我要进一步提到的是,你们的全球指标可能会发生更精确的变化。您可以使用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()

相关问题 更多 >