【问题标题】:Add items to a listbox after 5 seconds in Python在 Python 中 5 秒后将项目添加到列表框
【发布时间】:2019-03-04 21:00:14
【问题描述】:

我编写了一个简单的程序来创建数学乘法问题。我把它写成一个 CLI,但是由于一些老师说 CLI 太小,学生看不到,所以转移到了 GUI。

一切正常,只有一件事。单击“开始”按钮后,我想在 5 秒后用一个新问题更新左侧的列表框。在 CLI 版本中,我只是添加了 sleep(5) 来暂停程序然后恢复,但是 GUI 会暂停整个程序并等待,然后吐出所有问题。

如果我删除 for 循环,老师可以点击按钮 15 次,但这似乎很浪费。

这是我的代码:

from tkinter import *                
from tkinter import messagebox          
from time import sleep                  
from random import randint

questionList=[]

def main():
    for i in range (15):
        num1= randint(0,12)
        num2 = randint(1,12)
        question = ("Question",(i+1),")",num1,"X",num2)
        listbox.insert(END, question)
        #sleep(5)
        tempArray = []
        tempArray.append(num1)
        tempArray.append(num2)
        questionList.append(tempArray)

def answers ():
    for i in range (len(questionList)):
            ans = (questionList[i][0]*questionList[i][1])
            listbox1.insert(END,ans)

root = Tk()                                                                                         
root.geometry("445x590+460+70")                                                                    
root.title("Maths Machine")                                                                         
label = Label(root, text="Maths Machine", font = ("Arial",16)).grid(row = 0, columnspan = 2)
startButton = Button(root, text = "Start", width = 15, command = main).grid(row = 1, column = 1,padx = 10, pady = 10)
label = Label(root, text = "Questions", font = ("Arial",12)).grid(row  = 2, column = 0)
label = Label(root, text = "Answers", font = ("Arial",12)).grid(row  = 2, column = 1)
listbox = Listbox(root, width = 25, height = 15,font = ("Arial",16))
listbox.grid(row = 3, column = 0)
listbox1 = Listbox(root, width = 10, height = 15,font = ("Arial",16))
listbox1.grid(row = 3, column = 1)
answerButton = Button(root, text ="Show answers", width = 15, command = answers).grid(row = 4, column = 1, padx =10, pady =10)


mainloop()

【问题讨论】:

  • 查看如何创建minimal reproducible example
  • @PeterWood ...你是什么意思?
  • 您的情况下的 sleep() 函数在您的 GUI 的主线程中运行,这将阻止您的主线程中的所有其他操作(即按钮单击、文本更新,基本上一切)。我认为 root.after() 方法将是您正在寻找的方法,请在此链接中查看 'after' 方法:effbot.org/tkinterbook/widget.htm
  • @Erik 谢谢你。我已经设法让它一一出现,但是,它并没有停止。有什么想法吗?
  • @NeosNokia 在被“after”函数调用的函数中,你可以在其中的逻辑之前添加一个简单的“if foo == True:”。然后只需更改 foo 变量当您不希望执行该代码时将其设置为 False。或者也许可以完全停止 'after' 功能,请查看these answers

标签: python tkinter listbox


【解决方案1】:

要在哀号后更新列表框项目,您可以使用Tk 类的after 方法。

您需要在 5 秒后调用 main 方法。

def main():
    num1= randint(0,12)
    num2 = randint(1,12)
    question = ("Question",(len(questionList)+1),")",num1,"X",num2)
    listbox.insert(END, question)
    tempArray = []
    tempArray.append(num1)
    tempArray.append(num2)
    questionList.append(tempArray)
    if len(questionList) < 15:
        root.after(5000, main)

【讨论】:

  • 谢谢你。这是我最初所拥有的,但是,我需要它在 15 个问题后结束,因此我添加了将在 15 个循环后取消的计数。
【解决方案2】:

这就是我为停止程序运行并仍然保持暂停所做的操作。

count=listbox.size()

count+=1
num1= randint(0,12)
num2 = randint(1,12)
question = ("Q",count,")",num1,"X",num2)
listbox.insert(END, question)
tempArray = []
tempArray.append(num1)
tempArray.append(num2)
questionList.append(tempArray)
winsound.Beep(700,1000)
if notWindows == False:
    w=root.after(5000,main)
if listbox.size() == 15:
    root.after_cancel(w)
    answerButton = Button(root, text ="Show answers", width = 15, command = answers).grid(row = 4, column = 1, padx =10, pady =10)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2011-10-18
    相关资源
    最近更新 更多