我需要弄清楚如何让代码的数学部分显示在tkinter窗口上,我该怎么做?谢谢你的帮助
from tkinter import *
def Maths():
kph = 0
for x in range(12):
kph = kph + 10
mph = kph * 0.6214
print(kph," ",mph)
def main():
app = Tk()
app.title("kph to mph")
app.geometry('300x450')
Label(app, text="KPH to MPH converter").pack()
Label(app, text="-----------------------------").pack()
Label(app, text="KPH MPH").pack()
Label(app, text="-----------------------------").pack()
b1 = Button(app, text="Convert", command=Maths)
b1.pack(side='bottom')
app.mainloop()
main()
答案 0 :(得分:0)
我看到你最终使用了一个列表,但我认为你可能会觉得这很有帮助。 Tkinter标签有一个内置的textvariable选项,您可以设置它,它将在StringVar()设置时更新。我修改了Maths函数,将输出设置为我在标签中添加的StringVar。经过测试和工作。
from tkinter import *
def Maths():
#New Code
temp = ""
kph = 0
for x in range(12):
kph = kph + 10
mph = kph * 0.6214
#New Code
temp += ("%d %d\n" % (kph,mph))
output.set(temp)
app = Tk()
app.title("kph to mph")
app.geometry('300x450')
Label(app, text="KPH to MPH converter").pack()
Label(app, text="-----------------------------").pack()
Label(app, text="KPH MPH").pack()
Label(app, text="-----------------------------").pack()
b1 = Button(app, text="Convert", command=Maths)
b1.pack(side='bottom')
#New Code
output = StringVar()
Label(app, textvariable=output).pack()
app.mainloop()