0

我有一个要为大学做的 python 作业,但我遇到了问题。作业指出我需要创建复选框按钮,该按钮的值将在我按下按钮后总计。这是我设法做到的。我必须对单选按钮做同样的事情,在这里我遇到了问题。如果我创建它们工作的单选按钮,但我不知道如何为它们分配一个值,然后在我的 gandTotal 中使用这个值。

`enter code here``#帮助菜单程序

import random

from Tkinter import *

class App(Frame):
    #GUI application
    def __init__(self,master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        #Create all the widgets here
        #Label to welcome the user
        Label(self,text = "Welcome to Awesome Reasturant:"
            ).grid(row=1,column=0,sticky=W)
        #..........your code goes here

        #Labels to request the name of the customer
        Label(self,text = "Customer Name:"
            ).grid(row=2,column=0,sticky=W)
        self.CustomerName_ent = Entry(self)
        self.CustomerName_ent.grid(row = 2, column = 1, sticky = W)

        #Labels to request the telephone number
        Label(self,text = "Telephone Number:"
            ).grid(row=3,column=0,sticky=W)
        self.Telephone_ent = Entry(self)
        self.Telephone_ent.grid(row = 3, column = 1, sticky = W)

        #Label to generate a random order number - Nees work
        Label(self,text = "Random Order Number:"
           ).grid(row=3,column=2,sticky=W)
        #self.Random_ent = RandomNumber(random.randrange(100))

         # Create a label for Starter Menue
        Label(self,
              text = "Select from starter Menue @R55:"
              ).grid(row = 7, column = 0, sticky = W)

        # create variable for single starter menue item
        self.Starter_menue = StringVar()

        #create starter menue radio buttons
        #Starter_menue = ["Prawn and Muchroom cocktail", "9 Oysters", "Goats cheese"]
        #column = 0
        #for part in Starter_menue:
         #   Radiobutton(self,
          #              text = part,
           #             variable = self.Starter_menue,
            #            value = part
             #           ).grid(row = 9, column = column, sticky = W)
            #column += 1

        #create radion for Starter item menue 1
        Radiobutton(self,text="Prawn and Mushroom",
                    variable = self.Starter.menue, value = 55, command =self.determine_order
                    ).grid(row=9, column =1, sticky = W)
         # Create a label for Starter Menue
        Label(self,
              text = "Select from Mains Menue @R100:"
              ).grid(row = 10, column = 0, sticky = W)

        # create variable for single mains menue item
        self.Mains_menue = StringVar()



         #create main course radio buttons
        Mains_menue = ["250g Fillet", "Beef Schnitzel", "Chicken", "Quails", "Ravioli",         "Genooch"]
        column = 0
        for part in Mains_menue:
            Radiobutton(self,
                        text = part,
                        variable = self.Mains_menue,
                        value = part
                        ).grid(row = 11, column = column, sticky = W)
            column += 1

        #MENU items to choose from
        #I'll do one example with a check button

        #the salads
        Label(self,text = "Select from Salads"
            ).grid(row=4,column=0,sticky=W)
        self.has_green = BooleanVar()
        self.has_blue = BooleanVar()
        self.has_ceaser = BooleanVar()


        #the greens
        Checkbutton(self, text="Greens",
                    variable=self.has_green
                    ).grid(row=5,column=0,sticky=W)

        #the blue salad
        Checkbutton(self, text="Blue",
                    variable=self.has_blue
                    ).grid(row=5,column=1,sticky=W)

        #The Ceaser Salad
        Checkbutton(self,text="Ceaser Salad",
                    variable=self.has_ceaser
                    ).grid(row=5,column=2,sticky=W)


        #Place the order
        Button(self,text = "Place the Order",command = self.determine_order, bg="gold").grid(row = 14, column = 0, sticky = W)
        #The display area
        self.txt_order = Text(self, width = 75, height = 5, wrap = WORD)
        self.txt_order.grid(row = 15, column = 0, columnspan = 4)
    def RandomNumber():
        RandomNumber = random.randrange(100)
    def determine_order(self):
            #Get all the information from the GUI
            totalprice = 0
            totalMain =0
            totalStarter =0
            grandTotal = totalMain + totalStarter + totalprice
            greensaladprice=40
            bluesaladprice=40
            ceasersaladprice=40
            CustomerName =self.CustomerName_ent.get()
            Telephone=self.Telephone_ent.get()
            Starter_menue = self.Starter_menue.get()
            Starter1 = 55
            Starter2= 55
            Starter3=55


            #Check if and which salads are selected
            if self.has_green.get():
                totalprice = totalprice + greensaladprice
            if self.has_blue.get():
                totalprice = totalprice + bluesaladprice
            if self.has_ceaser.get():
                totalprice = totalprice + ceasersaladprice

            #Check if and which Starters are selected

            if self.Starter_menue(0): 
                totalStarter = totalStarter + Starter1
            if self.Starter_menue(1):
                totalStarter = totalStarter + Starter2
            if self.Starter.get():
                totalStarter = totalStarter + Starter3

            #Display the total

            self.txt_order.delete(0.0,END)
            self.txt_order.insert(0.0,float(grandTotal))

            # Add the name -Needs work
            self.txt_order.insert(100.50,CustomerName)
            #Add telephone number - Needs Work
            self.txt_order.insert(0.0,Telephone)
def main():
    root = Tk()
    root.title("Example - menu 1")
    app = App(root)

    root.mainloop()


main()
4

1 回答 1

0

首先,您是否在示例中遗漏了一些代码?可能是 App 类中的 Starter 初始化?

如果我尝试运行,我会得到回溯:

Traceback (most recent call last):
  File "test.py", line 165, in <module>
    main()
  File "test.py", line 160, in main
    app = App(root)
  File "test.py", line 10, in __init__
    self.create_widgets()
  File "test.py", line 57, in create_widgets
    variable = self.Starter.menue, value = 55, command =self.determine_order
AttributeError: App instance has no attribute 'Starter'
于 2013-09-07T15:44:51.143 回答