GUI程序计算#2

时间:2015-07-07 02:03:30

标签: python python-3.x tkinter

我应该编写一个带有检查按钮的程序,允许用户选择任何或所有这些服务。当用户单击按钮时,应显示总费用。我已完成第一部分并完成,但我需要第二部分的帮助。当用户点击按钮并计算总费用并在同一个框中显示时,我找不到有效的计算方法。添加所选项的正确计算是什么?这是我到目前为止所做的:

#Create the checkbutton widgets in top frame.

        self.cb1 = tkinter.Checkbutton(self.top_frame, \
                    text = 'Oil Change-$30.00', variable = self.cb_var1)
        self.cb2 = tkinter.Checkbutton(self.top_frame, \
                    text = 'Lube Job-$20.00', variable = self.cb_var2)
        self.cb3 = tkinter.Checkbutton(self.top_frame, \
                    text = 'Radiator Flush-$40.00', variable = self.cb_var3)
        self.cb4 = tkinter.Checkbutton(self.top_frame, \
                    text = 'Transmission Flush-$100.00', variable = self.cb_var4)
        self.cb5 = tkinter.Checkbutton(self.top_frame, \
                    text = 'Inspection-$35.00', variable = self.cb_var5)
        self.cb6 = tkinter.Checkbutton(self.top_frame, \
                    text = 'Muffler Replacement-$200.00', variable = self.cb_var6)
        self.cb7 = tkinter.Checkbutton(self.top_frame, \
                    text = 'Tire Rotation-$20.00', variable = self.cb_var7)

#Pack the checkbuttons.

        self.cb1.pack()
        self.cb2.pack()
        self.cb3.pack()
        self.cb4.pack()
        self.cb5.pack()
        self.cb6.pack()
        self.cb7.pack()

#Create an OK button and Quit button.

        self.ok_button = tkinter.Button(self.bottom_frame, \
                    text = 'OK', command = self.show_choice)
        self.quit_button = tkinter.Button(self.bottom_frame, \
                    text = 'Quit', command = self.main_window.destroy)

#Pack the buttons.

        self.ok_button.pack(side = 'left')
        self.quit_button.pack(side = 'left')

#Pack frame.

        self.top_frame.pack()
        self.bottom_frame.pack()

2 个答案:

答案 0 :(得分:0)

我不是一个tkinter人,但我会这样做,好像我在wxpython中制作它,你只需要从那里翻译它。

首先,我会为所有复选框名称和与之相关的价格制作一本字典。它看起来像这样:

priceCheckUp = {"Oil Change-$30.00":30, "Lube Job-$20.00":20...} #Keep going with all the rest of them

接下来,您需要所有复选框的列表。我会更改您的代码,将复选框附加到列表中,如:

checkboxes = []
checkboxes.append(tkinter.Checkbutton(self.top_frame, \
                    text = 'Oil Change-$30.00', variable = self.cb_var1)) #Do that for all of them

现在你有一个列表,你可以在用户按下按钮后使用这样的for循环:

total = 0
for i in checkboxes:
   if i.isChecked():
      total = total + priceCheckUp[i.GetLabel()]
print total #Or display the data however you want

上面的代码是为wxPython Checkbox所做的,所以你必须做一些翻译。

答案 1 :(得分:0)

这是我在评论中所说的内容的实现。虽然它是基于你的答案中的代码,但我添加了很多支持代码,所以我可以测试它。我做的一个重大改变是将所有的Checkbutton小部件及其相关的控制变量放入列表中,而不是明确地给每个小部件一个唯一的名称 - 这就是你手头有很多这样的名字。

也就是说,检查每个Checkbutton的状态并将show_choice()方法中显示的已检查项目的成本加起来。

import tkinter

# Table of services containing their names and costs.
SERVICES = [('Oil Change', 30.00),
            ('Lube Job', 20.00),
            ('Radiator Flush', 40.00),
            ('Transmission Flush', 100.00),
            ('Inspection', 35.00),
            ('Muffler Replacement', 200.00),
            ('Tire Rotation', 20.00)]

class MyApp(tkinter.Frame):
    def __init__(self, master=None):
        tkinter.Frame.__init__(self, master)
        self.pack()

        self.main_window = root
        self.top_frame = tkinter.Frame(self)
        self.bottom_frame = tkinter.Frame(self)

        # Create list of control variables to query state of CheckButtons.
        self.cb_vars = [tkinter.IntVar() for _ in range(len(SERVICES))]

        # Create another list to hold a corresponding Checkbuttons.
        self.cbs = [
            tkinter.Checkbutton(
                self.top_frame,
                text='{}-${:.2f}'.format(SERVICES[i][0], SERVICES[i][1]),
                variable=self.cb_vars[i])
            for i in range(len(self.cb_vars))
        ]
        # Pack the Checkbuttons.
        for i in range(len(self.cbs)):
            self.cbs[i].pack()

        #Create an OK button and Quit button.
        self.ok_button = tkinter.Button(self.bottom_frame,
                    text='OK', command=self.show_choice)
        self.quit_button = tkinter.Button(self.bottom_frame,
                    text='Quit', command=self.main_window.destroy)

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

        #Pack frames.
        self.top_frame.pack()
        self.bottom_frame.pack()

    def show_choice(self):
        popup_window = tkinter.Toplevel()
        label_frame = tkinter.LabelFrame(popup_window, text='Total Charges')
        label_frame.pack()
        # Add up the cost of all the items which are checked.
        total = sum(SERVICES[i][1] for i in range(len(self.cb_vars))
                        if self.cb_vars[i].get())
        tkinter.Label(label_frame, text='${:.2f}'.format(total)).pack()
        # Enter a local mainloop and wait for window to be closed by user.
        root.wait_window(popup_window)

root = tkinter.Tk()
app = MyApp(root)
app.master.title('Cost Calculator')
app.mainloop()

以下是它运行的截图:

screenshot of code running