关于在 Python 中使用 Tkinter 使用 Radiobutton 的问题
Posted
技术标签:
【中文标题】关于在 Python 中使用 Tkinter 使用 Radiobutton 的问题【英文标题】:Question about with Radiobutton using Tkinter in Python 【发布时间】:2022-01-18 08:59:03 【问题描述】: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()
基本上,我的问题是在 Cake Class 里面。当我运行代码并在第二个窗口中选择一个选项时,它没有打开名为“Receipt”的第三个窗口,我不明白为什么?
当我选择第三个选项“胡萝卜蛋糕”并提交时,它应该打印self.choice.get()
提交函数的输入if语句但它没有,它自动转到else语句This is the output I'm getting.
任何帮助将不胜感激
【问题讨论】:
对于子窗口,使用Toplevel()
而不是Tk()
。
【参考方案1】:
这是因为您使用了多个Tk()
实例。对于子窗口,请改用Toplevel()
:
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")
...
【讨论】:
以上是关于关于在 Python 中使用 Tkinter 使用 Radiobutton 的问题的主要内容,如果未能解决你的问题,请参考以下文章
Python & Tkinter -> 关于调用冻结程序的长时间运行函数