[英]How do you replace a label in Tkinter python?
我是一個新手程序員,正在制作貨幣轉換器。...它仍在進行中,但是誰能幫助我替換在'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:使用文本變量
如果將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)是全局變量還是實例變量,以便以后可以在代碼中訪問它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.