我是python的新手,正尝试制作一个简单的转换器应用程序,但我的问题是我不知道如何解决km至m的问题。如果我找出来,我可以找出其余的。提前致谢!这是我的代码
import tkinter from tkinter import ttk
window=tkinter.Tk()
window.title("Conversion Unit")
labelOne=ttk.Label(window, text='Enter Value')
labelOne.grid(row=0, column=0)
to_be_converted=ttk.Combobox(
values=('mm', 'cm', 'inches', 'feet', 'yards', 'meter', 'km', 'miles'),
width=10
).grid(row=0, column=2)
labelTwo=ttk.Label(window, text="Equivalent to")
labelTwo.grid(row=1, column=1)
converted=ttk.Combobox(
values=('mm', 'cm', 'inches', 'feet', 'yards', 'meter', 'km', 'miles'),
width=10
).grid(row=1, column=2)
userName=tkinter.DoubleVar()
userEntry=ttk.Entry(window, width=5, textvariable=userName)
userEntry.grid(row=0, column=1)
def convert():
if to_be_converted.get=='km' and converted.get=='m':
labelTwo.configure(text='Value is equivalent to:' + userName.get() * 1000)
btn=ttk.Button(window, text='Convert!', command=convert)
btn.grid(row=0, column=4)
window.mainloop()
答案 0 :(得分:1)
这是一个工作版本,有一些更改:
import tkinter
from tkinter import ttk
UNITS = ('mm', 'cm', 'inches','feet', 'yards', 'meter', 'km', 'miles')
window = tkinter.Tk()
window.title("Conversion Unit")
labelOne = ttk.Label(window,text='Enter Value')
labelOne.grid(row=0,column=0)
to_be_converted = ttk.Combobox(values=UNITS, width=10)
to_be_converted.grid(row=0, column=2)
labelTwo = ttk.Label(window, text="Equivalent to")
labelTwo.grid(row=1,column=1)
converted = ttk.Combobox(values=UNITS, width=10)
converted.grid(row=1, column=2)
userName = tkinter.DoubleVar()
userEntry = ttk.Entry(window, width=5, textvariable = userName)
userEntry.grid(row=0, column=1)
def convert():
if to_be_converted.get() == 'km' and converted.get() == 'meter':
labelTwo.configure(text='Value is equivalent to:' + str(userName.get()*1000))
btn = ttk.Button(window, text='Convert!', command=convert)
btn.grid(row=0, column=4)
window.mainloop()
.grid()返回无
所以这行:
to_be_converted = ttk.Combobox(values=UNITS, width=10).grid(row=0, column=2 )
必须是:
to_be_converted = ttk.Combobox(values=UNITS, width=10)
to_be_converted.grid(row=0, column=2)
(与converted
相同)
.get
是绑定方法,而不是属性
所以这行:
if to_be_converted.get == 'km' and converted.get == 'meter':
必须是:
if to_be_converted.get() == 'km' and converted.get() == 'meter':
(并且'm'必须替换为'meter'-或UNITS
必须具有'm'而不是'meter')。
无法将字符串与浮点数连接
所以这行:
labelTwo.configure(text='Value is equivalent to:' + userName.get()*1000)
必须是,例如:
labelTwo.configure(text='Value is equivalent to:' + str(userName.get()*1000))