【问题标题】:Python Tkinter Clear frame and associate user's answer to a valuePython Tkinter 清除框架并将用户的答案与一个值相关联
【发布时间】:2016-03-10 20:49:17
【问题描述】:

我正在尝试清除多项选择调查中提出的每个问题之间的框架,但我希望框架本身保持不变。我试图创建一个下一个按钮,允许用户跳到下一个问题,但是当我运行我的代码时框架没有出现(也没有错误消息出现)。

为了存储用户对问题的回答,是使用单选按钮的值 (1,2,3,4,5) 还是单选按钮的变量更好。

这是我的代码:

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()

【问题讨论】:

    标签: python tkinter radio-button


    【解决方案1】:

    一种解决方案是使用内部框架来固定单选按钮。然后,您可以删除将删除所有单选按钮的框架。最后,您可以使用新的单选按钮重新创建框架。

    另一种解决方案是简单地遍历单选按钮,为当前问题重新配置它们(假设所有问题都有相同数量的选择)。

    【讨论】:

      猜你喜欢
      • 2014-08-14
      • 2016-08-02
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      相关资源
      最近更新 更多