2013-04-11 63 views
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() 

这是我第一次尝试编程任何东西,所以我是一个总noob。一切似乎都很好,印刷的东西只是为了证明它。问题在于,打开程序并移动滑块或单击计算按钮后,GUI上的值完全没有更新。图形用户界面不文本

+0

相关,如果不是完整答案:[事件和绑定](http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm) – Bakuriu 2013-04-11 11:26:27

+0

您应该修复缩进......尤其是,有2个返回语句没有意义,没有第二个块 – berdario 2013-04-11 11:28:15

+0

我为你修复了它 – berdario 2013-04-11 12:21:54

回答

0

由于垃圾写道:您可以使用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() 
3

你不使用STRINGVAR更改标签的文本(中当然你可以,但这是在Tkinter程序中非常罕见的模式)。

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

所以你的标签的文字,而不需要使用STRINGVAR每个标签进行更新:小部件的text选项可以与config方法或使用该密钥"text",这是很容易被改变:

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