[Tutor] Python Dice Game

Alan Gauld alan.gauld at btinternet.com
Sat Apr 11 14:57:00 CEST 2009


"jake_k2011" <jk_kriegel at yahoo.com> wrote

> get it to work :\ I'm a total noob and just a BEGINNER. I can't seem to 
> get
> the buttons to work.Basically they are constructed and maybe activated in
> the main program, andI  need to be able to use them in the functions 
> roll()
> and turn() i believe.
>
> CAn anyone help to get this to work??

I'm not familiar with the Graphics module or how it works,
but in general when constructing widgets you need to either
a) pass in some kind of callback function, or
b) use a bind function to connect the button with the function or
c) use a naming convention to associate the button and callback.

I don't see anything in your code that connects the button to
the function you want to be called. So I suspect you need to
look at the GUI callback mechanism in more detail?

>    rollButton = Button(win, Point(150,30), 150, 10, "Roll Dice")
>    rollButton.activate()
>    passButton = Button(win, Point(150,10), 150, 10, "Pass Turn")
>    passButton.activate()

> def turn(passButton):
>    pt = win.getMouse()
>    score = 0
>    rollcount = 0
>    snakeeyes = 0
>    while snakeeyes == 0 and rollcount < 3 and not passButton.clicked(pt):
>        tempscore,snakeeyes = scoreroll()
>        score = score+tempscore
>        rollcount = rollcount+1
>    return score
>
>
> def roll():
>    pt = win.getMouse()
>    while not quitButton.clicked(pt):
>        if rollButton.clicked(pt):
>            value1 = randrange(1,7)
>            die1.setValue(value1)
>            value2 = randrange(1,7)
>            die2.setValue(value2)
>            value3= randrange(1,7)
>            die3.setValue(value3)
>            value4 = randrange(1,7)
>            die4.setValue(value4)
>            value5 = randrange(1,7)
>            die5.setValue(value5)
>    rlist = [value1,value2,value3,value4,value5]
>    return rlist

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list