【问题标题】:How do you continue a while loop only if a function has been called?只有当一个函数被调用时,你如何继续一个while循环?
【发布时间】:2020-05-12 04:28:42
【问题描述】:

我正在编写一个生成单词然后用户可以输入答案的 Gui 游戏。我现在需要循环来防止这些单词弹出,只有在检查单词是否正确时才运行。

    # Functions
def words():
    global score
    global timer
    start = time.time()
    while time.time() - start <= 60:
        entry.focus_set()  # Activate the entry box
        word = random.choice(word_list[0])
        word_label.config(text=word)
        # Here should be code that makes it that a new word is only generated if the check word is run


        root.update()

    results(score)

def check_word(event, word, score):
    global wrong
    if entry.get().lower() == word.lower():
        score += 1
        entry.delete(0, 9999999)
    else:
        wrong += 1

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    参考here, 我认为您可以将一个键事件绑定到将调用 2 个方法的输入框:

    root.update()

    results(score)

    类似:

    from Tkinter import *
    
    root = Tk()
    
    def key(event):
        print "pressed", repr(event.char)
        root.update()
        results(score)
    
    frame = Frame(root, width=100, height=100)
    frame.bind("<Key>", key)
    frame.pack()
    
    root.mainloop()
    

    【讨论】:

    • 问题是我需要在游戏功能中保持更新,才能让 60 秒计时器停止对吧?
    • 只需绑定另一个事件处理程序&lt;FocusIn&gt; 以在用户关注输入框时开始计算
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 2015-12-23
    • 2013-11-12
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多