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上的值根本没有更新。
答案 0 :(得分:3)
你不必须使用StringVar来改变标签的文本(当然你可以,但它在Tkinter程序中是一种非常罕见的模式)。可以使用text
方法或使用密钥config
更改窗口小部件的"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))
如果您想在移动滑块时重新计算标签的值,请在Scale小部件的command
选项中使用此功能:
Scale(..., command=lambda v: calc1())
答案 1 :(得分:0)
正如msw所写:你可以使用StringVar
但要更新回调中的标签,您必须为其提供状态
一个常见的模式,就是将状态封装在一个类中,就像在这个答案中一样: How to update image in tkinter label?
另一种方法是将其明确地作为参数传递(此处,为方便起见,使用functools.partial)
(另一种选择,是直接修改全局模块状态,但我认为这不是一个好习惯:最好是明确的)
此外,删除了无用的浮点转换,更改了命名样式,使其更符合pep8,并删除了* import(尽管现在应该更好地显式访问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()