我有一个学校项目,打算用tkinter python创建一个网站。我的问题是在功能“ Whats_for_dinner”中。我的检查按钮和单选按钮都没有替换变量以帮助我确定选择了哪一个(即单选按钮保持为零,而不是更改为1、2、3等)。此外,单选按钮还选择了多个。
感谢您的帮助。
我对使用python编码非常陌生,因此最简单的修复可能是我最了解的。
这是我的代码:
#Description: This GUI program will imitate an online sevice
# like Uber Eats. Another feature is a quiz for the user to take
# too make a good sugestion on a food you can order to eat.
import tkinter
import tkinter.messagebox
class MyGUI:
def __init__(self):
# Create the main window widget.
self.main_window = tkinter.Tk()
#Frames
self.quizFrame = tkinter.Frame(self.main_window)
#Quiz button
self.quiz = tkinter.Button (self.quizFrame, bg = 'light green', font = 'Cipote 55 bold', text = "What's for Dinner?", command = self.Whats_for_dinner)
#Pack Quiz Buttons
self.quiz.pack()
#Pack Frames
self.quizFrame.pack()
# Enter the tkinter main loop.
tkinter.mainloop()
def Whats_for_dinner(self):
#Create main window widget
self.second_window = tkinter.Tk()
#Frames
self.question1_frame = tkinter.Frame(self.second_window)
self.buttonFrame = tkinter.Frame(self.second_window)
#Question 1
self.Q1Label = tkinter.Label(self.question1_frame, font = 'times 14 bold', text = 'Are you...')
self.Q1 = tkinter.IntVar()
self.Q1.set(0)
self.vegitarian = tkinter.Radiobutton(self.question1_frame, text = 'Vegitarian', variable = self.Q1, value=1)
self.pescetarian = tkinter.Radiobutton(self.question1_frame, text = 'Pescetarian', variable = self.Q1, value=2)
self.vegan = tkinter.Radiobutton(self.question1_frame, text = 'Vegan', variable = self.Q1, value=3)
self.NR = tkinter.Radiobutton(self.question1_frame, text = 'No restriction', variable = self.Q1, value=4) #NR stands for No Restriction
# Make What's for dinner button and refresh button
self.whats_for_dinner = tkinter.Button(self.buttonFrame, text = "What's for Dinner?", command = self.calculate)
#Pack Questions
self.Q1Label.pack()
self.vegitarian.pack(side = 'left')
self.pescetarian.pack(side = 'left')
self.vegan.pack(side = 'left')
self.NR.pack(side = 'left')
#Pack Buttons
self.whats_for_dinner.pack(side = 'left')
#Pack Frames
self.question1_frame.pack()
self.buttonFrame.pack()
def calculate(self):
tkinter.messagebox.showinfo('Food', str(self.Q1.get()))
# Create an instance of the MyGUI class.
my_gui = MyGUI()
注意:按下绿色按钮可以访问单选按钮
答案 0 :(得分:0)
这里的问题是在单个程序中使用两个Tk()
窗口。
只需更改此行
self.second_window = tkinter.Tk()
使用
self.second_window = tkinter.Toplevel()