编程语言
首页 > 编程语言> > python-tkinter和拼字游戏求解器问题

python-tkinter和拼字游戏求解器问题

作者:互联网

我必须学习python 3.3.3的逻辑和设计课程.我对编程非常陌生,下面的代码是ive在10个星期内自己学到的东西的结晶.我的程序在没有GUI的情况下在程序级别上运行良好.我的程序是一个典型的拼字游戏求解器.我刚与tkinter在一起度过了可怕的时光.我需要将输入从主模块输入到主模块,然后将结果输入到列表框.我什至放弃了从主要类传递参数的过程,因此我将机架和结果设置为全局变量.我知道这对某人帮助可能是很麻烦的,但是我在总决赛之前完成此工作的任何帮助将不胜感激.先感谢您.

#only reason these are global is because im having trouble with tkinter and
#passing the proper parameters to the main module

global results
global rack

import tkinter as tk

#class for GUI
class application:
    def __init__(self):


       self.main=tk.Tk()


         #top fram includes rack label, rack entry, rack enter button and quit button
       self.top_frame = tk.Frame(self.main)

       self.main.title('Scrabble Solver')
       self.main.geometry('300x300')
       self.main.wm_iconbitmap('favicon.ico')

       self.racklbl = tk.Label(self.top_frame, text="Enter your rack")
       self.racklbl.pack()

       self.rackent=tk.Entry(self.top_frame)
       self.rackent.pack(side="left")

       self.rackbtn = tk.Button(self.top_frame, text = "Enter",
command=self.getRackData)
       self.rackbtn.pack(side="left")

       self.top_frame.pack()



       #bottom frame includes listbox for results display and scrollbar

       self.bot_frame = tk.Frame(self.main)

       self.validlist = tk.Listbox(self.bot_frame, width=30)
       self.validlist.pack(side="left")

       self.scrollbar = tk.Scrollbar(self.bot_frame)
       self.scrollbar.pack(side="right", fill="y")

       self.QUIT = tk.Button(self.top_frame, text="QUIT",                       fg="red", command=self.main.destroy)
       self.QUIT.pack(side='left')

       self.bot_frame.pack()

       tk.mainloop()


    def showError(self):
        tk.messagebox.showinfo('You have entered too many letters')
    def getRackData(self):
        rack = input(self.rackent.get())



def main():
    rack=""
    gui = application() 


    #dictionary for the scores
    scores = {"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}


    #get the rack letters
    #rack = getRackLetters(gui)
    #call module to put file into an array
    rack = getRackLetters(rack,gui)
    putFileIntoArray(rack, scores)


# function to get rack letters 
def getRackLetters(rack,gui):
    rack = rack.upper()
    #call function to verify number of letters entered
    verify= verifyRackLetters(rack)
    if verify == True:
        return rack
    else:
        gui.showError()
        main()
#function to verify number of letters entered    
def verifyRackLetters(rack):
    if len(rack) <= 8:
        verify = True
    else:
        verify = False
    return verify

#module to put file into an array    
def putFileIntoArray(rack, scores):
    words = []
    file = open("dict.dat", "r")   

    for line in file:
        line = line.strip()
        words.append(line)

    file.close()
    #call module to find and create an array of valid words then score them
    findValidWords(words, scores)



# module to find and create an array of valid words then score them
def findValidWords(words, rack, scores):

    valid = []
    for word in words:

        candidate = True
        rack_letters = list(rack)

        for letter in word:
            if letter not in rack_letters:
                candidate = False
            else:
                rack_letters.remove(letter)
        #score the valid words and append to list
        if candidate == True:
            total = 0
            for letter in word:
                total = total + scores[letter]
            valid.append([total, word])

    #call module to sort and print the valid words list with scores
    scoreValidWords(valid)

    #module to sort and print the list
def scoreValidWords(valid):
valid.sort()

    for entry in valid:
        score = entry[0]
        word = entry[1]
        results.append(str(score) + " " + word)


    print(results)
main()

解决方法:

尽管我删除了滚动条(我想您也会想出如何添加滚动条的方法),但我还是完全重写了您的代码,使其更具Python风格,并且更易于阅读和编写.但是,我尝试尽可能地坚持您的代码.

我想它现在正在工作:

这是我使用的dictionary.dat:

beast
lemon
ape
apple
sea
pea
orange
bat

这是代码本身:

# Import python modules
import tkinter
import tkinter.messagebox
from collections import Counter

# Module level constants
SCORES = {'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()

标签:python-3-3,tkinter,python,class
来源: https://codeday.me/bug/20191030/1965349.html