m0_51811055
m0_51811055
采纳率0%
2021-03-08 21:29

python tkinter 该如何让一个loop里的问题一个接一个显示

想用tkinter做一个从两个list里面出题的问卷

因为有12道题,都是大同小异,就想用一个loop来循环出12次。目前的问题是一下子12题同时出来了。。。有没有什么办法能让他们依次出来,一道题点了next按钮后下一道题才会出现,试了time,os.pause 都没用。。。

万分感谢!

import tkinter as tk
import random
import time
import os

Englis=['Jan','Feb','March','April','May','June','July','August','Sept','Oct','Nov','Dec']
Spalis=['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre']

def next():
    a=0

def test():
    cholis=Englis
    sollis=Spalis

    for i in range (0,12):
        num=random.randint(0,len(cholis)-1)
        
        EnglishWord=tk.StringVar()
        EnglishWord.set('Please print out the Spanish word of '+cholis[num]+': ')
        
        SpanishWord=tk.StringVar()
        SpanishWord.set(sollis[num])
        Question=tk.Label(window,textvariable=EnglishWord,font=('Arial', 25), width=15,height=3)
        Question.pack()        
        
        e2 = tk.Entry(window, show=None, font=('Arial', 14))
        e2.pack()
        
        NextQButton=tk.Button(window, text="Next", command=next)
        NextQButton.pack()   

        
        
window=tk.Tk()
window.title('My Window')
window.geometry('1920x1080')
test()
window.mainloop()
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • funny123 coagenth 1月前

    问题是用for循环会全部一次性显示在窗口中,要实现你想要的效果,把代码作如下修改:

    import tkinter as tk
    import random
    import time
    import os
    
    Englis = ['Jan', 'Feb', 'March', 'April', 'May', 'June',
              'July', 'August', 'Sept', 'Oct', 'Nov', 'Dec']
    Spalis = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio',
              'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre']
    window = tk.Tk()
    window.title('My Window')
    window.geometry('1920x1080')
    cholis = Englis
    sollis = Spalis
    def test():
        num = random.randint(0, len(cholis)-1)
    
        EnglishWord = tk.StringVar()
        EnglishWord.set(
            'Please print out the Spanish word of '+cholis[num]+': ')
        SpanishWord = tk.StringVar()
        SpanishWord.set(sollis[num])
        Question = tk.Label(window, textvariable=EnglishWord,
                            font=('Arial', 14), width=40, height=3)
        Question.pack()
    
        e2 = tk.Label(window, text=SpanishWord.get(), font=('Arial', 14))
        e2.pack()
        
        def next():
            test()
            Question.destroy()
            e2.destroy()
            NextQButton.destroy()
            QuitButton.destroy()
        NextQButton = tk.Button(window, text="Next", command=next)
        NextQButton.pack()
        QuitButton = tk.Button(window,text='Quit',command=window.quit)
        QuitButton.pack()
        
    test()
    window.mainloop()
    
    点赞 评论 复制链接分享