James Crabtree
James Crabtree

Reputation: 50

Can't get contents of tkinter Entry widget in Python 3

I'm starting out in Python 3 and I am having problems getting a value back from a tkinter widget. When I run my code I get a 'name edtGetMe is not defined' error. Can anyone see where I am going wrong?

from tkinter import *

# Define the class that forms my window.  
class Window(Frame):

def __init__(self, master=None):
    Frame.__init__(self, master)
    self.master = master
    self.init_window()

# Init the class.
def init_window(self):
    # Place widgets on the window.
    self.pack(fill=BOTH, expand=1)

    # Quit button.
    btnCancel = Button (self, text="Cancel", command=self.cancel_out)
    btnCancel.place (x=10, y=120)

    # Action button.
    btnAction = Button (self, text="Set Text", command=self.action)
    btnAction.place (x=100, y=120)

    # The Editboxes.
    edtGetMe = Entry (self)
    edtSetMe = Entry (self)
    edtGetMe.place (y=10, x=10, width=380, height=100)
    edtSetMe.place (y=155, x=10, width=380, height=100)

def cancel_out(self):
    exit()

def action (self):
    # Get the entry from the GetMe box.
    self.usrText = edtGetMe.get()
    # Stuff it into the other box.
    edtSetMe.insert(self.usrText)

def main():
     root = Tk()

# Size of window.
root.geometry("400x300")

# Start Window Class. 
app = Window(root)
root.mainloop()

if name == "main": main()

Upvotes: 0

Views: 183

Answers (1)

Phoenixo
Phoenixo

Reputation: 2133

First I replaced edtGetMe by self.edtGetMe and same for edtSetMe, so these variables are accessible from all the class functions. Then I added "insert" in your line self.edtSetMe.insert("insert", self.usrText). You also had indentation problems.

Try this :

from tkinter import *

# Define the class that forms my window.  
class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

    # Init the class.
    def init_window(self):
        # Place widgets on the window.
        self.pack(fill=BOTH, expand=1)

        # Quit button.
        btnCancel = Button (self, text="Cancel", command=self.cancel_out)
        btnCancel.place (x=10, y=120)

        # Action button.
        btnAction = Button (self, text="Set Text", command=self.action)
        btnAction.place (x=100, y=120)

        # The Editboxes.
        self.edtGetMe = Entry (self)
        self.edtSetMe = Entry (self)
        self.edtGetMe.place (y=10, x=10, width=380, height=100)
        self.edtSetMe.place (y=155, x=10, width=380, height=100)

    def cancel_out(self):
        exit()

    def action (self):
        # Get the entry from the GetMe box.
        self.usrText = self.edtGetMe.get()
        # Stuff it into the other box.
        self.edtSetMe.insert("insert", self.usrText)

def main():
    root = Tk()

    # Size of window.
    root.geometry("400x300")

    # Start Window Class. 
    app = Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Upvotes: 1

Related Questions