i have block of code choose word list , displays on label , user has retype correctly move on.
import random try: import tkinter tk except importerror: import tkinter 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 same code throughout versions of code since started working on few months ago. haven't altered 1 bit , it's worked until now. 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 error last week occurred can't past first word without throwing error. code has gone through major changes throughout past months have left these functions , related them untouched , have no idea why worked 3 months has stopped working.
i've tried above code , works when run inside full program error despite nothing else being related functions mentioned.
i've tried included more of program (which hope not much) still won't throw same error:
try: import tkinter tk except importerror: import tkinter 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(): 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 related function , can't reproduce error. won't post full program since way long there else can try?
as mentioned, above code seems run correctly, error receiving tells somewhere else, code (or code you're using elsewhere) declaring 'global word' , reassigning generator.
edit:
reading comment below triggered additional thought; since 'word' not declared in check_entry, , check_entry bound tkinter event, possibly manifest issue without external redeclaration of "word" variable. not familiar tkinter's codebase, depending on how trigger/store events, there may promissory return (generator's yield) call represents 'word' in bound state. you test theory declaring 'word' global within check_entry function, though still recommend proposed oopier solution instead of shortcut, if works
i suggest refactor use class here avoid using globals fix issue. see below, please note not perfect refactor; should try isolate tk inter work separate class per practices:
try: import tkinter tk except importerror: import tkinter tk import time import random class myprogram(object): def __init__(self): self.word = none self.start_time = none self.correct_words = [] self.words = ['basic', 'christmas', 'summer', 'sports', 'winter', 'negative', 'beach', 'country', 'christmas', 'food', 'games', 'music', 'family'] #self.words = ["test"] self.time_score = 0 self.word_count = 0 self.max_words = len(self.words) self.skips = 0 self.total_words = 0 self.words_found = 0 self.setup_tk_components() def setup_tk_components(self): self.go_btn = tk.button(root, text="go!", command=self.display, width=17) self.go_btn.pack() self.label = tk.label(root, font=("helvetica", 60)) self.time_score_label = tk.label(root, text="time: " + str(self.time_score) + "s", font=('helvetica', 14)) self.word_counter = tk.label(root, text="words: " + str(self.word_count), font =("helvetica", 14)) self.skip_counter = tk.label(root, text="skips: " + str(self.skips), font =("helvetica", 14)) self.wrong_label = tk.label(root, text="", font =("helvetica, 14")) self.entry = tk.entry() @staticmethod def end_game(): root.destroy() def choose_word(self): self.go_btn.pack_forget() self.start_time = time.time() self.entry.focus_set() if self.word_count < self.max_words: self.word = random.choice(self.words) self.label.config(text=str(self.word.lower())) self.time_score_label.config(text="time: " + str(self.time_score) + "s") self.entry.delete(0, tk.end) else: myprogram.end_game() def check_entry(self, event): if self.entry.get().lower() == self.word.lower(): self.update_right() elif self.entry.get().lower() != self.word.lower(): self.update_wrong() if len(self.entry.get()) < 1: self.update_skip() self.update_time() self.choose_word() def update_time(self): self.time_score += time.time() - self.start_time self.time_score = round(self.time_score, 2) def update_skip(self): self.skips += 1 self.skip_counter.config(text="skips: " + str(self.skips)) self.wrong_label.config(text="skipped!", fg='red') self.time_score_label.config(text="time: " + str(self.time_score) + "s") def update_right(self): self.word_count += 1 self.words_found += 1 self.words.remove(self.word) self.correct_words.append(self.word) self.time_score_label.config(text="time: " + str(self.time_score) + "s") self.word_counter.config(text="words: " + str(self.word_count)) self.wrong_label.config(text="") def update_wrong(self): self.wrong_label.config(text="wrong!", fg='red') self.time_score_label.config(text="time: " + str(self.time_score) + "s") def display(self): in (self.label, self.time_score_label, self.word_counter, self.skip_counter, self.wrong_label, self.entry): i.pack() self.choose_word() if __name__ == '__main__': root = tk.tk() mp = myprogram() root.bind("<return>", mp.check_entry) root.mainloop() print mp.correct_words
edit: simpler example of happening function changes string int:
def asstr(): global word word = "word" def asint(): global word word = 1 asstr() print word print word.lower() asint() print word print word.lower()
outputs:
word word 1 traceback (most recent call last): ... attributeerror: 'int' object has no attribute 'lower'
Comments
Post a Comment