0

Hey guys, I'm in an introductory computer science class that deals with Python. We're assigned to create a dice-rolling program using Tkinter, and I have the interface pretty much down, but I'm having trouble linking it to the dice-roller. Here it is.

import random
import Tkinter

win = Tkinter.Tk()
win.title("Die Roller")
class die():
    import Tkinter
    def __init__(self,ivalue,parent):
        self.value = ivalue
        self.display = Tkinter.Label(parent,relief='ridge', borderwidth=2, text=str(self.value))
    def roll(self):
        self.value = random.randint(1,6)
        self.display.config(text=str(self.value))
def rollin():
    d1.roll
    d2.roll
    d3.roll
row1 = Tkinter.Frame(win)
row2 = Tkinter.Frame(win)
d1 = die(1,row1)
d2 = die(1,row1)
d3 = die(1,row1)
d1.display.pack(side="left")
d2.display.pack(side="left")
d3.display.pack(side="left")
row1.pack()
rolldice = Tkinter.Button(row2, command=rollin(), text = "Roll")
rolldice.pack()
row2.pack()
win.mainloop()

When I click roll, nothing pops up as an output. Not quite sure what I'm doing wrong here. (Bear in mind Tkinter was just introduced to us a few days ago.)

4
Contributors
4
Replies
8
Views
5 Years
Discussion Span
Last Post by pyTony
2

What that post means is, you execute the return from rollin (which is 'None') because of the parens. It should be
rolldice = Tkinter.Button(row2, command=rollin, text = "Roll")
See Click Here for info on bindings and callbacks.

Also, you do not include the parens when you call dx.roll within rollin() so it does nothing. Finally, I like to use Tkinter variables because the widget is updated when the variables change. Otherwise you may have to call update_idletasks().

import random
import Tkinter

win = Tkinter.Tk()
win.title("Die Roller")
class die():
    import Tkinter
    def __init__(self,ivalue,parent):
        win.geometry("75x50+10+10")
        self.label_var = Tkinter.IntVar()
        self.label_var.set(ivalue)
        self.display = Tkinter.Label(parent,relief='ridge', borderwidth=2, 
                       textvariable=self.label_var)
        self.display.pack(side='left')

    def roll(self):
        value = random.randint(1,6)
        self.label_var.set(value)
        print "label_var =", value

def rollin():
    d1.roll()
    d2.roll()
    d3.roll()


row1 = Tkinter.Frame(win)
row2 = Tkinter.Frame(win)
d1 = die(1,row1)
d2 = die(1,row1)
d3 = die(1,row1)
##d1.display.pack(side="left")
##d2.display.pack(side="left")
##d3.display.pack(side="left")
row1.pack()
rolldice = Tkinter.Button(row2, command=rollin, text = "Roll")
rolldice.pack()
row2.pack()
win.mainloop()

Edited by woooee

0

... as long as the functions value is not function reference True. Unlikely for starting programmer, though.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.