Tkinter Canvas文本更新:按下按钮时文本不会更新

时间:2019-02-19 23:42:32

标签: python-3.x tkinter tkinter-canvas

创建骰子滚动程序,以在D&D会话中获得乐趣/帮助。尝试创建一个使用输入变量更新画布文本的函数。 (输入变量将是掷骰子)。但是由于某些原因,我无法获取用于更新Canvas文本的按钮。我已经测试了骰子掷骰子函数的输出,它们似乎按建议的方式工作(每个末尾的display()函数除外)。

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()

1 个答案:

答案 0 :(得分:0)

在Google的帮助下,我自己设法弄清楚了,然后弄清楚了发生了什么。

在我的原始代码中,当我创建一个按钮并设置command = diceroll()时。我不知道命令功能在创建按钮后立即执行。

为了将command =函数设置为等到按下按钮才能执行,您需要在其前面添加“ lambda:”标记。

advantage=Button(master, text="Advantage Roll", command = lambda: advan(int(ad.get()),int(addbns.get())))
相关问题