如何使条目小部件中的条目相互交互

时间:2014-11-18 22:26:04

标签: python tkinter widget

我试图创建一个允许用户输入货币和美元金额的程序,并让程序运行转换,当然也会向用户显示结果。我使用tkinter并弹出一个用户可以输入值的gui,截至目前它只是打印用户在IDLE窗口输入的内容(以测试我所拥有的是否会做任何事情)。

from tkinter import *
from math import *
fields = 'Dollars' , 'Currency'
def fetch(entries):
    for entry in entries:
        field = entry[0]
        text = entry[1].get()
        print('%s: "%s"' % (field, text))


def makeform(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)
        lab = Label(row, width = 15, text = field, anchor = 'w')
        ent = Entry(row)
        row.pack(side = TOP, fill = X, padx=5, pady = 5)
        lab.pack(side = LEFT)
        ent.pack(side = RIGHT, expand = YES, fill = X)
        entries.append((field, ent))
    return entries

if __name__ == '__main__':
    root = Tk()
    ents = makeform(root, fields)
    root.bind('<Return>', (lambda event, e = ents: fetch(e)))
    b1 = Button(root, text = 'Show', command = (lambda e=ents: fetch(e)))
    b1.pack(side = LEFT, padx = 5, pady =5)
    b2 = Button(root, text = 'Quit', command=root.quit)
    b2.pack(side = LEFT, padx = 5, pady = 5)
    root.mainloop()

我用于转换货币的唯一代码是这样的:

def convert():
    option = input("Please enter what you would like to convert $ to; Yen, Euros, or Pesos: ")
    dollars = eval(input("Please enter the dollar amount you wish to convert: "))
    if dollars < 0:
        print("You must enter a value greater than 0.")
        convert()
    elif option == "Yen":
        print("The amount of Yen you have is: ", dollars * 106.84)
    elif option == "Euros":
        print("The amount of Euros you have is: ", dollars * 0.77)
    elif option == "Pesos":
        print("The amount of Pesos you have is: ", dollars * 13.38)

我查看了很多网站,但我没有找到任何关于如何将两者合并在一起的有用信息。如果有人能帮我理解我需要做什么,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

修改问题:如何根据另一个条目的值更新条目的值? 这似乎需要代码如下:

value=eval(entryDollar.get())
entryCurrency.delete(0,END)
entryCurrency.insert(0,repr(value*exchangeRate))

在您与Show的当前绑定中,<Return>目前尚不清楚要转换的方向;方向指示器可能有所帮助。