重启tkinter中的主循环

时间:2017-09-21 05:39:04

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

我是Python和Tkinter的新手。这是我用GUI练习的游戏。我遇到的问题是当重新启动新游戏时,代码允许输家给出第一个猜测。这是一个我无法解释的无意识的加号。我的代码旨在为玩家1提供第一次猜测,但这并不会发生。我已经完成了尽职调查,并认为问题出在reset_game方法和player_guess方法之间的转换中。 请描述为什么我的代码在重新启动游戏时没有给予player1第一个猜测。 如果对格式,结构,干燥问题等有任何意见,我也会很感激。

''' 2 player guessing game with GUI'''

from tkinter import *
from random import randint
import tkinter.messagebox

# create window
root = Tk()
root.title('2 Player Guessing Game')

target_number = randint(1, 100)
message = StringVar()
current_player = ''

# reset target number for a new game
def reset_target_number():
    global target_number
    target_number = randint(1, 100)

# sets current player variable

def set_current_player(num):
    global current_player
    current_player = num

# gets player's guess and toggles turn back and forth. passes guess to game_play method


def player_guess():
    if p1_entry.get():
        print(target_number)
        guess = int(p1_entry.get())
        set_current_player(1)
        game_play(guess)
        p1_entry.delete(0, END)
        p1_entry.config(state=DISABLED)
        p2_entry.config(state=NORMAL)
        p2_entry.focus_set()
        #print(current_player + ' should be 1')
    else:
        guess = int(p2_entry.get())
        set_current_player(2)
        game_play(guess)
        p2_entry.delete(0, END)
        p2_entry.config(state=DISABLED)
        p1_entry.config(state=NORMAL)
        p1_entry.focus_set()
        #print(current_player + ' should be 2')


# evaluates the guess and returns a message to the message_label. also, calls congrats method when a player has
# guessed correctly
def game_play(guess):
    if guess == target_number:
        display_message('Winner!')
        congrats(current_player)
    elif guess < target_number:
        display_message('Too Low')
        print(current_player, ' is low')
    else:
        display_message('Too High')
        print(current_player, ' is high')


# displays appropriate message in message_label
def display_message(result):
    message.set(result)

# opens message box to declare winner. Gives option to exit or play again


def congrats(player_num):

    if player_num == 1:
        winner = "Player 1"
    else:
        winner = 'Player 2'

    answer = tkinter.messagebox.askquestion('Congratulations', winner + ' is the WINNER!! \n Would you like to play again?')

    if answer == 'no':
        root.quit()
    else:
        reset_game()


def reset_game():
    reset_target_number()
    message = StringVar()
    set_current_player(0)
    #print(target_number, message, current_player)
    p2_entry.delete(0, END)
    p1_entry.delete(0, END)
    p2_entry.config(state=DISABLED)
    #print('Did this run?')
    p1_entry.config(state=NORMAL)
    #print('Did this also run?')
    p1_entry.focus_set()

    # create instruction widgets
instruc_label = Label(root, text='Game Instructions')
instruc_label.grid(row=0, column=0, padx=2, pady=2, sticky=W)
instructions = Label(root, text="Players will take turns guessing the   mystery number. The first player to guess correctlyl wins! The other player doesn't win: but that doesn't mean the other player is a loser. It just means that that player did not win this time. No amount of failures determines whether a person is a loser. It is a person's attitude and character towards their failures that determines a winner and a loser.", wraplength=490, justify=LEFT)
instructions.grid(row=1, column=0, columnspan=3, padx=5, pady=5)

# create game play widgets
p1_label = Label(root, text='Player 1')
p1_label.grid(row=2, column=0)
p1_entry = Entry(root, font='Helvetica 44 bold', fg='black', bg='lightyellow', relief=SUNKEN, width=4, state=NORMAL, justify=CENTER)
p1_entry.grid(row=3, column=0)
p1_entry.focus_set()
message_label = Label(root, textvariable=message, fg='white', bg='darkgreen', relief=GROOVE, height=10, width=20)
message_label.grid(row=2, column=1, rowspan=2)
p2_label = Label(root, text='Player 2')
p2_label.grid(row=2, column=2)
p2_entry = Entry(root, font='Helvetica 44 bold', fg='black', bg='lightyellow', relief=SUNKEN, width=4, state=DISABLED, justify=CENTER)
p2_entry.grid(row=3, column=2)

# create submit button to enter guesses
submit = Button(root, text='Submit', width=20, command=player_guess)
submit.grid(row=4, columnspan=3, pady=5, padx=5)

root.mainloop()

马洛!

1 个答案:

答案 0 :(得分:0)

player_guess中,获胜者在game_play中决定。

如果有胜利者,您正在使用reset_game重置游戏,但您的代码会在game_play返回后继续并更改条目状态。 game_play应该是player_guess应该做的最后一件事。

def player_guess():
    if p1_entry.get():
        ....
        game_play(guess)
    else:
        ....
        game_play(guess)

或者只是将game_play移出if-elses,因为无论如何都要调用它。

def player_guess():
    if p1_entry.get():
        ....
        guess = int(p1_entry.get())
    else:
        ....
        guess = int(p2_entry.get())

    game_play(guess)