你如何替换Tkinter python中的标签?

时间:2013-12-08 12:39:07

标签: python tkinter converter currency labels

我是一名新手程序员,我正在制作货币转换器....它仍在进行中,但任何人都可以帮我尝试更换'def convert()'中的标签......要清楚,现在我每次转换时,都会弹出一个新标签,但我想要发生的是每次点击转换时我的标签都会被替换......

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 个答案:

答案 0 :(得分: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)

在这两种情况下,您都需要确保要更改的对象(窗口小部件或StringVar)是全局变量或实例变量,以便稍后可以在代码中访问它。