So basically im making blackjack for a high school project, Im trying to make the it and stand button now and i have all the functions, they worked in the non gui version and now they dont work with the gui. Heres my code any help will be appreciated:
# BlackJack
# Import required modules
import tkinter as tk
import tkinter.messagebox
import random
# Customize main window
root = tk.Tk()
root.title = ('Redemption BlackJack')
root.geometry('900x600') # This moves it to the middle of the screen becuase there is no title bar to move it with
root.resizable(False,False)
root.configure(bg='green')
# Create the frames and pack them
tframe = tk.LabelFrame (root, borderwidth = 0, bg='grey19')
tframe.pack(fill=tk.X, expand = False, side = 'top', pady=15)
bframe = tk.LabelFrame (root, borderwidth = 0, bg='grey19')
bframe.pack(fill=tk.X, expand = False, side='bottom', pady=15)
# Deck value and variables
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10,
2, 3, 4, 5, 6, 7, 8, 9, 10,
2, 3, 4, 5, 6, 7, 8, 9, 10,
2, 3, 4, 5, 6, 7, 8, 9, 10,
'J', 'Q', 'K', 'A', 'J', 'Q',
'K', 'A', 'J', 'Q', 'K', 'A',
'J', 'Q', 'K', 'A']
dealer_cards = []
player_cards = []
redemption_cards = []
player = 0
dealer = 0
# Functions needed to run the game
def dealcard(turn):
card = random.choice(deck)
turn.append(card)
deck.remove(card)
# Total value and give face cards value
def totalvalue(turn):
totalvalue = 0
facecards = ['J', 'Q', 'K']
for card in turn:
if card in range(1, 11):
totalvalue += card
elif card in facecards:
totalvalue += 10
else: # Chooses best value (11 / 1) for the Ace card
if totalvalue > 11:
totalvalue += 1
else:
totalvalue += 11
return totalvalue
# Scoreboard
def stats():
print('_____________________')
print(f'Player\t\tDealer\n{player}\t\t{dealer}')
print('---------------------')
# Redemption / new feature function
def redemption():
total = 0
if totalvalue(player_cards) > 21:
input('Please claim your redemption by pressing any key')
playerforcehit = (input('Please hit below \n1.Hit'))
if playerforcehit == '1':
dealcard(redemption_cards)
if totalvalue(redemption_cards) >= 5:
total = totalvalue(player_cards) - totalvalue(redemption_cards)
print(f"The redemption card value is: {totalvalue(redemption_cards)}")
print(f"Chance of redemption has been claimed, your new cards are {player_cards}, {redemption_cards} ")
print(f"Your final total value is {total} ")
else:
total = totalvalue(player_cards) - totalvalue(redemption_cards)
print(f"Seems like you hit and got a number {redemption_cards}, which is less than 5, so let's see what the dealer does.")
return [total]
# Actual game code for the game to work
while True: # Loop
# Variables that need to reset
dealer_cards = []
player_cards = []
redemption_cards = []
for dealing in range(2):
dealcard(dealer_cards)
dealcard(player_cards)
# Create labels to display total card value and player cards
playerscore = tk.Label(bframe, text = f'Players Total:\n{totalvalue(player_cards)}', font = 'times 20 bold', bg='grey19', fg='white')
playerscore.pack(side=tk.LEFT)
playercards = tk.Label(bframe, text = f'Your Cards:\n{player_cards}', font = 'times 20 bold', bg='grey19', fg='white')
playercards.pack(side=tk.LEFT, padx = 75, expand=True)
# Dealer labels
dealerscore = tk.Label(tframe, text = f'Dealers Total:\n{totalvalue(dealer_cards)}', font = 'times 20 bold', bg='grey19', fg='white')
dealerscore.pack(side=tk.LEFT)
dealercards = tk.Label(tframe, text = f'Dealers Cards:\n{dealer_cards}', font = 'times 20 bold', bg='grey19', fg='white')
dealercards.pack(side=tk.LEFT, padx = 75, expand=True)
# Add buttons
quit = tk.Button(root, command=root.destroy, text='Exit Game', height=4, width=20, bg='red3', highlightthickness=2, highlightbackground='black', font='times 13 bold')
quit.pack(side=tk.RIGHT, padx=10)
hit = tk.Button(root, command=dealcard(player_cards), text = 'Hit', height=4, width=20, bg='dodgerblue', highlightthickness=2, highlightbackground='black', font='times 13 bold')
hit.pack(side=tk.RIGHT, padx=10)
stand = tk.Button(bframe, text = 'Stand', height=4, width=20, bg='chocolate', highlightthickness=2, highlightbackground='black', font='times 13 bold')
stand.pack(anchor='se', padx=10)
root.mainloop()
The code I mainly need help on is: command=dealcard(player_cards) this function is not running when i run the GUI.
hit = tk.Button(root, command=dealcard(player_cards), text = 'Hit', height=4, width=20, bg='dodgerblue', highlightthickness=2, highlightbackground='black', font='times 13 bold')
hit.pack(side=tk.RIGHT, padx=10)