我的按钮函数在Python代码中不起作用

我的按钮函数在Python代码中不起作用,python,tkinter,button,Python,Tkinter,Button,你好, 我正在用Python和Tkinter库设计一个简单的餐馆Gui,但是我的重置和计算价格按钮不能正常工作。请问有什么问题 from tkinter import * class Application (Frame): def __init__(self,anjie): super(Application,self).__init__(anjie) #getting the init() function in frame,the supercla

你好, 我正在用Python和Tkinter库设计一个简单的餐馆Gui,但是我的重置和计算价格按钮不能正常工作。请问有什么问题

from tkinter import *

class Application (Frame):
    def __init__(self,anjie):
        super(Application,self).__init__(anjie)
        #getting the init() function in frame,the superclass
        #super is a function that takes two parameters: the name of your subclass i.e. Application and self
        self.grid() #allows grid function to work
        self.toppings=["sausage","pepperoni","chicken","mushroom","black olive","green pepper","red pepper","onion"]
        self.prices={"medium":9.99, "large":12.99, "x-large":14.99, "mid-topping":1.0, "large-topping":1.4, "xl-topping":1.8}
        self.checkbox=list(self.toppings)
        self.checkboxVar=list(self.toppings)
        self.create_widgets()
        
    def create_widgets(self):
        menubar=Menu(self)
        filemenu=Menu(menubar)
        menubar.add_cascade(label="File", menu=filemenu)
        menubar.add_cascade(label="Quit",command=window.quit)
        
        filemenu.add_command(label="Submit",command=self.process)
        
            
        self.label1=Label(self,text="Select size: ")
        self.label1.grid(row=0,column=0,sticky=W)
        self.size=StringVar() #similar to name in html, groups similar elements together with variable self.size
        print(StringVar())
        
        self.radio1=Radiobutton(self,text="Medium",variable=self.size,value="Medium")
        self.radio1.grid(row=1,column=0,sticky=W)
        self.radio1.select() #makes radio1 the default selected radio button
        
        self.radio2=Radiobutton(self,text="Large",variable=self.size,value="Large")
        self.radio2.grid(row=1,column=1,sticky=W)
        self.radio2.select()
        
        self.radio3=Radiobutton(self,text="Extra Large",variable=self.size,value="X-Large")
        self.radio3.grid(row=1,column=2,sticky=W)
        self.radio3.select()
        
        self.label2=Label(self,text="Select size: ")
        self.label2.grid(row=2,column=0,sticky=W)
        
        
        line=2#last row number
        for i in range(len(self.toppings)):
            line+=1
            self.checkboxVar[i]=BooleanVar() #default value added is false
            self.checkboxVar[i]=Checkbutton(self,text=self.toppings[i], variable=self.checkboxVar[i])
            self.checkboxVar[i].grid(row=line,column=0,sticky=W)
            
        line+=1
        self.button1=Button(self,text="Reset",command=self.reset)
        self.button1.grid(row=line,column=0,sticky=E)
        self.button2=Button(self,text="Calculate Price",command=self.calculate)
        self.button2.grid(row=line,column=2)
        
        line+=1
        self.label3=Label(self,text="")
        self.label3.grid(row=line,column=0)
        
        line+=1
        self.label4=Label(self,text="Total: ")
        self.label4.grid(row=line,column=0,sticky=E)
        self.result=Entry(self,width=10)
        self.result.grid(row=line,column=1,sticky=W)
        
        window.config(menu=menubar)
        
    def process(self):
        print("This is the process to submit: ")
        
    def reset(self):
        self.radio1.select()
        for i in range(len(self.toppings)):
            self.checkbox[i].deselect()
        self.result.delete(0,END)
        
    def calculate(self):
        self.totalToppings=0
        for i in range(len(self.toppings)):
            if self.checkboxVar[i].get():
                self.totalToppings+=1
                        
            if self.size.get()=="Medium":
                self.price=self.prices["medium"]+(self.totalToppings * self.prices["mid-topping"])
            if self.size.get()=="Large":
                self.price=self.prices["large"]+(self.totalToppings*self.prices["large-topping"])
            if self.size.get()=="X-Large":
                self.price=self.prices["x-large"]+(self.totalToppings*self.prices["xl-topping"])
                
            
            str_price="{.2f}".format(self.price)
            self.result.delete(0,END)
            self.result.insert(END,str_price)
            
window=Tk()
window.title("Anjie\'s Pizza")
window.geometry("800x500") #WIDTH BY HEIGHT
app=Application(window)
app.mainloop()

我将非常感谢在这方面的任何帮助。我使用Python tkinter库完成了这项工作,但我的问题是在总条目中没有显示任何结果。

您的代码中几乎没有问题:

调用。在所有单选按钮上选择,应仅调用self.radio1.select

使用self.checkboxVar[i]代替self.checkbox[i]

def创建_Widgetself: ... self.radio2.select不应调用 ... self.radio3.select不应调用 ... 行=2最后一行编号 对于rangelenself.toppings中的i: 行+=1 self.checkboxVar[i]=BooleanVar默认添加值为false 应使用self.checkbox[i]=。。。而不是self.checkboxVar[i]=。。。 self.checkbox[i]=Checkbuttonself,text=self.toppings[i],variable=self.checkboxVar[i] self.checkbox[i].gridrow=line,column=0,sticky=W ... 使用了无效的浮点格式{.2f},应改为{.2f}: def calculateself: ... str_price={:.2f}.formatself.price ...
如果我运行此操作并按下标签为“计算价格”的按钮,我会收到错误消息:AttributeError:“Checkbutton”对象没有属性“get”,而对于重置,我会收到AttributeError:“str”对象没有属性“deselect”。如何在看不到错误消息的情况下运行此操作?请明确说明您想要的预期结果。@acw1668 Calculate price应返回他们想要订购的产品的价格。重置应重置所有检查boxes@Matthias我能清楚地看到错误。我的问题是我不知道如何纠正它们。因此,我问了这个问题。我这样做了,但在Tkinter回调回溯上一次最近的调用中仍然存在此错误PY_VAR1异常:文件C:\Program Files\Spyder\pkgs\Tkinter_init_uu1;.PY,第1705行,在call return self.func*args文件C:\Users\ariel\OneDrive\Documents\400L\SIWES\NH PYTHON\newpython\eatery.PY,第89行,在calculate if self.checkbox[i].get:AttributeError:“Checkbutton”对象中没有属性“get”。我的计算价格按钮仍然不起作用,但我的重置按钮works@AnjieLayo这是因为您更改了self.checkboxVar[i]。进入self.checkboxVar[i]。也进入计算。仔细看,在我的回答中,我只将create_小部件中的self.checkboxVar[I]改为self.checkboxVar[I]两行。我这样做了,我有一个错误:Tkinter回调回溯中的PY_VAR1异常最近一次调用:文件C:\Program Files\Spyder\pkgs\Tkinter_init_uu.PY,第1705行,在call return self.func*args文件C:\Users\ariel\OneDrive\Documents\400L\SIWES\NH PYTHON\newpython\eatery.py的第89行中,在calculate if self.checkbox[i]中。get:AttributeError:'Checkbutton'对象没有属性'get',仔细查看错误,您已经使用了self.checkbox[i]。get。self.checkboxVar[i].get应该在calculate中使用。是的,这是我最初所做的,但我的错误仍然类似:Tkinter回调中的异常回溯最近的调用最后一次:文件C:\Program Files\Spyder\pkgs\Tkinter\u init\uuuz.py,第1705行,调用返回self.func*args文件C:\Users\ariel\OneDrive\Documents\400L\SIWES\NH PYTHON\newpython\eatery.py,第89行,在calculate if self.checkboxVar[i]中。get:AttributeError:'str'对象没有属性'get'