python 3 tkinter类变量和标签不更新的Spinbox

时间:2016-05-12 03:43:48

标签: python python-3.x tkinter widget spinner

无法弄清楚为什么我的标签没有根据我的Spinbox上的点击进行更新

from tkinter import *

mainBackground = "#0047BF"
mainForeground = "#C2F2E6"
inputBackground = "#230E38"

window = Tk()
window.title("Gurps Game Manager")
window.geometry("700x600")
window.wm_iconbitmap('pyramid.ico')
window.configure(background = mainBackground)

# Creating Variable Classes and setting to base stat
strength = IntVar(window)
strength.set(10)
dexterity = IntVar(window)
dexterity.set(10)
intelligence = IntVar(window)
intelligence.set(10)
health = IntVar(window)
health.set(10)
hpMod = IntVar(window)
hpMod.set(0)
willMod = IntVar(window)
willMod.set(0)
perMod = IntVar(window)
perMod.set(0)
fpMod = IntVar(window)
fpMod.set(0)
bSpeedMod = IntVar(window)
bSpeedMod.set(0)
bMoveMod = IntVar(window)
bMoveMod.set(0)
hpTotal = IntVar(window)
hpTotal.set(10)
willTotal = IntVar(window)
willTotal.set(10)
perTotal = IntVar(window)
perTotal.set(10)
fpTotal = IntVar(window)
fpTotal.set(10)
bSpeedTotal = IntVar(window)
bSpeedTotal.set(5)
bMoveTotal = IntVar(window)
bMoveTotal.set(5)
bLift = IntVar(window)
bLift.set(20)
bThrust = StringVar(window)
bThrust.set('1d-2')
bSwing = StringVar(window)
bSwing.set('1d')
charPointTotal = IntVar(window)
charPointTotal.set(0)
charPointLimit = IntVar(window)
charPointLimit.set(250)
charPointDiff = IntVar(window)
charPointDiff.set(250)


# Functions to create widgets
def mainLabel(text):
    label = Label(window, text = text, bg = mainBackground, fg = mainForeground,
        font = ('Lucida Console', 14))
    return label


def calcLabel(textVariable):
    label = Label(window, textvariable = textVariable, bg = mainBackground, fg = mainForeground, font = ('Lucida Console', 14))
    return label


def mainEntry(callback = None):
    entry = Entry(window, bg = inputBackground, fg = mainForeground, insertbackground = mainForeground,
        command = callback)
    return entry


def mainSpinbox(callback, textVar, floor, ceiling, increment = 1):
    spinbox = Spinbox(window, fg = mainForeground, state = 'readonly', readonlybackground = mainBackground,
        from_ = floor, to = ceiling, increment = increment, textvariable = textVar, command = callback,
        justify = 'center', border = '0', width = '5')
    return spinbox


# Command function to set values on change
def charPointsCallback():
    st = int(strength.get())
    dx = int(dexterity.get())
    iq = int(intelligence.get())
    ht = int(health.get())
    hp = int(hpMod.get())
    will = int(willMod.get())
    per = int(perMod.get())
    fp = int(fpMod.get())
    bSpeed = int(bSpeedMod.get())
    bMove = int(bMoveMod.get())
    hpTotal.set(st + hp)
    willTotal.set(iq + will)
    perTotal.set(iq + per)
    fpTotal.set(ht + fp)
    bSpeedTotal.set((ht + dx) / 4 + bSpeed)
    bMoveTotal.set((ht + dx) // 4 + bMove)
    bLift.set((st * st) / 5)
    cpTotal = ((st - 10) * 10) + ((dx - 10) * 20) + ((iq - 10) * 20) + ((ht - 10) * 10) + (hp * 2) + (will * 5) + (
        per * 5) + (fp * 5) + (bSpeed * 20) + (bMove * 5)
    cpLimit = charPointLimit.get()
    charPointTotal.set(cpTotal)
    charPointDiff.set(cpLimit - cpTotal)


# Lists of strings and variables for loop to create widgets
coreStatLabels = ['ST', 'DX', 'IQ', 'HT']
coreStatVar = [strength, dexterity, intelligence, health]
modStatLabels = ['HP', 'Will', 'Per', 'FP', 'Basic Speed', 'Basic Move']
modStatVar = [hpMod, willMod, perMod, fpMod, bSpeedMod, bMoveMod]
totalStatVar = [hpTotal, willTotal, perTotal, fpTotal, bSpeedTotal, bMoveTotal]
derivedStatLabels = ['Basic Lift', 'Basic Thrust', 'Basic Swing', 'Character Point Total:']
derivedStatVar = [bLift, bThrust, bSwing, charPointTotal]


# Widgets
characterNameLbl = mainLabel('Character Name:')
characterNameLbl.grid(row = 0, column = 0, columnspan = 2)
characterNameEntry = mainEntry(charPointsCallback())
characterNameEntry.grid(row = 0, column = 2)
charPointLimitLbl = mainLabel('Character Point Limit:')
charPointLimitLbl.grid(row = 1, column = 0, columnspan = 2)
charPointLimitSpin = mainSpinbox(charPointsCallback(),charPointLimit, 100, 500, 10)
charPointLimitSpin.grid(row = 1, column = 2)

# Loops that create the widgets
for i in range(4):
    row = range(2, 6)
    coreStatLbl = mainLabel(coreStatLabels[i])
    coreStatLbl.grid(row = row[i], column = 0)
    coreStatSpin = mainSpinbox(charPointsCallback(), coreStatVar[i], 5, 15)
    coreStatSpin.grid(row = row[i], column = 2)

for i in range(5):
    row = range(6, 12)
    modStatLbl = mainLabel(modStatLabels[i])
    modStatLbl.grid(row = row[i], column = 0)
    modStatSpin = mainSpinbox(charPointsCallback(), modStatVar[i], -5, 5)
    modStatSpin.grid(row = row[i], column = 1)
    totalStatLabel = calcLabel(totalStatVar[i])
    totalStatLabel.grid(row = row[i], column = 2)

for i in range(4):
    row = range(12,16)
    derivedStatLbl = mainLabel(derivedStatLabels[i])
    derivedStatLbl.grid(row = row[i], column = 0)
    derivedStatCalc = calcLabel(derivedStatVar[i])
    derivedStatCalc.grid(row = row[i], column = 2)

window.mainloop()

我对变量类的理解是,任何设置了变量类的窗口小部件都会在变量发生变化时更新。我点击向上/向下微调器,没有任何变化。

1 个答案:

答案 0 :(得分:1)

执行此操作时:

modStatSpin = mainSpinbox(charPointsCallback(), ...)

这与你这样做完全相同:

something = charPointsCallback()
modStatSpin = mainSpinbox(something, ...)

如果要将回调函数传递给函数,则必须将引用传递给函数(即:删除尾随()