views:

78

answers:

1

Thank you to everybody who helped answer my last question:

My friend took the code, and has attempted to use Tkinter to make a box that we could use to make things nicer-looking, but he has been unable to integrate the dice roller from the last question and the Tkinter. Any help or ideas in getting the dice roller into the code below would be wonderful!

from Tkinter import *

def callme():
    label3 = Label(root, text = 'Haha! I lied!')
    label3.pack(padx = 10, pady = 10)

root = Tk()

label = Label(root, text = 'How many dice do you want to roll?')
label.pack(padx = 10, pady = 10)

entry = Entry(root,bg = 'white').pack(padx = 10, pady = 10)

label2 = Label(root, text = 'How many dice do you want to roll?')
label2.pack(padx = 10, pady = 10)

entry = Entry(root,bg = 'white').pack(padx = 10, pady = 10)

frame = Frame(root, bg = 'yellow')
button = Button(frame, command = callme, text = 'Roll!',width = 5, height = 2)

frame.pack()
button.pack(padx=10,pady=10)

root.mainloop()
+2  A: 

Because half the fun is trying to work it out yourself I'll give you a few hints instead of a complete program.

You should store the variables for your input entries, so you can use them later to get the values out again, but don't do this:

entry = Entry(root,bg = 'white').pack(padx = 10, pady = 10)

That doesn't quite do what you expect, because you're also calling pack() it won't return the entry widget, you'll end up with NoneType instead beacuse pack() returns nothing. Use the following:

entry = Entry(root,bg = 'white')
entry.pack(padx = 10, pady = 10)

Also, use two separate entry variables, because you want one for your number of rolls, and one for the number sides, for example:

entry_sides = Entry(root,bg = 'white')
entry_sides.pack(padx = 10, pady = 10)

In your callme() function, you can then get those values out again, to use them to roll the dice:

number_of_sides = int(entry_sides.get())

And finally, instead of creating label3 inside callme, create it as part of the frame, as you did with the other labels, and then update its value after you calculate the value of the dice roll:

def callme():
    # get dice total here using variables from entry
    label3.config(text = str(dice_total))
Andre Miller