Python Tkinter清除框架并关联用户';这是一个值的答案
我试图在多项选择题调查中澄清每个问题之间的框架,但我希望框架本身保持不变。我试图创建一个“下一步”按钮,允许用户跳到下一个问题,但当我运行代码时,框架没有出现(也没有出现错误消息) 为了存储用户对问题的答案,最好使用单选按钮的值(1,2,3,4,5)还是单选按钮的变量 这是我的密码:Python Tkinter清除框架并关联用户';这是一个值的答案,python,tkinter,radio-button,Python,Tkinter,Radio Button,我试图在多项选择题调查中澄清每个问题之间的框架,但我希望框架本身保持不变。我试图创建一个“下一步”按钮,允许用户跳到下一个问题,但当我运行代码时,框架没有出现(也没有出现错误消息) 为了存储用户对问题的答案,最好使用单选按钮的值(1,2,3,4,5)还是单选按钮的变量 这是我的密码: from Tkinter import * fenetre = Tk() fenetre.title("Life Quiz") from random import shuffle #questions q
from Tkinter import *
fenetre = Tk()
fenetre.title("Life Quiz")
from random import shuffle
#questions
quest_1 = "What is your favorite color?"
quest_2 = "Which of these animals do you like the most?"
quest_3 = "Which of these superpowers would you want to have the most?"
quest_4 = "Which food would you want to eat right now?"
quest_5 = "If you could only bring one thing on an island, what would it be?"
#liste de réponse
list_reponse = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
#Associé liste_reponse avec IntVar()
list_reponse[0] = IntVar()
list_reponse[1] = IntVar()
list_reponse[2] = IntVar()
list_reponse[3] = IntVar()
list_reponse[4] = IntVar()
class Questions(object):
#fonction pour afficher les questions et les choix de réponses
def __init__(self, master, n_question):
self.master = master
self.liste_choix =[ [quest_1, "Blue", "White", "Yellow", "RAINBOWS", "Red"], [ quest_2, "Blue Whale", "Pig", "Cat", "Unicorns", "Dog"], [quest_3, "Make it rain whenever you want", "Transform yourself into a ladder", "Invisibility when no one's around", "Read the mind of unicorns", "Ability to fly" ], [quest_4, "Ice cream", "Sushi", "Salad", "Cupcakes", "Pizza"] ,[quest_5, "Nothing...", "A fishing rod", "A book", "A leprechaun", "A knife"] ]
self.liste_choix_questions = self.liste_choix [n_question]
shuffle(self.liste_choix_questions)
self.display_frame = None
self.next_question = 0
def display_next(self):
if self.next_question < len(self.liste_choix_questions):
if self.display_frame:
self.display_frame.destroy()
self.display_frame=Frame(self.master)
self.display_frame.grid(row=0, column=0)
Label(self.display_frame,
text=self.liste_choix_questions[0],
bg="lightblue").grid(row=0, column=0)
display_next(self)
R1 = Radiobutton(fenetre, text= self.liste_choix[n_question][1], variable= list_reponse[n_question], value=1)
R1.pack( anchor = W )
R2 = Radiobutton(fenetre, text= self.liste_choix[n_question][2], variable= list_reponse[n_question], value=2)
R2.pack( anchor = W )
R3 = Radiobutton(fenetre, text= self.liste_choix[n_question][3], variable= list_reponse[n_question], value=3)
R3.pack( anchor = W)
R4 = Radiobutton(fenetre, text= self.liste_choix[n_question][4], variable= list_reponse[n_question], value=4)
R4.pack( anchor = W )
R5 = Radiobutton(fenetre, text= self.liste_choix[n_question][5], variable= list_reponse[n_question], value=5)
R5.pack( anchor = W )
Button(self.display_frame, text="next",
command=display_next).grid(row=1, column=0)
n_question += 1
Q = Questions(fenetre,1)
fenetre.mainloop()
从Tkinter导入*
fenetre=Tk()
fenetre.标题(“生活测验”)
从随机导入洗牌
#问题
quest_1=“你最喜欢什么颜色?”
quest_2=“你最喜欢这些动物中的哪一种?”
quest_3=“你最想拥有这些超级大国中的哪一个?”
quest_4=“您现在想吃什么食物?”
quest_5=“如果你只能在岛上带一件东西,那会是什么?”
#李斯特·德雷庞
列表响应=[“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”]
#Associéliste_reponse avec IntVar()
列表\u响应[0]=IntVar()
列表\u响应[1]=IntVar()
列表\u响应[2]=IntVar()
列表\u响应[3]=IntVar()
列表\u响应[4]=IntVar()
课堂提问(对象):
#问题与合唱团基金会
定义初始(self,master,n_问题):
self.master=master
self.liste_choix=[[quest_1,“蓝色”、“白色”、“黄色”、“彩虹”、“红色”]、[quest_2,“蓝鲸”、“猪”、“猫”、“独角兽”、“狗”]、[quest_3,“想下雨就下雨”、“把自己变成梯子”、“无人时隐身”、“读独角兽的心”、“飞翔能力”]、[quest_4]、“冰淇淋”、“寿司”、“沙拉”,“纸杯蛋糕”、“比萨饼”],[quest_5,“什么都没有……”,“钓鱼竿”、“书”、“小妖精”、“刀”]]
self.liste_choix_questions=self.liste_choix[n_question]
洗牌(自列问题)
self.display\u frame=无
self.next_问题=0
def显示_下一步(自我):
如果self.next_question
一种解决方案是使用内部框架来保存单选按钮。然后,您可以删除框架,从而删除所有单选按钮。最后,您可以使用新的单选按钮重新创建框架
另一个解决方案是简单地迭代单选按钮,为当前问题重新配置它们(假设所有问题都有相同数量的选项).一种解决方案是使用一个内部框架来保存单选按钮。然后,您可以删除该框架,该框架将删除所有单选按钮。最后,您可以使用新的单选按钮重新创建框架 另一个解决方案是简单地迭代单选按钮,为当前问题重新配置它们(假设所有问题都有相同数量的选项)