嗨,我一直在寻找答案,但我找不到。基本上我在 Tkinter 做一个货币转换器。我选择了类似于计算器的显示器,但我需要能够使用每个按钮并最终得到一个数字。这是我的代码(还没有完成!):
from Tkinter import *
root = Tk()
root.title("Currency Converter")
Currency = []
currency = {"Pound": 1.0,
"Euro": 1.10,
"Dollar":1.10}
for i in currency:
Currency.append(i)
button_list = [
'1', '2', '3',
'4', '5', '6',
'7', '8', '9',
'0', '.']
r = 1
c = 0
for t in button_list:
cmd = lambda x=t: display.insert(INSERT, x)
Button(root,text=t,width=3, command=cmd).grid(row=r,column=c, padx=3, pady=3)
c += 1
if c > 2:
c = 0
r += 1
variable = StringVar(root)
variable.set(Currency[0])
variable2 = StringVar(root)
variable2.set(Currency[0])
startCurrency = apply(OptionMenu, (root, variable) + tuple(Currency))
startCurrency.grid(row=0, column=3, columnspan=2)
label = Label(root, text="To")
label.grid(row=1, column=3)
newCurrency = apply(OptionMenu, (root, variable2) + tuple(Currency))
newCurrency.grid(row=2, column=3, columnspan=2)
go = Button(root, text="Go")
go.grid(row=4, column=2)
display = Text(root, width=15, height=1)
display.grid(row=0,column=0, columnspan=3)
root.mainloop()