1

Creating a dice rolling program for fun/help out with D&D sessions. Trying to create a function that updates canvas text with the input variable. (Input variable will be whatever the dice roll is). But for some reason I can't get the buttons to update the Canvas text. I've tested the outputs from the dice roll functions and they seem to work as suggested (except for the display() function at the end of each).

from tkinter import ttk
from tkinter import messagebox
from tkinter import *
from tkinter.ttk import *
import random
import numpy as np
master = Tk()
master.title('DiceRoll.Exe')
def display(a):
    msgbox.itemconfigure(box,text=a)
    msgbox.update()
def dmg(x,y,bdmg):
    i=0
    a=np.zeros(int(x))
    for i in range(int(x)):
        a[i]= random.randint(1,int(y))
    b= int((np.sum(a) + int(bdmg)))
    return b
def advan(ad,addbns):
    q=np.zeros(ad)
    for i in range(ad):
        a= random.randint(1,20)
        b= random.randint(1,20)
        if a > b:
            q[i] = (a + addbns)
        else:
            q[i] = (b + addbns)
    display(q)
def disadvan(disadd,disbns):
    q=np.zeros(disadd)
    for i in range(disadd):
        a= random.randint(1,20)
        b= random.randint(1,20)
        if a < b:
            q[i]=(a + disbns)
        else:
            q[i]=(b + disbns)
    display(q)
def dmgroll(z,x,y,bdmg):
    a=np.zeros(int(z))
    i=0
    for i in range( int(z) ):
        a[i]= dmg(x,y,bdmg)
    display(a)


msgbox=Canvas(master, width= 200, height=50, background="white")
msgbox.grid(row=1, column=5, rowspan=2, columnspan=2)
box=msgbox.create_text(30,25, text="Welcome")



#First Row
z=Spinbox(master, from_=1, to=20, justify= CENTER)
z.grid(row=0, column=0)# Z = number of units making rolls
x=Spinbox(master,from_=1, to=20, justify= CENTER)
x.grid(row=0, column=1) # 'X' is the first number in the XdY diceroll formula 
Label(master, text='d').grid(row=0,column=2)
y=Spinbox(master,from_=1, to=20, justify= CENTER)
y.grid(row=0, column=3) #'Y' is the 2nd number in the XdY diceroll formula
Label(master, text='+').grid(row=0,column=4)
bdmg=Spinbox(master, from_=0, to=20, justify= CENTER)
bdmg.grid(row=0, column=5) #bdmg= bonus damage    XdY + bdmg
roll=Button(master, text="Roll Dice", command= dmgroll(z.get(),x.get(),y.get(),bdmg.get())).grid(row=0, column=6)

#2nd Row
ad=Spinbox(master, from_=1, to=20, justify=CENTER)
ad.grid(row=1, column=0)
Label(master, text="+").grid(row=1, column=2)
addbns=Spinbox(master, from_=0, to=20, justify=CENTER)
addbns.grid(row=1, column=3)
advantage=Button(master, text="Advantage Roll", command = advan(int(ad.get()),int(addbns.get()))).grid(row=1,column=1)


#3rd Row
disadd=Spinbox(master, from_=1, to=20, justify=CENTER)
disadd.grid(row=2, column=0)
Label(master, text="+").grid(row=2, column=2)
disbns=Spinbox(master, from_=0, to=20, justify=CENTER)
disbns.grid(row=2, column=3)
disadvantage=Button(master, text="Disadvantage Roll", command= disadvan(int(disadd.get()),int(disbns.get()))).grid(row=2, column=1)

msgbox.itemconfigure(box, text="Welcome")


master.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Cloudekill
  • 31
  • 3
  • After some troubleshooting, I find that the Display() functions at the end of each dice roll function works properly if I change the msgbox.config() to a simple print() and run the functions outside of the Tkinter GUI. So it seems to be something wrong with the way I'm calling up the functions while inside the GUI, or something wrong with how I'm trying to update the Canvas text with msgbox.config(). – Cloudekill Feb 20 '19 at 20:42

1 Answers1

0

Well I managed to figure it out on my own, with assistance of Google, and figured out what was going on.

In my original code, when I created a button and set the command=diceroll(). I did not know that the command function executes as soon as the button is created.

In order to set the command= function to wait until button press for execution, you need to add the 'lambda:' designation in front of it.

advantage=Button(master, text="Advantage Roll", command = lambda: advan(int(ad.get()),int(addbns.get())))
Cloudekill
  • 31
  • 3