嗨我正在努力弄清楚当我用完猜测或赢得比赛时如何设置一个按钮。我也希望这个按钮能够保留我所留下的胜利,损失和猜测。我目前已经拥有它,因此它将猜测计数到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()
答案 0 :(得分:0)
我找到了一些东西:
我在调用函数submit()
时收到错误:
TypeError: submit() missing 1 required positional argument: 's'
因为我按下按钮时没有传递任何参数。由于你不能将参数用于任何事情,你可以将它写成:
def submit(*s):
星号告诉函数接受零参数并将它们存储在s中。
与stop()
功能相同。我也认为破坏窗口可能比使用sys.exit()
更好。
def stop(*e):
window.destroy()
当你给这些buttoans一个回调函数时,你也不需要将它们绑定到鼠标按钮
s.bind('<Button-1>', submit) # Not necessary
设置按钮
至于按钮,你可以创建它,然后忘记它,直到你想再次显示它:
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
然后在submit()
功能中,您可以再次显示它。
if guesses == 0:
...
t.grid() # It will remember its place
存储获胜,损失和猜测量的最简单方法是您已经拥有的变量。然后编写try_again()
函数并让它为变量赋值。