如何更正此功能以在Tkinter Python中创建标签?

时间:2013-12-07 15:17:34

标签: python function tkinter command converter

你可以帮我纠正这个,所以我得到一个标签出现了吗?我不认为2个函数是正确编写的,我是新函数并且需要一些帮助......因为“转换”函数是由'#convert按钮'命令的。

import sys
from Tkinter import *

root = Tk()
root.title("CURRENCY CONVERTER")
root.geometry('600x300+30+100')
root.config(bg="yellow", bd=5)

def rate():
        if var =='GBP' and var2 =='USD':
            rate=float(1.63452)
        if var == 'GBP' and var2=='EUR':
            rate=float(1.19529)
        else:
            rate=1
        return rate

def convert(rate):
        converted=Label(root, text=(var, entarr,">>>", (entarr*rate()), var2))
        converted.pack(expand=1)
        return convert

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

#entry box
entarr = IntVar()
entry = Entry(root, textvariable=entarr)
entry.config(font=('century gothic',(15)),bg='yellow',width=5, relief=SOLID)
entry.pack(padx=10,pady=10, expand = 1)

#currency 1
var = StringVar(root)
var.set('Choose a currency to convert from')
choices = ['GBP', 'USD', 'EUR']
option = OptionMenu(root, var, *choices)
option.config(font=('century gothic',(15)),bg='yellow',activebackground='cyan',width=0, relief=FLAT)
option.pack(ipadx=40,ipady=0, expand=1)

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

#currency 2
var2 = StringVar(root)
var2.set('Choose a currency to convert from')
choices2 = ['GBP','USD','EUR']
option2 = OptionMenu(root, var2, *choices2)
option2.config(font=('century gothic',(15)),bg='yellow',activebackground='cyan',width=0, relief=FLAT)
option2.pack(ipadx=40,ipady=0, expand=1)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

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)
    ## TODO:: add other convertion conditions
    return rate

def convert():
    converted=Label(root, text=(var.get(), entarr.get(),">>>", (entarr.get()*rate()), var2.get()))
    converted.pack(expand=1)
    return convert