2013-12-08 97 views
0

我是一個新手程序員,我正在製作一個貨幣轉換器....它仍在進行中,但任何人都可以幫助我嘗試替換'def convert()'中製作的標籤...要清楚,現在每次我轉換,一個新的標籤底下彈出,但我希望發生的是,我的標籤被替換每次我點擊轉換...如何替換Tkinter python中的標籤?

import sys 
from Tkinter import * 

root = Tk() 
root.title("CURRENCY CONVERTER") 
root.geometry('600x300+30+100') 
root.config(bg="#000000") 

#*************************************GBP*************************************# 
def rate(): 
    rate = 1 
    if var.get() =='GBP' and var2.get() =='USD': 
     rate=float(1.63452) 
    if var.get() =='GBP' and var2.get() =='EUR': 
     rate=float(1.19529) 
    if var.get() =='GBP' and var2.get() =='INR': 
     rate=float(99.9639) 
    if var.get() =='GBP' and var2.get() =='AUD': 
     rate=float(1.79578) 
    if var.get() =='GBP' and var2.get() =='CAD': 
     rate=float(16.8796) 
    if var.get() =='GBP' and var2.get() =='NZD': 
     rate=float(1.97334) 
    if var.get() =='GBP' and var2.get() =='JPY': 
     rate=float(168.143) 
    if var.get() =='GBP' and var2.get() =='CNY': 
     rate=float(9.93698) 
#*************************************USD*************************************# 
    if var.get() =='USD' and var2.get() =='GBP': 
     rate=float() 
##to do ....ADD MORE 

    return rate 
#----------------------------------HELP------------------------------# 
def convert(): 
    converted=Label(root, text=(var.get(), int(entarr.get()),">>>", round((float(entarr.get())*rate()),3), var2.get())) 
    converted.config(font=('century gothic',(15)),bg='#000000',fg="white",width=0, relief=FLAT) 
    converted.pack(expand = 1,anchor="center") 
    return 
#--------------------------------HELP--------------------------------# 


#title 
Title=Label(root, text="Currency Converter", cursor="heart") 
Title.config(font=('century gothic',(35)),bg='#fff60b', fg="#9c0200",width=0,relief=RAISED) 
Title.pack(expand=1, anchor=CENTER) 

#entry box 
entarr = DoubleVar() 
entarr.set(0) 
entry = Entry(root, textvariable=entarr, cursor="plus") 
entry.config(font=('century gothic',(15)),bg='#ff6100',width=0, relief=SOLID) 
entry.pack(expand = 1, anchor="center") 

#currency 1 
var = StringVar(root) 
var.set('Choose a currency to convert from') 
choices = ['GBP', 'USD', 'EUR','INR','AUD','CAD','NZD','JPY','CNY'] 
option = OptionMenu(root, var, *choices) 
option.config(font=('century gothic',(15)),bg='#fff60b',fg="#9c0200",activebackground='#00ff80',width=0, cursor="", relief=FLAT) 
option.pack(ipadx=10,ipady=0, expand=1,anchor="center") 

#convert button 
Arrow= Button(root, text=">>>>CONVERT>>>>", command = convert, cursor="exchange") 
Arrow.config(font=('century gothic',(15)),width=0, bg="#ff6100", relief=SOLID) 
Arrow.pack(ipadx=1,ipady=0, expand =1, anchor="center") 

#currency 2 
var2 = StringVar(root) 
var2.set('Choose a currency to convert to') 
choices2 = ['GBP', 'USD', 'EUR','INR','AUD','CAD','NZD','JPY','CNY'] 
option2 = OptionMenu(root, var2, *choices2) 
option2.config(font=('century gothic',(15)),bg='#fff60b',fg="#9c0200",activebackground='#00ff80',width=0, relief=FLAT) 
option2.pack(ipadx=10,ipady=0, expand=1,anchor="center") 

root.mainloop() 

編輯:

這麼糊塗請幫我。我不知道,這裏超級菜鳥!

def convert(): 
     newValue=(var.get(), int(entarr.get()),">>>", round((float(entarr.get())*rate()),3), var2.get()) 
     converted=Label(root, textvariable=newValue) 
     converted.config(font=('century gothic',(15)),bg='#000000',fg="white",width=0, relief=FLAT) 
     converted.config(text=newValue) 
     converted.pack(expand = 1,anchor="center") 
     return 

回答

1

有幾個簡單的方法可以實現這一點。在這兩種情況下,都需要創建一個標籤,一次,然後動態更改顯示的文本。

方法1:使用textvariable

如果您關聯的標籤,只要你改變STRINGVAR的價值StringVar,標籤將自動更新:

labelVar = StringVar() 
label = Label(..., textvariable=labelVar) 
... 
# label is automatically updated by this statement: 
labelVar.set(newValue) 

方法2:用configure方法更新文字:

label = Label(...) 
... 
# update the label with the configure method: 
label.configure(text=newValue) 

在這兩種情況下,您都需要確保您要更改的對象(widget或StringVar)是全局變量或實例變量,以便您稍後可以在代碼中訪問它。

+0

嗨,非常感謝你回答......但正如我所說,我是一個超級新手,你能編輯我的代碼,告訴我你的意思嗎?再次感謝 –