Привет. Я с трудом пытаюсь понять, как настроить кнопку, которая появляется, когда у меня заканчиваются догадки или когда я выигрываю игру. Я также хотел бы, чтобы эта кнопка сохраняла количество побед, поражений и предположений, которые я оставил. У меня в настоящее время есть, так что он считает до 0, но я не думаю, что у меня есть что-нибудь еще. Я был бы очень благодарен за помощь.
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()