python tkinter二十一点游戏,添加两个变量时的typeerror

时间:2012-11-05 00:24:16

标签: python python-2.7 tkinter typeerror

这是我的代码:

from __future__ import division 
from Tkinter import *
import tkMessageBox
import os
import subprocess
from random import randrange
from random import shuffle
from random import choice
from time import sleep
import sys, time
from select import select


class MyApp(object):

    a = choice(range(1, 12))
    b = choice(range(1, 12))
    money = 100
    bet = 5
    hand = 0


    def __init__(self):
        self.root = Tk()
        self.root.wm_title("The Game of 21")
        Label(self.root, text="Balance: %d" %(self.money)).grid(row=0, column=0, sticky=E)
        Label(self.root, text="Choose Bet:").grid(row=2, column=0, sticky=E)
        Button(self.root, text="5", command=self.bet).grid(row = 2, column = 1)  #create a button
        Button(self.root, text="10", command=self.bet).grid(row = 2, column = 2)  #create a button
        Button(self.root, text="25", command=self.bet).grid(row = 2, column = 3)  #create a button
        Button(self.root, text="50", command=self.bet).grid(row = 2, column = 4)  #create a button

        Button(self.root, text="Deal", command=self.gameOn).grid(row = 5, column = 1)  #create a button
        Button(self.root, text="Exit", command=self.root.destroy).grid(row = 5, column = 2)  #create a button

        self.root.mainloop()

    def gameOn(self):
        """Setup the application's main window as a 2x5 grid"""
        self.root = Tk()
        self.root.wm_title("The Game of 21")
        self.bet = DoubleVar()

        self.cards = StringVar()
        Label(self.root, text = 'Cards dealt:').grid(row = 1, column = 0, sticky = W) #cards dealt
        Label(self.root, text = self.a).grid(row = 1, column = 1, sticky = E)        # card 1 (aka card a)
        Label(self.root, text = ',').grid(row = 1, column = 2)      # inserting a comma between card1 and card2
        Label(self.root, text = self.b).grid(row = 1, column = 3)        # card 2 (aka card b)
        Label(self.root, text = 'Total = %d' %(self.a + self.b + self.hand)).grid(row = 1, column = 4)  # card 1 + card 2
        Label(self.root, text = "Time remaining:").grid(row = 3, column = 1) 

        Button(self.root, text="Hit", command=self.hit).grid(row = 2, column = 0)  #create a button
        Button(self.root, text="Stand" , command=self.stand).grid(row = 2, column = 1) # create a button




#   def bet(betAmount):
#       self.bet = 10

    def hit(self):
        self.hand = choice(range(1, 12))  
        total = self.hand + self.a + self.b 
        dealerHand = choice(range(14,25))


        self.root.destroy()

        result = "Dealer wins. You lose."
        if dealerHand > 21 and total < 22: 
            self.money = self.money - self.bet
            result = "Winner! Winner! Chicken Dinner!"
        if total > 21:
            result
            self.money = self.money - self.bet
        elif total > dealerHand:
            self.money = self.money + self.bet
            result = "Winner! Winner! Chicken Dinner!" 
        elif total == dealerHand: 
            self.money = self.money - self.bet
            result = "Dealer wins. You lose." 
        tkMessageBox.showinfo("Results" , "Hand = %d\nDealer = %d\n%s" %((self.hand + self.a + self.b), (dealerHand), result)) # print result


    def stand(self): 
        self.hand = 0
        total = self.hand + self.a + self.b 
        dealerHand = choice(range(14,25))    
        self.root.destroy()

        result = "Dealer wins. You lose."
        if dealerHand > 21 and total < 22: 
            self.money += bet
            result = "Winner! Winner! Chicken Dinner!"
        if total > 21: 
            self.money += bet
            result
        elif total > dealerHand: 
            self.money += bet
            result = "Winner! Winner! Chicken Dinner!" 
        elif total == dealerHand: 
            self.money += bet
            result = "Dealer wins. You lose." 
        print self.money
        tkMessageBox.showinfo("Results" , "Your hand: %d\nDealer: %d\n%s" %((self.hand + self.a + self.b), (dealerHand), result)) # print result




MyApp()

基本上我要做的是创建一个二十一点类型的游戏,我想要有四个按钮(我已经制作过),当你点击它们时它改变了bet变量。即使我尝试将默认变量设置为5也不行,我的问题就知道了。我得到一个TypeError。我也不知道如何在货币上涨和下跌时更新第一个窗口的“余额”。对于凌乱的代码感到抱歉,这是漫长的一天,我想我已经尽可能地完成了自己的工作。

编辑:弹出的错误是:

line 75, in hit
    self.money = self.money - self.bet
TypeError: unsupported operand type(s) for -: 'int' and 'instance'

1 个答案:

答案 0 :(得分:1)

self.betDoubleVar。要获取bet所持有的数据,您需要self.bet.get()