tkinter and scrabble solver problems
Although I removed the scrollbar ( I guess you will figure out how to add that too) I completely rewrote your code, to be more pythonic, and easier to read and write. However I tried to stick to your code as much as possible.
I guess it is working now:
Here is the dictionary.dat
I used:
beastlemonapeappleseapeaorangebat
And here is the code itself:
# Import python modulesimport tkinterimport tkinter.messageboxfrom collections import Counter# Module level constantsSCORES = {'A': 1, 'C': 3, 'B': 3, 'E': 1, 'D': 2, 'G': 2, 'F': 4, 'I': 1, 'H': 4, 'K': 5, 'J': 8, 'M': 3, 'L': 1, 'O': 1, 'N': 1, 'Q': 10, 'P': 3, 'S': 1, 'R': 1, 'U': 1, 'T': 1, 'W': 4, 'V': 4, 'Y': 4, 'X': 8, 'Z': 10}class App(tkinter.Tk): def __init__(self, word_list, *args, **kwargs): # Initialize parent class by passing instance as first argument tkinter.Tk.__init__(self, *args, **kwargs) # Store values self.word_list = word_list # Setup main window self.title('Scrabble Solver') # Create widgets rack_label = tkinter.Label(self, text='Enter your rack') self.rack_entry = tkinter.Entry(self) rack_button = tkinter.Button(self, text='Enter', command=self.search) quit_button = tkinter.Button(self, text='Quit', command=self.quit) self.valid_list = tkinter.Listbox(self, width=40, font='Courier') # Place widgets rack_label.grid(row=0, column=0, sticky=tkinter.W) self.rack_entry.grid(row=1, column=0, sticky=tkinter.W) rack_button.grid(row=1, column=1, sticky=tkinter.W) quit_button.grid(row=1, column=2, sticky=tkinter.W) self.valid_list.grid(row=2, columnspan=3, sticky=tkinter.W) def run(self): # Enter event loop self.mainloop() def error(self): # Throw and error message dialog tkinter.messagebox.showinfo('You have entered too many letters!') def search(self): # Cleanup up valid list self.valid_list.delete(0, tkinter.END) # Get data of entry, and make them lower case rack = self.rack_entry.get().lower() # Check length of data if len(rack) <= 8: return self.find_valid_words(rack) self.error() def find_valid_words(self, rack): # Create a dictionary for valid words and its values valid = {} rack_chars = Counter(rack) for word in self.word_list: word_chars = Counter(word) if word_chars == word_chars & rack_chars: valid[word] = sum(SCORES[letter.upper()] for letter in word) # Sort the results and insert them into the list box if valid: for word, score in sorted(valid.items(), key=lambda v: v[1], reverse=True): self.valid_list.insert(tkinter.END, '{:<10} {}'.format(word, score)) else: self.valid_list.insert(tkinter.END, 'No results found.')if __name__ == '__main__': # Open dictionary file and scan for words with open('dictionary.dat', 'r') as f: all_words = [word for word in f.read().split()] # Create instance and call run method App(all_words).run()