I'm new to GUIs, and I'm writing some code segments for school. This is an issue I've been having in multiple assignments and I can't seem to wrap my head around it. When I implement checkboxes, the value returned is always positive regardless of whether or not I check to box. For example, this is a code I've been writing that is supposed to create a menu for the user to select from, and them display the correct total:
from tkinter import *
def main():
class Application(Frame):
#GUI application that creates a menu to be chosen from and selections to be made.
def __init__(self, master):
#Initialize Frame.
Frame.__init__(self, master)
self.grid()
self.createWidgets()
def addPrices(self):
#store price float
price = 0.00
#add all contents of the list selected
#use if statement to determine which prices need to be added
if self.isPizza:
price += 3.00
if self.isFries:
price += 0.50
if self.isCoke:
price += 0.25
if self.isChurro:
price += 1.50
#display totals
#Use label to print total
Label(self,text = "Your total is: ").grid(row = 9, column = 0, columnspan = 2, sticky = W)
Label(self,text = price).grid(row = 10, column = 0, columnspan = 2, sticky = W)
def createWidgets(self):
#create instruction label
Label(self,text = "Please Select your Items:").grid(row = 0, column = 4, columnspan = 2, sticky = W)
#create labels for checkboxes / menu
Label(self,text = "Pizza.... $3.00").grid(row = 1, column = 5, sticky = W)
Label(self,text = "Fries... $0.50").grid(row = 2, column = 5, sticky = W)
Label(self,text = "Coke... $0.25").grid(row = 3, column = 5, sticky = W)
Label(self,text = "Churro... $1.50").grid(row = 4, column = 5, sticky = W)
#create input via checkboxes
#Create a plain of checkboxes to select items
self.isPizza = BooleanVar()
Checkbutton(self,text = "Pizza",variable = self.isPizza).grid(row = 6, column = 0, sticky = W)
self.isFries = BooleanVar()
Checkbutton(self, text = "Fries",variable = self.isFries).grid(row = 6, column = 1, sticky = W)
self.isCoke = BooleanVar()
Checkbutton(self, text = "Coke",variable = self.isCoke).grid(row = 7, column = 0, sticky = W)
self.isChurro = BooleanVar()
Checkbutton(self, text = "Churro",variable = self.isChurro).grid(row = 7, column = 1, sticky = W)
#Create submit button
Button(self,text = "Click to submit order",command = self.addPrices).grid(row = 8, column = 0, sticky = W)
root = Tk()
root.geometry("800x400")
root.title(" Order Up!")
app = Application(root)
root.mainloop()
main()
The program always returns 5.25 as the total, which is just the amount of all items added together. Am I missing a segments that will change my Boolean variables depending on user input?
Aucun commentaire:
Enregistrer un commentaire