将python hangman游戏转换为GUI

时间:2016-05-11 06:24:24

标签: python user-interface tkinter

我已经在python中编写了一个刽子手类游戏,我试图将其转换为GUI程序,到目前为止还没有成功。 这是我试图开始工作的代码:

#!/usr/bin/env python3

import random
from tkinter import *
from tkinter import ttk

def getHint(word, guesses):
    hint = ' '
    for letter in word:
        if letter not in guesses:
            hint += '_ '
        else:
            hint += letter

    return hint

def loadWordList(filename):
    file = open(filename, 'r')
    line = file.read().lower()
    wordlist = line.split()

    return wordlist

def countOccurences(word, guess):
    length = len(word)
    i = 0
    total = 0
    while i < length:
        if guess == word[i]:
            total += 1
        i += 1

    return total

def play():
    word = random.choice(loadWordList('words.txt'))
    guesses = []
    remaining = 10

    while remaining > 0:
        hint = getHint(word, guesses)
        remaining = str(remaining)
        guess = guess.lower()

        while guess.isalpha() == False or len(guess) != 1:
            output = 'Invalid input. Please enter a letter from a-z.'

        if guess in guesses:
            output = 'That letter has already been guessed!'

        else:
            if guess not in word:
                output = 'That letter does not occur in the secret word.'
            else:
                output = 'That is a good guess! ' + str(guess) + ' occurs ' + str(countOccurences(word, guess)) + ' time(s) in the secret word'
                guesses += guess
                remaining -= 1

        if '_' not in getHint(word, guesses):
            result = 'Congratulations! You guessed the secret word: ' + word
            break

    if remaining == 0:
        result = 'Sorry, the secret word was: ' + word

def buttonClicked():
    play()
    getHint(word, guesses)

root = Tk()
root.title('Guessing Game')

frame = ttk.Frame(root, padding='3 3 12 12')
frame.grid(column=0, row=0, sticky=(N, W, E, S))
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

guess = StringVar()
output = StringVar()
hint = StringVar()
remaining = StringVar()
result = StringVar()

a_label = ttk.Label(frame, text='Enter a letter to guess: ')
a_label.grid(column=1, row=1, sticky=E)
a_entry = ttk.Entry(frame, width=7, textvariable=guess)
a_entry.grid(column=2, row=1,sticky=E)

b_label = ttk.Label(frame, textvariable=output)

c_label = ttk.Label(frame, text='Current Hint: ')
c_label.grid(column=1, row=3, sticky=E)
d_label = ttk.Label(frame, textvariable=hint)
d_label.grid(column=2, row=3)

e_label = ttk.Label(frame, text='Guesses Remaining: ')
e_label.grid(column=1, row=4, sticky=E)
f_label = ttk.Label(frame, textvariable=remaining)
f_label.grid(column=2, row=4)

button = ttk.Button(frame, text='Submit', command=buttonClicked)
button.grid(column=3, row=5)

g_label = ttk.Label(frame, textvariable=result)
g_label.grid(column=1, row=6)


for child in frame.winfo_children():
    child.grid_configure(padx=5, pady=5)

root.bind('<Return>',)

root.mainloop()

这些是我得到的错误:

  • 第77行,在buttonClicked中 - 播放()
  • 第52行,在游戏中 - guess = guess.lower() UnboundLocalError:局部变量&#39; guess&#39;在分配前引用

有关如何进一步重新排列此代码以获得实际连接GUI的功能的任何提示将非常感激。 谢谢!

1 个答案:

答案 0 :(得分:-1)

嗯,就像它说的那样,变量&#34;猜测&#34;没有定义。 你可能想做类似

的事情
guess = word.lower()

而不是

guess = guess.lower()

小提示:使用PEP8,它将帮助其他人阅读您的代码。