I have a block of code which choose a word from a list and displays it on a label and the user has to retype it correctly to move on.
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()
I've been using this same code throughout ALL versions of my code since I started working on it a few months ago. I haven't altered it one bit and it's worked perfectly up until now. The error is:
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'
I noticed this error last week as it occurred rarely but now I can't get past the first word without it throwing this error. My code has gone through MAJOR changes throughout the past months but I have left these functions and anything related to them completely untouched and I have no idea why it worked perfectly for 3 months then has now stopped working.
I've tried the above code and it works perfectly but when I run it inside my full program I get the error despite nothing else being related to the functions I mentioned.
I've tried included even more of my program (which I hope is not too much) but it still won't throw the same error:
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()
This is everything related to this function and I can't reproduce the error. I won't post my full program since it is way too long so is there anything else I can try?
Aucun commentaire:
Enregistrer un commentaire