import Tkinter

class converter(Tkinter.Tk):
   
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.labelVariable = Tkinter.IntVar()
        welcome = Tkinter.Label(self, textvariable=self.labelVariable, anchor="w", fg="black", bg="lightgrey")
        welcome.grid(column=0, row=0, columnspan=3, sticky='EW')
        self.labelVariable.set(u"Hello there. Welcome to MAKK's currency converter.")

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.grid(column=0, row=1, sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable.set(u"Enter currency here.")
        money = self.entryVariable.set

        toUSD = Tkinter.Button(self, text=u"Convert to USD", command=self.ConvertToUSD)
        toUSD.grid(column=1, row=1)
        
        toCAD = Tkinter.Button(self, text=u"Convert to CAD", command=self.ConvertToCAD)
        toCAD.grid(column=2, row=1)

        self.labelVariable = Tkinter.StringVar()
        output = Tkinter.Label(self, textvariable=self.labelVariable, anchor="w", fg="black", bg="lightgrey")
        output.grid(column=0, row=3, columnspan=3, sticky='EW')
        self.labelVariable.set(u"Hello !")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(False,False)
        self.update()
        self.geometry(self.geometry())
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def ConvertToUSD(self):
        self.labelVariable.set(self.entryVariable.get()+ "You clicked the button !")
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def ConvertToCAD(self):
        money = entry(self)
        money = money * 1.05
        self.labelVariable.set("The converted currency in CAD is " + money)
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

    def OnPressEnter(self,event):
        self.labelVariable.set(self.entryVariable.get()+ "You pressed enter !")
        self.entry.focus_set()
        self.entry.selection_range(0, Tkinter.END)

if __name__ == "__main__":
    app = converter(None)
    app.title('Currency Converter')
    app.mainloop()

I get the error "NameError: global name 'entry' is not defined".. I'm trying to use the text inputted in the textbox as an integer variable usable to convert currency between CAD and USD.

Member Avatar for masterofpuppets

hi,
I think the problem is here:

def ConvertToCAD(self):
    # get text as string and convert it to float
    money = float( self.entry.get() )
    # do computation
    money = money * 1.05
    # update label by again convertion money into string
    self.labelVariable.set("The converted currency in CAD is " + str( money ) )
    self.entry.focus_set()
    self.entry.selection_range(0, Tkinter.END)

You should use the .get() method to get the text from the Entry field and convert it either to int or float and then do the multiplication. When you get text from Entry field the text is a string so you need conversion :)

hope this helps :)

It works ! Masterofpuppets, thank you so much, i was stuck on that one part for so long. I'm really new to programming, and it was bothering me. Thanks again !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.