我有一塊代碼從列表中選擇一個單詞並將其顯示在標籤上,用戶必須正確地重新輸入以便繼續前進。突然收到AttributeError
import random
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
WORDS = ['Games', 'Development', 'Keyboard', 'Speed', 'Typer', 'Anything',
'Alpha']
score = 0
def choose_word():
global word
entry.focus_set()
word = random.choice(WORDS)
label.config(text=str(word.lower()))
def check_entry(event):
global score
if entry.get().lower() == word.lower():
score += 1
print(score)
elif entry.get().lower() != word.lower():
score -= 1
print(score)
choose_word()
entry.delete(0, tk.END)
root = tk.Tk()
label = tk.Label(root)
entry = tk.Entry(root)
label.pack()
entry.pack()
choose_word()
root.bind('<Return>', check_entry)
root.mainloop()
自從我幾個月前開始研究它以來,我一直在使用此代碼貫穿所有版本的代碼。我一點也沒有改變它,直到現在它的工作狀況都很好。錯誤是:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\ernxs\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\ernxs\Downloads\speedtypr\Speedtypr FINAL\speedtyper.pyw", line 685, in choose_word
label.config(text=str(word.lower()))
AttributeError: 'generator' object has no attribute 'lower'
上週我注意到這個錯誤,因爲它很少發生,但現在我不能讓過去的第一個字,沒有它拋出這個錯誤。我的代碼在過去幾個月中經歷了主要更改,但我已將這些功能和任何與它們相關的任何功能完全保留,我不知道爲什麼它在3個月內完美運行,現在已停止工作。
我試過上面的代碼,它完美的工作,但是當我在完整的程序中運行它時,儘管沒有其他與我提到的功能有關的錯誤,但仍然得到錯誤。
我試過包括更我的節目(我希望是不要太多),但它仍然不會拋出同樣的錯誤:
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
import time
import random
correct_words = []
WORDS = ['Basic', 'Christmas', 'Summer', 'Sports', 'Winter', 'Negative',
'Beach', 'Country', 'Christmas', 'Food', 'Games', 'Music', 'Family']
time_score = 0
word_count = 0
max_words = 12
skips = 0
total_words = 0
words_found = 0
def end_game():
root.destroy()
def choose_word():
global word, start_time
go_btn.pack_forget()
start_time = time.time()
entry.focus_set()
if word_count < max_words:
word = random.choice(WORDS)
label.config(text=str(word.lower()))
time_score_label.config(text="Time: " + str(time_score) + "s")
else:
end_game()
def check_entry(event):
if entry.get().lower() == word.lower():
update_right()
elif entry.get().lower() != word.lower():
update_wrong()
if len(entry.get()) < 1:
update_skip()
update_time()
choose_word()
entry.delete(0, tk.END)
def update_time():
global time_score
time_score += time.time() - start_time
time_score = round(time_score,2)
def update_skip():
global skips
skips += 1
skip_counter.config(text="Skips: " + str(skips))
wrong_label.config(text="SKIPPED!", fg='red')
time_score_label.config(text="Time: " + str(time_score) + "s")
def update_right():
global word_count, words_found
word_count += 1
words_found += 1
WORDS.remove(word)
correct_words.append(word)
time_score_label.config(text="Time: " + str(time_score) + "s")
word_counter.config(text="Words: " + str(word_count))
wrong_label.config(text="")
def update_wrong():
wrong_label.config(text="WRONG!", fg='red')
time_score_label.config(text="Time: " + str(time_score) + "s")
def display():
for i in (label, time_score_label, word_counter, skip_counter, wrong_label,
entry):
i.pack()
choose_word()
root = tk.Tk()
go_btn = tk.Button(root, text="GO!", command=display, width=17)
go_btn.pack()
label = tk.Label(root, font=("Helvetica", 60))
time_score_label = tk.Label(root, text="Time: " + str(time_score) +
"s", font=('Helvetica', 14))
word_counter = tk.Label(root, text="Words: " + str(word_count),
font =("Helvetica", 14))
skip_counter = tk.Label(root, text="Skips: " + str(skips),
font =("Helvetica", 14))
wrong_label = tk.Label(root, text="", font =("Helvetica, 14"))
entry = tk.Entry()
root.bind("<Return>", check_entry)
root.mainloop()
這一切與此相關的功能和我無法重現錯誤。我不會發布我的完整程序,因爲它太長了,所以還有其他事情可以嘗試嗎?
一個猜測是你重新定義名稱「單詞」的地方。將該函數中的名稱更改爲word_1或其他內容,看看是否有幫助。 –
label.config(text = str(word.lower()))不使用從「WORDS」列表中選擇的「單詞」,不知何故它與「全局單詞」混淆,如果您重命名它,則不會收到錯誤。 – FatmaT