1
from Tkinter import *

root = Tk()

armor = Label(root, text="Armor:", font=("Helvetica", 12))
armor.grid(row=1, column=0)
armorscale = Scale(root, from_=1337, to=20000, orient=HORIZONTAL, length=500)
armorscale.grid(row=1, column=1)
###
damage = Label(root, text="Base Damage:", font=("Helvetica", 12), justify=LEFT)
damage.grid(row=2, column=0)
damagescale = Scale(root, from_=100, to=2000, orient=HORIZONTAL, length=500)
damagescale.grid(row=2, column=1)
###    
armorfloat = float(armorscale.get())
damagefloat = float(damagescale.get())
fReduction = float(armorfloat / (armorfloat + 12 * damagefloat))
sReduction = str(fReduction)
fTaken = damagefloat * (1 - (1* fReduction))
sTaken = str(fTaken)
###
def calc1():
    armorfloat = float(armorscale.get())
    damagefloat = float(damagescale.get())
    fReduction = float(armorfloat / (armorfloat + 12 * damagefloat))
    sReduction = str(fReduction)
    fTaken = damagefloat * (1 - (1 * fReduction))
    sTaken = str(fTaken)
    print sReduction
    print sTaken
    return sReduction
    return sTaken

###
reduction = Label(root, text="Reduction %:" + sReduction, font=("Helvetica", 12), justify=LEFT)
reduction.grid(row=3, column=0)
taken = Label(root, text="Damage Taken:" + sTaken, font=("Helvetica", 12), justify=LEFT)
taken.grid(row=4, column=0)
button = Button(root, text="Calculate", command=calc1)
button.grid(row=3, column=1, pady=5, sticky=E)
###
root.mainloop()

这是我第一次尝试编程任何东西,所以我完全是个菜鸟。一切似乎都运行良好,打印的东西只是为了证明这一点。问题是在打开程序并移动滑块或单击计算按钮后,GUI 上的值根本没有更新。

4

2 回答 2

3

您不必使用StringVar 来更改标签的文本(当然可以,但这是 Tkinter 程序中非常罕见的模式)。text可以使用config方法或使用 key更改小部件的选项"text",这更容易:

label.config(text="new text")
# or
label["text"] = "new text"

因此,您的标签文本可以更新,而无需为每个标签使用 StringVar:

def calc1():
    armorfloat = float(armorscale.get())
    damagefloat = float(damagescale.get())
    fReduction = float(armorfloat / (armorfloat + 12 * damagefloat))
    fTaken = damagefloat * (1 - (1 * fReduction))
    reduction.config(text="Reduction %:{}".format(fReduction))
    taken.config(text="Damage Taken:{}".format(fTaken))

如果您还想在移动滑块时重新计算标签的值,请在command“缩放”小部件的选项中使用此功能:

Scale(..., command=lambda v: calc1())
于 2013-04-11T12:04:45.487 回答
0

正如 msw 所写:您可以使用 StringVar

但是要更新回调中的标签,您必须为其提供状态

一种常见的模式,是将状态封装在一个类中,就像这个答案: How to update image in tkinter label?

另一种选择是将其作为参数显式传递(这里,为方便起见,使用 functools.partial )

(另一种选择是直接修改全局模块状态,但我认为这不是一个好习惯:最好是明确的)

此外,删除了无用的浮点转换,更改了命名样式以更符合 pep8,并删除了 * 导入(尽管现在显式访问 Tkinter 属性应该更好,因为我们要导入其中的很多属性)

from Tkinter import Tk, Label, Scale, HORIZONTAL, LEFT, StringVar, Button, E
from functools import partial

root = Tk()

armorlabel = Label(root, text="Armor:", font=("Helvetica", 12))
armorlabel.grid(row=1, column=0)
armorscale = Scale(root, from_=1337, to=20000, orient=HORIZONTAL, length=500)
armorscale.grid(row=1, column=1)
###
damagelabel = Label(root, text="Base Damage:", font=("Helvetica", 12), justify=LEFT)
damagelabel.grid(row=2, column=0)
damagescale = Scale(root, from_=100, to=2000, orient=HORIZONTAL, length=500)
damagescale.grid(row=2, column=1)
###    
damage_reduction = StringVar()
damage_taken = StringVar()


def calc1(reduction, taken):
    armor = armorscale.get()
    damage = damagescale.get()
    reduction_value = armor / (armor + 12.0 * damage)
    reduction.set("Reduction %%: %s" % reduction_value)
    taken_value = damage * (1 - reduction_value)
    taken.set("Damage Taken: %s" % taken_value)

###
reduction_label = Label(root, textvariable=damage_reduction, font=("Helvetica", 12), justify=LEFT)
reduction_label.grid(row=3, column=0)
taken_label = Label(root, textvariable=damage_taken, font=("Helvetica", 12), justify=LEFT)
taken_label.grid(row=4, column=0)
calc = partial(calc1, damage_reduction, damage_taken)
button = Button(root, text="Calculate", command=calc)
button.grid(row=3, column=1, pady=5, sticky=E)
calc()
###
root.mainloop()
于 2013-04-11T11:46:16.787 回答