【问题标题】:Change questions using buttons使用按钮更改问题
【发布时间】:2023-03-11 17:35:01
【问题描述】:

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

这样的代码根本不起作用。我还想我在 while 循环中犯了一个错误,要求显示所有问题,而不是一次显示一个。任何想法让这样的代码工作? 感谢您的宝贵时间!

【问题讨论】:

  • 首先,直到 while 循环结束,按钮才会显示。所以你需要将 for 循环移动到你的 while 循环中。

标签: python button tkinter while-loop


【解决方案1】:

导致您的代码无法运行的主要原因有两个:

  1. 在一个脚本中,您不能使用两个或多个几何管理器编写 GUI。这不仅适用于 tkinter,也适用于 PyQt 等其他 GUI 第三方库。
  2. 标签应在每一轮中显示不同的消息或问题。所以这意味着你需要用 StringVar 来修改内容。它表现为字符串变量。你可以了解更多here

我不清楚您为什么要存储按钮以及保存用户创建的结果的位置。

这是我的代码:

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

【讨论】:

    【解决方案2】:

    这也可以以面向对象的方式完成,这可能更适合未来的验证。

    请参阅下面我注释的脚本版本以获取解释和示例:

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

    这本质上是不同的,因为我们只是将label 配置为显示问题list 中的下一个元素,而不是破坏它或弹出list

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 2023-03-21
      • 2015-10-11
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多