我想将里程转换为公里数,但运行代码时遇到错误(Python2.7.13):
import sys
from Tkinter import *
def value():
q = float(num1.get())
d = float(q+1.6)
label = Label(root, text='the value entered in kms is'%d).pack(side=BOTTOM)
return
root = Tk()
root.title("kms converetr")
root.geometry('400x450+400+150')
frame = Frame(root)
frame.pack()
num1 = StringVar()
radbtn = StringVar()
radbtn.set(None)
frame1 = Frame(root)
frame1.pack(side=TOP)
label1 = Label(frame1, text='enter miles for conversion', fg='black',relief=RAISED)
label1.pack(side=TOP)
txtDisplay=Entry(frame1,textvariable=num1,bd=30,justify='center')
txtDisplay.pack(side=TOP)
radio1 = Radiobutton(frame1, text='kms', variable=radbtn, value='kms', command=value).pack(side=BOTTOM)
button=Button(frame1, text='miles int0kmsis', command=value).pack(side=BOTTOM)
root.mainloop()
答案 0 :(得分:0)
在value()
函数上,您应该使用txtDisplay.get()
获取值,并使用+ str(d)
正确的代码是:
import sys
from Tkinter import *
def value():
q = float(txtDisplay.get())
d = float(q+1.6)
label = Label(root, text='the value entered in kms is' + str(d)).pack(side=BOTTOM)
return
root = Tk()
root.title("kms converetr")
root.geometry('400x450+400+150')
frame = Frame(root)
frame.pack()
num1 = StringVar()
radbtn = StringVar()
radbtn.set(None)
frame1 = Frame(root)
frame1.pack(side=TOP)
label1 = Label(frame1, text='enter miles for conversion', fg='black',relief=RAISED)
label1.pack(side=TOP)
txtDisplay=Entry(frame1,bd=30,justify='center')
txtDisplay.pack(side=TOP)
radio1 = Radiobutton(frame1, text='kms', variable=radbtn, value='kms', command=value).pack(side=BOTTOM)
button=Button(frame1, text='miles int0kmsis', command=value).pack(side=BOTTOM)
root.mainloop()
顺便说一下,我认为你的里程到公里转换是错误的,但这是另一个问题。