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