0

I need to figure out how to get the maths section of code to appear on the tkinter window, How would I do this? thanks for the help

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()
Mia
  • 2,466
  • 22
  • 38
Tobleh
  • 5
  • 3
  • It's unclear for what you're asking, but it's look like you just need a pair of [entries](http://effbot.org/tkinterbook/entry.htm) to show speeds. One for `kph` and another for `mph`. – CommonSense Apr 17 '17 at 07:42
  • I started off having to make a program which converted the kph values from 1-12 into their equivalent mph speeds. However, now I need to make an interface for that program and I am not sure how to get the main section of the code to appear, without just adding them all as their own label. – Tobleh Apr 17 '17 at 10:22
  • what's wrong with "adding them all as their own"? You can use [this](http://stackoverflow.com/a/11049650/6634373) snippet for that purposes. Just a looks-like-a-table set of labels. You can switch from labels to entries if you wish. If you don't like it either - you can try [listbox/treeview](https://www.daniweb.com/programming/software-development/threads/350266/creating-table-in-python) widgets. – CommonSense Apr 17 '17 at 11:36
  • ok, Thanks for the help, I ended up using the list – Tobleh Apr 17 '17 at 23:16

1 Answers1

0

I saw that you ended up using a list but I thought you might find this helpful. Tkinter labels have a built in textvariable option that you can set and it will update as the StringVar() is set. I modified your Maths function to set your output to the StringVar I added in the label. Tested and Works.

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()