我大学有一个python作业要做,但我有一个问题。那个赋值说明我需要创建复选框按钮,该按钮的值在我按下一个按钮后将总计。我已经设法做到了。我必须对单选按钮做同样的事情,但我现在遇到了一个问题。如果我创建了单选按钮,它们起作用,但我不知道如何给它们赋值,然后在我的总汇中使用这个值。在
“在此处输入代码”菜单程序帮助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()