Python 如何获取Tkinter中单选按钮值的总和?
我的单选按钮有以下代码,这是我的tkinter全菜单编程项目的代码:Python 如何获取Tkinter中单选按钮值的总和?,python,user-interface,tkinter,Python,User Interface,Tkinter,我的单选按钮有以下代码,这是我的tkinter全菜单编程项目的代码: from tkinter import * from time import sleep class SchoolCampMenuGUI: def __init__(self,parent): #------------------------------Layout Of Menu------------------------------------------# Top=F
from tkinter import *
from time import sleep
class SchoolCampMenuGUI:
def __init__(self,parent):
#------------------------------Layout Of Menu------------------------------------------#
Top=Frame(parent,bg="white")
Top.pack(side=TOP) #frame for title School Camp Menu
lblTitle=Label(Top,font=('New York Times',15),text="\t\tSchool Camp Menu\t\t\n(Please choose 1 breakfast,lunch and dinner and below 8700KJ) ")
lblTitle.pack() #setting fonts and size
f4=Label(borderwidth=3,relief=SUNKEN)
f4.pack(side=BOTTOM,fill=X)
f1=Label(borderwidth=3,relief=SUNKEN,bg="white")
f1.pack(side=LEFT) #first label for Breakfast
f2=Label(borderwidth=3,relief=SUNKEN,bg="white")
f2.pack(side=RIGHT) #second label for Lunch
f3=Label(borderwidth=3,relief=SUNKEN,bg="white")
f3.pack() #third label for dinner
def onclick1():
r.set(None)
q.set(None)
v.set(None)
def clear():
sleep(0.5)
f4.configure(text="")#define the definition of RESET button all value set to None to reselect choices and clears all calculations.
b1=Button(f4,text="RESET",width=8,bg="red",command=lambda:[onclick1(),clear()])#calling the combined function
b1.pack(side=RIGHT)
def total():
total=int(v.get())+int(r.get())+int(q.get())
f4.configure(text="Your Current Total Is: "+total+" KJs.")
r=StringVar()
v=StringVar()
q=StringVar()
r.set(0)
v.set(0)
q.set(0)
#--------------------------------------Lunch--------------------------------------#
lblMeal=Label(f3,text="Lunch",font=('arial',14,'bold'),bg="white")
lblMeal.pack()
rb1=Radiobutton(f3,text="Chicken Burgers",variable=r,font=('arial',12,'bold'),value=1180,bg="white",command=total)
rb1.pack(anchor=W)
rb2=Radiobutton(f3,text="Chicken Curry and Rice",variable=r,font=('arial',12,'bold'),value=1800,bg="white",command=total)
rb2.pack(anchor=W)
rb3=Radiobutton(f3,text="Teriyaki Chicken Sushi *Gluten Free",variable=r,font=('arial',12,'bold'),value=1730,fg="violet",command=total)
rb3.pack(anchor=W)
rb4=Radiobutton(f3,text="Caprese Panini *Gluten Free",variable=r,font=('arial',12,'bold'),value=2449,fg="violet",command=total)
rb4.pack(anchor=W)
rb5=Radiobutton(f3,text="Vegetable Risotto *Vegetarian",variable=r,font=('arial',12,'bold'),value=1432,fg="blue",command=total)
rb5.pack(anchor=W)
rb6=Radiobutton(f3,text="Gourmet Vegetable Pizza *Vegetarian",variable=r,font=('arial',12,'bold'),value=1463,fg="blue",command=total)
rb6.pack(anchor=W)
#----------------------------------Breakfast----------------------------------#
Meal=Label(f1,text="Breakfast",font=('arial',14,'bold'),bg="white")
Meal.pack()
rb7=Radiobutton(f1,text="Bacon and Egg Muffin",variable=v,font=('arial',12,'bold'),value=1240,bg="white",command=total)
rb7.pack(anchor=W)
rb8=Radiobutton(f1,text="Scrambled Eggs & Bake Beans",variable=v,font=('arial',12,'bold'),value=1533,bg="white",command=total)
rb8.pack(anchor=W)
rb9=Radiobutton(f1,text="2 Weet-Bix w/ milk",variable=v,font=('arial',12,'bold'),value=1110,bg="white",command=total)
rb9.pack(anchor=W)
rb10=Radiobutton(f1,text="Pancakes w/ syrup",variable=v,font=('arial',12,'bold'),value=2019,bg="white",command=total)
rb10.pack(anchor=W)
rb11=Radiobutton(f1,text="Bread with jam",variable=v,font=('arial',12,'bold'),value=491,bg="white",command=total)
rb11.pack(anchor=W)
rb12=Radiobutton(f1,text="Cinnamon Roll Doughnuts",variable=v,font=('arial',12,'bold'),value=1130,bg="white",command=total)
rb12.pack(anchor=W)
#----------------------------------dinner-----------------------------------#
Dinner=Label(f2,text="Dinner",font=('arial',14,'bold'),bg="white")
Dinner.pack()
rb13=Radiobutton(f2,text="Spaghetti Bolongnese",variable=q,font=('arial',12,'bold'),value=1523,bg="white",command=total)
rb13.pack(anchor=W)
rb14=Radiobutton(f2,text="Beef Burgers w/ Chips and Salad",variable=q,font=('arial',12,'bold'),value=3620,bg="white",command=total)
rb14.pack(anchor=W)
rb15=Radiobutton(f2,text="Meatball and Butter Bean Stew *Gluten Free",variable=q,font=('arial',12,'bold'),value=1820,fg="violet",command=total)
rb15.pack(anchor=W)
rb16=Radiobutton(f2,text="Roast Beef *Gluten Free",variable=q,font=('arial',12,'bold'),value=2280,fg="violet",command=total)
rb16.pack(anchor=W)
rb17=Radiobutton(f2,text="Creamy Broccoli Gnocchi *Vegetarian",variable=q,font=('arial',12,'bold'),value=2800,fg="blue",command=total)
rb17.pack(anchor=W)
rb18=Radiobutton(f2,text="Vegetable Wellington *Vegetarian",variable=q,font=('arial',12,'bold'),value=2270,fg="blue",command=total)
rb18.pack(anchor=W)
是否有一种方法可以将所有值相加,但不能得到它们?这是我的学校菜单项目。谢谢你的帮助。
注:食品的数值单位为KJs。到目前为止,我有所有的值,但它们只是放在那里,例如11801800,但没有加起来。我使用了r.get()+v.get(),但它们实际上并没有将值相加。它们确实相加了。您的问题是r.get()返回的是字符串,而不是整数。先换算,然后求和
int(r.get()) + int(v.get())
您好,我尝试了您的方法,但这是我得到的错误:只能将str(而不是“int”)连接到str。有解决方法吗?谢谢感谢你的帮助,我的总数是:def total():total=int(v.get())+int(r.get())+int(q.get())f4.configure(text=“你当前的总数是:”+total+“KJs”)@Cheezdoodle你把所有的
。get
结果都转换成int了吗?谢谢,它现在正在工作,我已经在上面的注释中了。所以我改成了IntVar()和def total():total=int(v.get())+int(r.get())+int(q.get())f4.configure(text=“您当前的总数是:”+str(v.get())+“+”+str(r.get())+“+”+str(q.get())+“=”+str(total)+“KJs”)并且它现在正在工作。对str()
的调用是不必要的v.get()
已返回字符串:)好的,谢谢!idk为什么,但在我添加str()之前,它一开始对我不起作用。。。。特金特不喜欢我,勒芒