Reputation: 1
from tkinter import *
from PIL import Image, ImageTk
class Cake:
def __init__(self):
self.window=Tk()
self.window.title("Type of Cake")
# self.window.geometry("500x300") #window size
self.choice =IntVar()
self.velvet_radiobutton= Radiobutton(self.window, text="Red Velvet Cake", var=self.choice, value=0)
self.velvet_radiobutton.grid(row=0, column=0)
self.chocolate_radiobutton= Radiobutton(self.window, text="Chocolate Cake", var=self.choice, value=1 )
self.chocolate_radiobutton.grid(row=1, column=0)
self.carrot_radiobutton= Radiobutton(self.window, text="Carrot Cake", var=self.choice, value=2 )
self.carrot_radiobutton.grid(row=2, column=0)
self.submit_button=Button(self.window, text="Submit", command=self.submit)
self.submit_button.grid(row=3, column=0)
self.window.mainloop()
def submit(self):
print(self.choice.get())
if self.choice.get()==2:
velvet_cake =Velvet_cake()
print(self.choice.get())
else:
print("dont work")
def submit1():
#entry.get() will give you a string
shape=shape_entry.get()
topping= topping_entry.get()
name=name_entry.get()
#name validation
if not name.isalpha():
print("Please input an appropriate name")
return
if not shape.isalpha():
print("Please input an appropriate cake shape")
return
if topping.isdecimal():
topping= int(topping)
else:
print("This is not a valid number")
return
#compare age
if topping>50:
print("That's too many toppings!")
elif topping<0:
print("Topping number is invalid")
else:
print(f"Hi {name}, you've ordered {topping} toppings on a {shape} cake. That sounds delicious!")
Zoo=Cake()
class Velvet_cake:
def __init__(self):
self.window= Tk()
self.window.title("Receipt")
main= Tk()
main.title("Online Cake Order")
# image_file= Image.open("CakeBran.png")
# image=ImageTk.PhotoImage(image_file)
# image_label=Label(main, image=image)
# image_label.grid(row=0,columnspan=3)
name_label = Label(main, text= "Name")
name_label.grid(row=1, column=0)
name_entry = Entry(main)
name_entry.grid(row=1,column=1)
shape_label = Label(main, text="Shape of Cake")
shape_label.grid(row=2, column=0)
shape_entry = Entry(main)
shape_entry.grid(row=2,column=1)
topping_label =Label(main, text="Number of topings")
topping_label.grid(row=3,column=0)
topping_entry= Entry(main)
topping_entry.grid(row=3, column=1)
submit_button=Button(main, text="Submit",command=submit1)
submit_button.grid(row=4, column=2)
main.mainloop()
Basically, my question is inside the Cake Class. When I run the code and choose an option in the the second window, it doesn't open a third window named "Receipt" which I don't understand why?
When I Choose the third option "Carrot Cake" and submit, it should print self.choice.get()
the enter the if statement of the submit function but it doesn't, it automatically goes to the else statementThis is the output I'm getting.
Any help would be appreciated
Upvotes: 0
Views: 66
Reputation: 46669
It is because you have used multiple instances of Tk()
. For child windows, use Toplevel()
instead:
class Cake:
def __init__(self):
self.window = Toplevel() # use Toplevel() instead of Tk()
...
...
class Velvet_cake:
def __init__(self):
self.window = Toplevel() # use Toplevel() instead of Tk()
self.window.title("Receipt")
...
Upvotes: 1