Necesito ayuda con un problema que me ha surgido, estoy diseñando un juego de BlackJack y tengo un problema con la actualización de un frame de Tkinter. El programa, cuando es ejecutado, crea una nueva ventana que es la interfaz para jugar, y se muestra de la siguiente manera:
Estoy empezando a configurar el juego y estoy programando la función que se ejecuta cuando presiono el botón de "Bet $5". El problema es que en la función que se ejecuta cuando lo presiono se actualizan dos variables:
- La variable "My_money_variable" que almacena el valor del dinero que posee el jugador
- La variable "the_pot_value" que almacena el valor del dinero que se está apostando
Lo he estado comprobando y es cierto que esos valores, declarados como variables globales, se llegan a actualizar. El problema es que en la interfaz de la imagen anterior, el valor de "My money" (que en este caso se puede ver que es 90$) no se actualiza, y el valor de "The Pot" (que en este caso es 0$") tampoco se llega a actualizar. No se qué problema existe con el código. A continución lo comparto y sería de enorme ayuda si alguien pudiese echarme un cable.
import sys
import tkinter
import tkinter.messagebox
import os
import random
class Card:
def __init__(self,numero):
self.numero = numero
def isFaceDown(self):
if self.descubierta == True:
return True
else:
return False
def setFaceDown(self):
self.descubierta = False
def setFaceUp(self):
self.descubierta = False
def getBlackJackRank(self):
if ((self.numero == 11) or (self.numero == 12) or (self.numero == 13) or (self.numero == 24) or (self.numero == 25) or (self.numero == 26) or (self.numero == 37) or (self.numero == 38) or (self.numero == 39) or (self.numero == 50) or (self.numero == 51) or (self.numero == 52)):
return 10
if ((self.numero == 2) or (self.numero == 15) or (self.numero == 28) or (self.numero == 41)):
return 2
if ((self.numero == 3) or (self.numero == 16) or (self.numero == 29) or (self.numero == 42)):
return 3
if ((self.numero == 4) or (self.numero == 17) or (self.numero == 30) or (self.numero == 43)):
return 4
if ((self.numero == 5) or (self.numero == 18) or (self.numero == 31) or (self.numero == 44)):
return 5
if ((self.numero == 6) or (self.numero == 19) or (self.numero == 32) or (self.numero == 45)):
return 6
if ((self.numero == 7) or (self.numero == 20) or (self.numero == 33) or (self.numero == 46)):
return 7
if ((self.numero == 8) or (self.numero == 21) or (self.numero == 34) or (self.numero == 47)):
return 8
if ((self.numero == 9) or (self.numero == 22) or (self.numero == 35) or (self.numero == 48)):
return 9
if ((self.numero == 10) or (self.numero == 23) or (self.numero == 36) or (self.numero == 49)):
return 10
if ((self.numero == 1) or (self.numero == 14) or (self.numero == 27) or (self.numero == 40)):
return 1
def main():
i = 0
def Hit():
global numero_de_carta_del_jugador
numero_de_carta_del_jugador = numero_de_carta_del_jugador + 1
carta_siguiente = random.choice(List_of_cards)
imagen_siguiente_carta_jugador = (tkinter.PhotoImage(file=(str(carta_siguiente) + ".png"))).subsample(3)
List_of_cards.remove(carta_siguiente)
valor_que_tiene_el_jugador = valor_que_tiene_el_jugador + int(Card.getBlackJackRank(carta_siguiente))
def Stand():
i = 0
def Bet_5():
global My_money_variable
global the_pot_value
if My_money_variable < 5:
print("No funds")
else:
My_money_variable = My_money_variable - 5
the_pot_value = the_pot_value + 5
def Bet_10():
global My_money_variable
global the_pot_value
if My_money_variable < 10:
print("No funds")
else:
My_money_variable = My_money_variable - 10
the_pot_value = the_pot_value + 10
#defino la interfaz abajo
root = tkinter.Tk()
root.title("Black Jack")
root.resizable(width=True,height=True)
bar = tkinter.Menu(root)
fileMenu = tkinter.Menu(bar,tearoff=0)
fileMenu.add_command(label="Exit",command=root.quit)
bar.add_cascade(label="File",menu=fileMenu)
root.config(menu=bar)
cardsFrame = tkinter.Frame(root,borderwidth=1,padx=5,pady=5)
cardsFrame.pack()
entryFrame = tkinter.Frame(cardsFrame)
entryFrame.pack(side=tkinter.RIGHT)
zona_Cartas = tkinter.Frame(cardsFrame)
zona_Cartas.pack(side=tkinter.LEFT)
tkinter.Button(entryFrame,text="Hit", command=Hit).grid( row =0 , column =0, columnspan = 2)
tkinter.Button(entryFrame,text="Stand", command=Stand).grid( row =1 , column =0, columnspan = 2)
tkinter.Button(entryFrame,text="Bet $5", command=Bet_5).grid( row =2 , column =0, columnspan = 2)
tkinter.Button(entryFrame,text="Bet $10", command=Bet_10).grid( row =3 , column =0, columnspan = 2)
My_money_Label = tkinter . Label (entryFrame, text ="My Money")
My_money_Label . grid ( row =4 , column =0 )
My_money_value = tkinter . Label (entryFrame, text = str(My_money_variable) + "$")
My_money_value . grid ( row =4 , column =1 )
The_Pot_Label = tkinter . Label (entryFrame , text ="The Pot")
The_Pot_Label . grid ( row =5 , column =0)
The_Pot_value = tkinter . Label (entryFrame , text = str(the_pot_value) + "$" )
The_Pot_value . grid ( row =5 , column =1 )
Card_Totals_Label = tkinter . Label ( entryFrame , text ="Card Totals")
Card_Totals_Label . grid ( row =6 , column =0 , columnspan = 2)
Player_Label = tkinter . Label ( entryFrame , text ="Player")
Player_Label . grid ( row =7 , column =0 )
Player_value = tkinter . Label ( entryFrame)
Player_value . grid ( row =7 , column =1 )
Dealer_Label = tkinter . Label ( entryFrame , text ="Dealer")
Dealer_Label . grid ( row =8 , column =0 )
Dealer_value = tkinter . Label ( entryFrame)
Dealer_value . grid ( row =8 , column =1 )
List_of_cards = list(range(1,52))
#reparto las dos cartas al jugador
carta_1_jugador = random.choice(List_of_cards)
List_of_cards.remove(carta_1_jugador)
carta_2_jugador = random.choice(List_of_cards)
List_of_cards.remove(carta_2_jugador)
#reparto las dos cartas al dealer
carta_1_dealer = random.choice(List_of_cards)
List_of_cards.remove(carta_1_dealer)
carta_2_dealer = random.choice(List_of_cards)
List_of_cards.remove(carta_2_dealer)
#inserto las imagenes de las cartas
imagen_carta_1_jugador = (tkinter.PhotoImage(file="parte_de_atras.png")).subsample(3)
imagen_carta_2_jugador = (tkinter.PhotoImage(file="parte_de_atras.png")).subsample(3)
imagen_carta_1_dealer = (tkinter.PhotoImage(file="parte_de_atras.png")).subsample(3)
imagen_carta_2_dealer = (tkinter.PhotoImage(file="parte_de_atras.png")).subsample(3)
imagen_carta_a_repartir = tkinter.PhotoImage(file="parte_de_atras.png").subsample(3)
label_imagen_carta_1_jugador = tkinter.Label(zona_Cartas, image = imagen_carta_1_jugador)
label_imagen_carta_1_jugador.grid(row =2, column = 0)
label_imagen_carta_2_jugador = tkinter.Label(zona_Cartas, image = imagen_carta_2_jugador)
label_imagen_carta_2_jugador.grid(row =2, column = 1)
label_imagen_carta_1_dealer = tkinter.Label(zona_Cartas, image = imagen_carta_1_dealer)
label_imagen_carta_1_dealer.grid(row =0, column = 0)
label_imagen_carta_2_dealer = tkinter.Label(zona_Cartas, image = imagen_carta_2_dealer)
label_imagen_carta_2_dealer.grid(row =0, column = 1)
label_imagen_carta_a_repartir = tkinter.Label(zona_Cartas, image = imagen_carta_a_repartir)
label_imagen_carta_a_repartir.grid(row =1, column = 0)
carta_1_jugador = Card(carta_1_jugador)
carta_2_jugador = Card(carta_2_jugador)
carta_1_dealer = Card(carta_1_dealer)
carta_2_dealer = Card(carta_2_dealer)
valor_que_tiene_el_jugador = carta_1_jugador.getBlackJackRank() + carta_2_jugador.getBlackJackRank()
valor_que_tiene_el_dealer = carta_1_dealer.getBlackJackRank() + carta_2_dealer.getBlackJackRank()
#defino la interfaz arriba
def appClosing():
root.destroy()
root.quit()
sys.exit()
root.protocol("WM_DELETE_WINDOW", appClosing)
tkinter.mainloop()
if __name__ == "__main__":
numero_de_carta_del_jugador = 3
numero_de_carta_del_dealer = 3
the_pot_value = 0
My_money_variable = float(input("Please enter the money you want to start with: "))
main()
Pego todo el código para que puedan entender el contexto, pero lo importante es la función Bet_5() que es la que rige el funcionamiento cuando selecciono el botón "Bet $5"
Sería de enorme ayuda si pudieseis ayudarme a encontrar por qué no se actualiza en la ventana.
mainloop
sobre el objetotkinter
, me parece debes usarlo sobre el objetoroot
, es decir:root.mainloop()
.label.config(image=imagen)