0

newbie question, but please i need help
i just started learning gui programming on python, been getting this error on a code am writing concerning uising the get() method to read an integer from my entry box

from Tkinter import *
root=Tk()
var=StringVar()
label=Label(root, textvariable=var, relief=RAISED)
var.set("converter")
label.pack()
L1=Label(text="x:")
L1.pack(side= LEFT)
E1=Entry(bd=5)
E1.pack()
content =int(E1.get())
import Tkinter
def m_km(content):
    km=content/1000
    tkMessageBox.showinfo("Answer" , "%d km" %(km))
B=Tkinter.Button(text="metres to kilometres", command= m_km)
B.pack(side= RIGHT)
mainloop()
3
Contributors
3
Replies
20
Views
3 Years
Discussion Span
Last Post by dreking6
0

You are trying to get integer from the box even it has not yet been entered. You are reimporting Tkinter, even you imported it all to main namespace in the beginning.

Edited by pyTony: addition

0

Mildly modified so it will actually work ...

from Tkinter import *
import tkMessageBox

def m_km():
    content = int(E1.get())
    # force floating point division 
    km = content/1000.0  
    #print(km)  # test
    tkMessageBox.showinfo("Answer" , "%f km" % (km))

root=Tk()

label = Label(root, text="converter", relief=RAISED)
label.pack()

E1 = Entry(bd=5)
E1.pack()
E1.focus()

L1 = Label(text="x:")
L1.pack(side=LEFT)
B = Button(text="metres to kilometres", command=m_km)
B.pack(side=RIGHT)

mainloop()

Note: capitalized variable names by style convention are used for class names

Edited by vegaseat

0

thanks so much guys,i got it but due to some reasons ive not been able to login to update.i've even gone up to the stage of converting it to exe and compiling it(my first)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.