lundi 2 février 2015

Enabling and Disabling button in Tkinter depending on event

I'm writing a code that contains two buttons.


When the first button(ButtonAlpha) is pressed, a D6 is rolled. If the result is higher than or equal to a defined value(AlphaPass), it increases a variable(AlphaCount) by one.


Once AlphaCount is equal to or more than a value(BetaThreshold), the second button(ButtonBeta, which is disabled by default) should be enabled.



import random
from Tkinter import *

Axb = Tk()

AlphaCount = IntVar()
BetaCount = IntVar()
SuccessCheckAlpha = IntVar()
AlphaPass = 2
BetaPass = 4
BetaThreshold = 5

def onClickAlpha():
SuccessCheckAlpha = random.randrange(0, 7)
if SuccessCheckAlpha >= AlphaPass:
AlphaCount.set(AlphaCount.get() + 1)
print AlphaCount

def onClickBeta():
SuccessCheckBeta = random.randrange(0, 7)
if SuccessCheckBeta >= BetaPass:
BetaCount.set(BetaCount.get() + 1)
print BetaCount

DisplayAlphaLabel1 = Label(Axb, text = 'Alpha Count: ').grid(row=0, column=0)
DisplayAlphaLabel2 = Label(Axb, textvariable=AlphaCount).grid(row=0, column=1)
DisplayBetaLabel1 = Label(Axb, text = 'Beta Count: ').grid(row=1, column=0)
DisplayBetaLabel2 = Label(Axb, textvariable=BetaCount).grid(row=1, column=1)
ButtonAlpha = Button(text = 'Alpha Increase', command = onClickAlpha).grid(row=2, column=0, columnspan=2)
ButtonBeta = Button(text = 'Beta Increase', command = onClickBeta, state = 'disabled').grid(row=3, column=0, columnspan=2)

if AlphaCount >= BetaThreshold:
ButtonBeta.configure(state = 'normal')
else:
ButtonBeta.configure(state = 'disabled')

Axb.mainloop()


Because the final program will have conditions where AlphaCount will decrease, ButtonBeta needs to return to a disabled state if AlphaCount returns to under 5.


However, some problems have arisen...


When run normally, the code produces this error:



ButtonBeta.configure(state 'disabled')
AttributeError: 'NoneType' object has no attribute 'configure'


And when ButtonBeta.Configure is commented out, ButtonBeta does not detect when AlphaCount is 5 or higher. I'm sure PY_VAR0 being printed when checking AlphaCount has something to do with it...but I need to display the value in the window too.


How can I correct this?


I'm using Python 3.2.


Aucun commentaire:

Enregistrer un commentaire