-2

Hi I am having a hard time trying to figure out how to set up a button that appears when I either run out of guesses or when I win the game. I would also like this button to keep the amount of wins, losses, and guesses I have left. I currently have it so it counts down the guesses until 0 but I don not think I have anything else. I would love the help thank you so much.

from tkinter import *
import random
from time import sleep
import sys

randomNumber = random.randint(1,13)
if randomNumber == 11:
    randomNumber = 'Jack'
elif randomNumber == 12:
    randomNumber = 'Queen'
elif randomNumber == 13:
    randomNumber = 'King'
elif randomNumber == 1:
    randomNumber = 'Ace'
randomSuit = ('Clubs Diamonds Hearts Spades' .split())
cardSuit = random.choice(randomSuit)
card = str(randomNumber) + ' of ' + cardSuit

win = 0
loss = 0
guesses = 5

def submit(s):
    global textentry
    global guesses
    global win
    global loss
    entered_text=textentry.get() #collect text from entry box
    output = Label(window, text='', width=30, height=3, bg='white')
    output.grid(row=8, column=0, sticky=N, pady=20)
    textentry.delete(0, END)

    if card.lower() == entered_text.lower():
        output = Label(window, text='You won!', width=30, height=3, bg='white')
        win = win + 1
        wins = Label(window, text='Wins: ' + str(win), bg='#004d00', fg='white') .grid(row=11, column=0, sticky=W)
        losses = Label(window, text='Losses: ' + str(loss), bg='#004d00', fg='white') .grid(row=12, column=0, sticky=W)
        guessesLeft = Label(window, text='Guesses: ' + str(guesses), bg='#004d00', fg='white') .grid(row=13, column=0, sticky=W)
        output.grid(row=8, column=0, sticky=N, pady=20)
        Button(window, text='Submit', width=6, state=DISABLED, relief=FLAT, disabledforeground='white', bg='red', fg='white') .grid(row=4, column=0, sticky=N)

    elif card.lower() != entered_text.lower():
        guesses = guesses - 1
        wins = Label(window, text='Wins: ' + str(win), bg='#004d00', fg='white') .grid(row=11, column=0, sticky=W)
        losses = Label(window, text='Losses: ' + str(loss), bg='#004d00', fg='white') .grid(row=12, column=0, sticky=W)
        guessesLeft = Label(window, text='Guesses: ' + str(guesses), bg='#004d00', fg='white') .grid(row=13, column=0, sticky=W)
        output = Label(window, text='Wrong, try again.', width=30, height=3, bg='white')
        output.grid(row=8, column=0, sticky=N, pady=20)

    if guesses == 0:
        loss = loss + 1
        output = Label(window, text='', width=30, height=3, bg='white')
        output.grid(row=8, column=0, sticky=N, pady=20)
        output = Label(window, text='''You ran out of guesses, better luck
        next time.The correct card was
        ''' + card, width=30, height=3, bg='white')
        output.grid(row=8, column=0, sticky=N, pady=20)
        Button(window, text='Submit', width=6, state=DISABLED, relief=FLAT, disabledforeground='white', bg='red', fg='white') .grid(row=4, column=0, sticky=N)

return

def stop(e):
    exit()

window = Tk()
window.title('Card Guess!')
window.configure(background='#004D00', cursor='heart')

photo1 = PhotoImage(file='card.gif')
Label (window, image=photo1, bg='black') .grid(row=0, column=0, sticky=W)

Label (window, text='''Welcome to The Card Guesser! You will try and attempt     to guess
the card I am thinking of within 5 guesses! Good luck!''', bg='#004d00', fg='white', font='none 12 bold') .grid(row=1, column=0, sticky=N)

Label (window, text='Guess a card. i.e 8 of Spades:', bg='#004d00', fg='white', font='none 12 bold') .grid(row=2, column=0, sticky=N)

textentry = Entry(window, width=20, bg='white')
textentry.grid(row=3, column=0, sticky=N, pady=20)
textentry.focus_set()

Label (window, text='\nOutput:', bg='#004D00', fg='white', font='font 12 bold') .grid(row=7, column=0, sticky=N) #lable for output

output = Label(window, text='', width=30, height=3, bg='white')
output.grid(row=8, column=0, sticky=N, pady=20)

Label (window, text=card) .grid(row=6, column=0, sticky=W) #get rid of

s = Button(window, text='Submit', width=6, command=submit, relief=FLAT, activebackground='red', bg='green', fg='white')
s.grid(row=4, column=0, sticky=N)
window.bind('<Return>', submit)
s.bind('<Button-1>', submit)
#submit button

e = Button(window, text='Exit', width=6, command=stop, relief=FLAT, activebackground='red', bg='green', fg='white')
e.grid(row=10, column=0, sticky=N) #exit button
window.bind('<Escape>', stop)
e.bind('<Button-1>', stop)

wins = Label(window, text='Wins: ' + str(win), bg='#004d00', fg='white')         .grid(row=11, column=0, sticky=W)
losses = Label(window, text='Losses: ' + str(loss), bg='#004d00', fg='white') .grid(row=12, column=0, sticky=W)
guessesLeft = Label(window, text='Guesses: ' + str(guesses), bg='#004d00', fg='white') .grid(row=13, column=0, sticky=W)


window.mainloop()
mase
  • 3
  • 2
  • So basically you just want to show/hide a button widget? (sorry don't have time to fully digest your code). [This](https://stackoverflow.com/questions/3819354/in-tkinter-is-there-any-way-to-make-a-widget-not-visible) answer may help in that respect (unless you already found that one). Having the button *keep* the numer of guesses etc sounds fishy, its a ui element and those things should be stored in your game mode/logic. – Paul Rooney May 01 '18 at 23:01
  • Yeah, I guess I just want to show/hide the try again button. I had it where it restarted everything but then my submit button did not count down my guesses or anything, so I am lost. – mase May 01 '18 at 23:04

1 Answers1

0

I found a few things:

I'm getting an error when I call the function submit():

TypeError: submit() missing 1 required positional argument: 's'

because I'm not passing any arguments when I press the button. Since you don't use the argument for anything you can write it as:

def submit(*s):

The star tells the function to accept zero ar more argument and to store them in s.

The same thing with the stop() function. I also think maybe it is better to destroy the window than to use sys.exit().

def stop(*e):
    window.destroy()

As you are giving these buttoans a callback function you don't need to also bind them to the mouse button

s.bind('<Button-1>', submit)     # Not necessary

set up a button

As for the button you can create it and then forget it until you want to display it again:

t = Button(window, text='Try again', ... , command=try_again)
t.grid(row=12, column=0, sticky=N)   # Did not quite understand your layout
t.grid_remove()     # Hide the button

Then in the submit() function you can show it again.

if guesses == 0:
    ...
    t.grid()        # It will remember its place

The easiest way to store the amount of wins, losses, and guesses is what you already have, in variables. Then write the try_again() function and let it assign new values to your variables.

figbeam
  • 7,001
  • 2
  • 12
  • 18