I am a newbie programmer and I am making a currency converter....It is still in progress, but could anyone help me to try to replace the label made in 'def convert()'...To be clear, right now everytime I convert, a new label pops up underneath, but what I want to happen is that my label gets replaced everytime I click 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()
EDIT:
SO CONFUSED PLEASE HELP ME. I have no idea, super noob here!
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
解决方案
There are a couple of simple ways to accomplish this. In both cases, it involves creating a label once, and then dynamically changing the text that is displayed.
Method 1: use a textvariable
If you associate a StringVar with a label, whenever you change the value of the StringVar, the label will be automatically updated:
labelVar = StringVar()
label = Label(..., textvariable=labelVar)
...
# label is automatically updated by this statement:
labelVar.set(newValue)
Method 2: update the text with the configure method:
label = Label(...)
...
# update the label with the configure method:
label.configure(text=newValue)
In both cases you need to make sure the object that you're changing (either the widget or the StringVar) is either a global variable or an instance variable so that you can access it later in your code.