Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用按钮更改问题_Python_Button_Tkinter_While Loop - Fatal编程技术网

Python 使用按钮更改问题

Python 使用按钮更改问题,python,button,tkinter,while-loop,Python,Button,Tkinter,While Loop,我是Python编程新手,正在学习如何创建用户界面。我想创建一个非常基本的界面,它有以下流程:使用while循环,界面显示问题列表中包含的所有问题。每次出现问题时,问题下方都会出现两个按钮(是/否)。只有单击其中一个问题时,界面才会显示下一个问题。 我在这里附上我试过的代码 import tkinter as tk questions=['Question 1','Question 2','Question 3','Question 4', 'Question 5'] root = tk.Tk

我是Python编程新手,正在学习如何创建用户界面。我想创建一个非常基本的界面,它有以下流程:使用while循环,界面显示问题列表中包含的所有问题。每次出现问题时,问题下方都会出现两个按钮(是/否)。只有单击其中一个问题时,界面才会显示下一个问题。 我在这里附上我试过的代码

import tkinter as tk

questions=['Question 1','Question 2','Question 3','Question 4', 'Question 5']
root = tk.Tk()
root.minsize(300, 300)
answers=['Yes','No']
b_list = [] 

def ask():
    count=0
    while count<len(questions):
        lab=tk.Label(root,text=questions[count])
        lab.pack()
        count+=1

for i in range(2):
    b = tk.Button(root, text = answers[i],command=ask)
    b.grid(row = 0, column = i)

    b_list.append(b) 

root.mainloop()
将tkinter作为tk导入
问题=[‘问题1’、‘问题2’、‘问题3’、‘问题4’、‘问题5’]
root=tk.tk()
root.minsize(300300)
答案=[“是”、“否”]
b_列表=[]
def ask():
计数=0

当count时,有两个主要原因使代码无法运行:

  • 在一个脚本中,不能使用两个或多个几何体管理器编写GUI。这不仅适用于tkinter,也适用于其他GUI第三方库,如PyQt
  • 标签应在每一轮中显示不同的信息或问题。因此,这意味着您需要使用StringVar修改内容。它表现为字符串的变量。你可以学到更多
  • 我不清楚为什么要存储按钮以及保存用户结果的位置

    这是我的密码:

    import tkinter as tk
    
    questions = ['Question 1', 'Question 2',
                 'Question 3', 'Question 4', 'Question 5']
    answers = ['Yes', 'No']
    
    
    root = tk.Tk()
    root.minsize(300, 300)
    
    
    def ask():
        if questions:
            lab_text.set(questions.pop(0))
    
    for index, answer in enumerate(answers):
        lab_text = tk.StringVar()
        lab = tk.Label(root, textvariable=lab_text)
        lab.grid(row=0, column=0)
        b = tk.Button(root, text=answer, command=ask)
        b.grid(row=1, column=index)
    
    #initialize label
    ask()
    
    
    root.mainloop()
    

    这也可以以面向对象的方式完成,这可能会更好地支持将来的校对

    有关解释和示例,请参见下面我对脚本的评论版本:

    from tkinter import *
    import random
    
    class App:
        def __init__(self, root):
            self.root = root
            self.array = ["Question1", "Question2", "Question3", "Question4", "Question5"] #list containing questions
            self.answer = [] #empty list for storing answers
            self.question = Label(self.root, text=self.array[len(self.answer)]) #creates a text label using the element in the 0th position of the question list
            self.yes = Button(self.root, text="Yes", command=self.yescmd) #creates button which calls yescmd
            self.no = Button(self.root, text="No", command=self.nocmd) #creates button which calles nocmd
            self.question.pack()
            self.yes.pack()
            self.no.pack()
        def yescmd(self):
            self.answer.append("yes") #adds yes to the answer list
            self.command() #calls command
        def nocmd(self):
            self.answer.append("no") #adds no to the answer list
            self.command() #calls command
        def command(self):
            if len(self.answer) == len(self.array): #checks if number of answers equals number of questions
                self.root.destroy() #destroys window
                print(self.answer) #prints answers
            else:
                self.question.configure(text=self.array[len(self.answer)]) #updates the text value of the question label to be the next question
    root = Tk()
    App(root)
    root.mainloop()
    

    这本质上是不同的,因为我们只是配置
    标签
    以显示问题
    列表中的下一个元素
    ,而不是销毁它或弹出
    列表

    首先,在while循环结束之前,甚至不会显示所有按钮。因此,您需要将for循环移动到while循环中。