tkinter python hangman游戏到GUI界面

时间:2016-05-22 03:05:17

标签: linux user-interface python-3.x tkinter

为什么要打印到终端而不是网格?有人可以解决它或告诉我我是如何尝试和失败几个小时现在.. 另一个是在我的代码中它有“insert_line(输出,”猜猜单词“,'。')”无论如何都有这样的话,在猜出它出现你需要猜出的单词之后呢?

链接到代码:http://ideone.com/MjDeZF

from tkinter import *
from tkinter import ttk

def insert_line(text, user, line):
  """ A function to allow adding lines to the conversation easier"""
  text.insert("end", str(user) + ": " + str(line) + "\n")

# Set up window
root = Tk()
root.title("Hangman")
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)

variable = StringVar()

# Set up widgets
content = ttk.Frame(root)
output = Text(content)
chat_scroll = ttk.Scrollbar(content, orient = VERTICAL, command =         output.yview)
output["yscrollcommand"] = chat_scroll.set
label = ttk.Label(content, text = "guess:")  # you'd really set this when the     user logs in
entry = ttk.Entry(content, textvariable = variable)
button = ttk.Button(content, text = "send")

#place widgets
f_label = ttk.Label(frame, text='welcome to hangman')
f_label.grid(column=0, row=0, sticky=E)
output.grid(row = 1, column = 1, columnspan = 2, sticky = (N, S, W, E))
chat_scroll.grid(row = 1, column = 3, sticky = (N, S, W))
label.grid(row = 2, column = 1, sticky = E)
entry.grid(row = 2, column = 2, sticky = (E, W))
button.grid(row = 2, column = 3, columnspan = 2, sticky = W, pady = 10)
content.grid(sticky = (N, S, W, E))

# Configure resizing
root.columnconfigure(0, weight = 1)
root.rowconfigure(0, weight = 1)

content.columnconfigure(1, weight = 1)
content.columnconfigure(2, weight = 3)
content.columnconfigure(3, weight = 1)
content.columnconfigure(4, weight = 3)
content.columnconfigure(5, weight = 1)

content.rowconfigure(1, weight = 1)

insert_line(output, "                   Guess the word", '.')

#setting up the send button
def send_pressed():
        player_guess = entry.get()
        insert_line(output, " send_pressed:", player_guess)
        entry.delete(0, END)
        entry.focus_set()
button["command"] = send_pressed

#importing the game
import random

frame.grid

play_again = True

while play_again:


        words = ["hangman", "chairs", "backpack", "bodywash", "clothing",
                 "computer", "python", "program", "glasses", "sweatshirt",
                 "sweatpants", "mattress", "friends", "clocks", "biology",
                 "algebra", "suitcase", "knives", "ninjas", "shampoo"
                 ]

        chosen_word = random.choice(words).lower()
        player_guess = None
        guessed_letters = []
        word_guessed = []
        for letter in chosen_word:
        word_guessed.append("-")
        joined_word = None

        HANGMAN = (
"""
""",
"""
try again...
""",
"""
not this time
""",
"""
you can do it
""",
"""
keep going
""",
"""
there's still a chance
""",
"""
6 times the charm?
""",
"""
or 7...
""",
"""
maybe not...
""",
"""
you can still do it!
""",
"""
better luck next time.
""")

        print(HANGMAN[0])
        attempts = len(HANGMAN) - 1


        while (attempts != 0 and "-" in word_guessed):
            print(("\nYou have {} attempts remaining").format(attempts))
            joined_word = "".join(word_guessed)
            print(joined_word)

            try:
                player_guess = str(input("\nPlease select a letter between A-Z" + "\n> ")).lower()
            except:
                print("That is not valid input. Please try again.")
                continue
            else:
                if not player_guess.isalpha():
                    print("That is not a letter. Please try again.")
                    continue
                elif len(player_guess) > 1:
                    print("That is more than one letter. Please try again.")
                continue
                elif player_guess in guessed_letters:
                    print("You have already guessed that letter. Please try again.")
                    continue
               else:
                    pass

            guessed_letters.append(player_guess)

            for letter in range(len(chosen_word)):
                if player_guess == chosen_word[letter]:
                    word_guessed[letter] = player_guess

            if player_guess not in chosen_word:
                attempts -= 1
                print(HANGMAN[(len(HANGMAN) - 1) - attempts])

        if "-" not in word_guessed:
            print(("\nCongratulations! {} was the word").format(chosen_word))
        else:
            print(("\nUnlucky! The word was {}.").format(chosen_word))

        print("\nWould you like to play again?")

        response = input("> ").lower()
        if response not in ("yes", "y", "yeah", "tk"):
            play_again = False

if __name__ == "__main__":
    main()




# start main loop
root.mainloop()

0 个答案:

没有答案