我在初学者编码课和我的小组和我的Python代码有点麻烦 .

基本上我们正在制作一个小赌场,但我们到目前为止所有的都是更高/更低的游戏 . 我们在使用tkinter时遇到了一些麻烦 .

我们正在尝试实现它,而不是从头开始创建两个窗口,单击按钮高/低时会打开一个新窗口 . 我们已经尝试了一些事情,但是我们继续得到NameErrors,因为当我们尝试创建一个运行游戏本身然后将其绑定到按钮的新类时,我们之前的类被破坏了 .

我们尝试将最后一行代码(不在函数或类中的代码)放入它们自己的单独类中,但它最终使所有内容复杂化并将其变为混乱 . 我们感谢任何帮助 . 这是我们目前非常粗略的代码 .

import random
import tkinter as tk

NUMOFLABELS = 4

class Card:
# Attributes/state variables:
#   suit = string containing which suit the card is (clubs, spades, hearts, diamonds)
#   value = ??? representing the number/value of the card

    suit_names = ["Diamonds", "Hearts", "Clubs", "Spades"]
    value_names = [None, "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]



    def __init__(self, value = 2, suit = 0):
    #Initialize class
        self.value = value
        self.suit = suit


    def getValue(self):
        return int(self.value)



    def __str__(self):
    #Returns an easy-to-read string description of the card
        return "%s of %s" % (Card.value_names[self.value], Card.suit_names[self.suit])





class Deck:
# Attributes:
#   cards = a list of the card objects from Card class

    def __init__(self):
    #Initializes a deck of 52 cards
        self.cards = []
        for suit in range(4):
            for value in range(1, 14):
                card = Card(value, suit)  #Create new variable for card class
                self.cards.append(card)   #Add each card from the card class into the new list 'cards



    def __str__(self):
    #Creates a string to visualize a new deck of cards
        compileDeck = []
        for card in self.cards:
            compileDeck.append(str(card))
        return '\n'.join(compileDeck)




    def shuffle(self):
    #Shuffles the cards in the deck
        random.shuffle(self.cards)

    def shuffleShow(self):
        random.shuffle(self.cards)
        print(deck.__str__())


    def drawCard(self):
        card1 = self.cards.pop()
        return card1




class HighLow:

    def __init__(self, base, comp, baseV, compV):
        self.base = base
        self.comp = comp
        self.baseV = baseV
        self.compV = compV
        self.Ypos = 15
        self.labelList = [None] * NUMOFLABELS
        for i in range(0, NUMOFLABELS):
            self.labelList[i] = my_canvas1.create_text((300, self.Ypos), text="")
            self.Ypos += 15


    def runGame(self):
        my_canvas1.itemconfig(self.labelList[0], text=self.base)
        my_canvas1.itemconfig(self.labelList[1], text="Do you think the next card is higher, lower, or equal to this card? ")
        my_canvas1.itemconfig(self.labelList[2], text="")
        my_canvas1.itemconfig(self.labelList[3], text="")

    def playAgain(self):
        self.base = deck.drawCard()
        self.baseV = self.base.getValue()
        self.comp = deck.drawCard()
        self.compV = self.comp.getValue()
        HLG.runGame()


    def shuffle(self):
        deck.shuffle()


    def higher(self):
        if self.compV > self.baseV:
            my_canvas1.itemconfig(self.labelList[2], text="Congratulations! You Win!")
        else:
            my_canvas1.itemconfig(self.labelList[2], text="Better luck next time... ")
        my_canvas1.itemconfig(self.labelList[3], text=self.comp)


    def lower(self):
        if self.compV < self.baseV:
            my_canvas1.itemconfig(self.labelList[2], text="Congratulations! You Win!")
        else:
            my_canvas1.itemconfig(self.labelList[2], text="Better luck next time... ")
        my_canvas1.itemconfig(self.labelList[3], text=self.comp)

    def equal(self):
        if self.compV == self.baseV:
            my_canvas1.itemconfig(self.labelList[2], text="Congratulations! You Win!")
        else:
            my_canvas1.itemconfig(self.labelList[2], text="Better luck next time... ")
        my_canvas1.itemconfig(self.labelList[3], text=self.comp)




class Account:
    #Attributes
    #   balance: float, current balance
    #   initial: float, initial account balance
    #   number: int, represents the accounut number


    def __init__(self, bal):
        # initialize both balance and initial to the input bal
        self.balance = bal
        self.initial = bal   #Since balance = initial at begininning

    def getBalance(self):
        # returns the current balance of the account
        return self.balance

    def getInitBalance(self):
        # return the initial balance of the account
        return self.initial

    def deposit(self, amt):
        #adds amt to the current balance and returns the updated balance
        self.balance += amt
        return self.balance

    def withdraw(self, amt):
        # subtracts amt from the current balance and returns the new balance
        #   be sure the current balance is greater than amt first
        if self.balance < amt:
            # if the amount requested was more than the balance, alert
            # the user and withdraw only up to the current balance
            print("Insufficient funds, withdrawing $" + str(self.balance) + " instead of $" + str(amt) + ".")
            amt = self.balance
        self.balance = self.balance - amt
        return self.balance

    def __str__(self):
        # prints message containing initial balance and current balance
        print("The initial balance of the account: ", self.initial)
        print("The current balance of the account: ", self.balance)
















    #######################   HIGH/LOW   ########################

win = tk.Tk()
win.title("High/Low")

my_canvas1 = tk.Canvas(win, width=600, height=500)
my_canvas1.pack()
canvas_table = my_canvas1.create_rectangle(0, 0, 600, 500, fill="green")

#create label for account info
accountLabel = tk.Label(win, text="Account: ") ############# NEED TO CALL ACCOUNT CLASS LATER



    #######################   INITIALIZING GAME   ########################

# Creates a deck and sets it to variable "deck"
deck = Deck()
# Shuffles the deck
deck.shuffle()

# Draws the first card that will act as the base card for the HIGH/LOW 
game, shown to the player
base = deck.drawCard()
# Converts that base card to only its value. All strings taken out. 
Used by the computer
baseV = base.getValue()

# Draws the next card from the deck that will be compared to the first card. Shown to the player
comp = deck.drawCard()
# Converts that comparison card to only its value. All strings taken out. Used by the computer
compV = comp.getValue()

# Initializes the game with the inputs above and stores as variable HLG
HLG = HighLow(base, comp, baseV, compV)





buttonH = tk.Button(win, text="Higher")
buttonH.pack()
buttonH['command'] = HLG.higher

buttonL = tk.Button(win, text="Lower")
buttonL.pack()
buttonL['command'] = HLG.lower

buttonE = tk.Button(win, text="Equal to")
buttonE.pack()
buttonE['command'] = HLG.equal

buttonN = tk.Button(win, text="Play Again")
buttonN.pack()
buttonN['command'] = HLG.playAgain

buttonS = tk.Button(win, text="Shuffle")
buttonS.pack()
buttonS['command'] = HLG.shuffle

quit_button = tk.Button(win, text="Quit")
quit_button.pack()
quit_button['command'] = win.destroy





     #######################   HOME MENU   ########################
                            #Not Finished#

name = str(input("To create a more personalized experience, please enter your name:"))

win = tk.Tk()
win.title("The Casino")

my_canvas = tk.Canvas(win, width=300, height=50)
my_canvas.pack()
welcomeLabel = tk.Label(win, text="Welcome to The Casino, " + name + "!", font='50')
welcomeLabel.pack()
welcomeLabel2 = tk.Label(win, text="Please choose a game to play. ")
welcomeLabel2.pack()



button4 = tk.Button(win, text="High/Low")
button4.pack()
button4['command'] = HLG.runGame