【问题标题】:Using tkinter gives spinning beachball on MacOS使用 tkinter 在 MacOS 上提供旋转沙滩球
【发布时间】:2020-11-04 07:09:10
【问题描述】:

每次我尝试运行这个程序时,调用 dictee() 时程序都会卡住。 Mac 上著名的旋转沙滩球。我发现了一个类似的问题,window.update() 得到了解决方案,但没有成功。要么它没有继续,要么循环通过程序。

在我将 check() 函数添加到游戏之前,它一直运行良好。我不想指出方向,但它有助于提供该信息。

几周前我刚开始学习 python 来帮助我的孩子。我首先在没有 tkinter 的情况下编写了脚本,效果很好,但决定使用 tkinter 使其更具“吸引力”。

它基本上需要做的是从词汇列表中显示一个随机单词,创建一个条目窗口并将条目与随机单词进行比较,更新分数列表,继续一个新单词,直到它提供的词汇为空选择另一轮或退出。此选项也在单词之间提供,因此用户可以随时退出。

谢谢。

"""import nescessary."""
import tkinter as tk
import sys
import random

#setup window
window = tk.Tk()
window.geometry("500x300")
window.title("Aliyah's dictee spel")
window.configure(background = "black")

#my photo
photo1 = tk.PhotoImage(file="dictee.gif")
tk.Label (window, image=photo1, bg="black") .grid(row=0, column=0)

def main():
    """Setting up the game"""
    global VOCABULARY, WORDS_PASSED, WORDS_CORRECT, WORDS_WRONG
    VOCABULARY = ['kip', 'hok', 'bal', 'muis', 'gat']
    WORDS_PASSED = []
    WORDS_CORRECT = 0
    WORDS_WRONG = 0
    for widget in window.winfo_children():
        widget.destroy()
    tk.Label (window, image=photo1, bg="black") .grid(row=0, column=0)
    tk.Label (window, text = "Dit is het dictee spel voor Aliyah. Wil je beginnen?", bg="black", fg="white", font="none 12 bold") .grid(row=1)
    tk.Button(window, text="Ja", width=6, command=dictee) .grid(row=2, column=0)
    tk.Button(window, text="Nee", width=6, command=sys.exit) .grid(row=2, column=1)

def dictee():
    """Show random words and ask for input."""
    global ANSWER, WORD
    while True:
        if VOCABULARY == []:
            for widget in window.winfo_children():
                widget.destroy()
            tk.Label (window, image=photo1, bg="black") .grid(row=0, column=0)
            tk.Label (window, text = "Dat waren alle woordjes.", bg="black", fg="white", font="none 12 bold") .grid(row=1, column=0)
            tk.Label (window, text = "Woordjes goed: %d" % WORDS_CORRECT, bg="black", fg="white", font="none 12 bold") .grid(row=2, column=0)
            tk.Label (window, text = "Woordjes fout: %d" % WORDS_WRONG, bg="black", fg="white", font="none 12 bold") .grid(row=3, column=0)
            tk.Label (window, text = "Wil je het nog een keer proberen?", bg="black", fg="white", font="none 12 bold") .grid(row=4, column=0)
            tk.Button(window, text="Ja", width=6, command=main) .grid(row=5, column=0)
            tk.Button(window, text="Nee", width=6, command=exit_game) .grid(row=5, column=1)
        else:
            for widget in window.winfo_children():
                widget.destroy()
            WORD = random.choice(VOCABULARY)
            tk.Label (window, image=photo1, bg="black") .grid(row=0, column=0)
            tk.Label (window, text = "{}".format(WORD), bg="black", fg="white", font="none 20 bold") .grid(row=1, column=0)
            ANSWER = tk.Entry(window, width=20, bg="white")
            ANSWER.grid(row=2, column=0)
            tk.Button(window, text="Check", width=6, command=check) .grid(row=3, column=0)
            WORDS_PASSED.append("{}".format(WORD))
            VOCABULARY.remove("{}".format(WORD))

def check():
    '''Cross check word shown with answer given'''
    global ANSWER, WORDS_CORRECT, WORDS_WRONG
    if ANSWER == WORD:
        tk.Label (window, text = "Wat goed!", bg="black", fg="white", font="none 12 bold") .grid(row=4, column=0)
        WORDS_CORRECT += 1
    else:
        tk.Label (window, text = "Jammer!", bg="black", fg="white", font="none 12 bold") .grid(row=4, column=0)
        WORDS_WRONG += 1

    tk.Label (window, text = "Wil je het nog een keer proberen?", bg="black", fg="white", font="none 12 bold") .grid(row=4, column=0)
    tk.Button(window, text="Ja", width=6, command=dictee) .grid(row=5, column=0)
    tk.Button(window, text="Nee", width=6, command=exit_game) .grid(row=5, column=1)

def exit_game():
    '''summarize results and exit after pushing enter'''
    for widget in window.winfo_children():
        widget.destroy()
    tk.Label (window, image=photo1, bg="black") .grid(row=0, column=0)
    tk.Label (window, text = "Tot de volgende keer.", bg="black", fg="white", font="none 12 bold") .grid(row=1, column=0)
    tk.Label (window, text = "Woordjes goed: %d" % WORDS_CORRECT, bg="black", fg="white", font="none 12 bold") .grid(row=2, column=0)
    tk.Label (window, text = "Woordjes fout: %d" % WORDS_WRONG, bg="black", fg="white", font="none 12 bold") .grid(row=3, column=0)
    tk.Label (window, text = "Klik op OK om af te sluiten", bg="black", fg="white", font="none 12 bold") .grid(row=4, column=0)
    tk.Button(window, text="OK", width=6, command=sys.exit) .grid(row=5, column=0)


if __name__ == "__main__":
    main()

window.mainloop()```

【问题讨论】:

  • 我认为dictee() 中的while 循环是不必要的,应该删除。
  • @acw1668 老兄!我多么愚蠢 :-) 你是完全正确的,两者之间的选择已经把它带回 dictee() :-) 它是固定的 :-) 我如何将其标记为答案?

标签: python-3.x macos tkinter


【解决方案1】:

dictee() 中的 while 循环阻止了 tkinter 更新,从而导致了问题。

对于您的情况,它不是必需的,可以删除。

【讨论】:

  • 谢谢!虽然现在我看到另一个问题.. ANSWER 与 WORD 的检查无法正常工作。它的行为就像他们不匹配时一样,它继续 {else} 我还在全局定义中添加了 WORD,但它不起作用。你看到什么了吗?
  • 问题是由if ANSWER == WORD: 内的check() 行引起的。因为ANSWEREntery,所以它应该是if ANSWER.get() == WORD:
猜你喜欢
  • 2018-02-08
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多