Python Forum
[Tkinter] How to use .get() for Entry?
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to use .get() for Entry?
#1
I tried a lot of stuff but I kept failing so I gave up.

Here is what I am having a problem with:
def SubmitCurrency(event):
    CurrencyTaken = Entry.get(Entry1)
    print(CurrencyTaken)
I even tried (it says Entry1 is not defined)

def SubmitCurrency(event):
    CurrencyTaken = Entry.get(self.Entry1)
    print(CurrencyTaken)
But then it says "self" is not defined. So I even tried changing all the names from self.Entry1 to self and then try but it still said that the thing was not defined.

widget code:
self.Entry1 = Entry(top)
        self.Entry1.place(relx=0.04, rely=0.1,height=20, relwidth=0.19)
        self.Entry1.configure(background="white")
        self.Entry1.configure(disabledforeground="#a3a3a3")
        self.Entry1.configure(font="TkFixedFont")
        self.Entry1.configure(foreground="#000000")
        self.Entry1.configure(insertbackground="black")
        self.Entry1.configure(width=44)
I tried a lot of stuff but I kept failing so I gave up.

Here is what I am having a problem with:
def SubmitCurrency(event):
    CurrencyTaken = Entry.get(Entry1)
    print(CurrencyTaken)
I even tried

def SubmitCurrency(event):
    CurrencyTaken = Entry.get(self.Entry1)
    print(CurrencyTaken)
But then it says "self" is not defined. So I even tried changing all the names from self.Entry1 to self and then try but it still said that the thing was not defined.

widget code:

I tried a lot of stuff but I kept failing so I gave up.

Here is what I am having a problem with:
def SubmitCurrency(event):
    CurrencyTaken = Entry.get(Entry1)
    print(CurrencyTaken)
I even tried

def SubmitCurrency(event):
    CurrencyTaken = Entry.get(self.Entry1)
    print(CurrencyTaken)
But then it says "self" is not defined. So I even tried changing all the names from self.Entry1 to self and then try but it still said that the thing was not defined.

widget code:
        self.Entry1 = Entry(top)
        self.Entry1.place(relx=0.04, rely=0.1,height=20, relwidth=0.19)
        self.Entry1.configure(background="white")
        self.Entry1.configure(disabledforeground="#a3a3a3")
        self.Entry1.configure(font="TkFixedFont")
        self.Entry1.configure(foreground="#000000")
        self.Entry1.configure(insertbackground="black")
        self.Entry1.configure(width=44)
So when I run that event, which is supposed to get what is inside the entry and print it, it says that "self" is not defined, even when I renamed the self.Entry1 to "self". I just don't know what to do here.

The script was generated by PAGE.

Here is the full script:
#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.14
# In conjunction with Tcl version 8.6
#    Jun 15, 2018 02:37:37 PM

import sys

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

import unknown_support

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = Tk()
    top = Converter (root)
    unknown_support.init(root, top)
    root.mainloop()

w = None
def create_Converter(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt
    rt = root
    w = Toplevel (root)
    top = Converter (w)
    unknown_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_Converter():
    global w
    w.destroy()
    w = None


class Converter:
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85' 
        _ana2color = '#d9d9d9' # X11 color: 'gray85' 

        top.geometry("228x101+504+157")
        top.title("Converter")
        top.configure(background="#d9d9d9")



        self.Entry1 = Entry(top)
        self.Entry1.place(relx=0.04, rely=0.1,height=20, relwidth=0.19)
        self.Entry1.configure(background="white")
        self.Entry1.configure(disabledforeground="#a3a3a3")
        self.Entry1.configure(font="TkFixedFont")
        self.Entry1.configure(foreground="#000000")
        self.Entry1.configure(insertbackground="black")
        self.Entry1.configure(width=44)

        self.Entry1_1 = Entry(top)
        self.Entry1_1.place(relx=0.04, rely=0.4,height=20, relwidth=0.19)
        self.Entry1_1.configure(background="white")
        self.Entry1_1.configure(disabledforeground="#a3a3a3")
        self.Entry1_1.configure(font="TkFixedFont")
        self.Entry1_1.configure(foreground="#000000")
        self.Entry1_1.configure(highlightbackground="#d9d9d9")
        self.Entry1_1.configure(highlightcolor="black")
        self.Entry1_1.configure(insertbackground="black")
        self.Entry1_1.configure(selectbackground="#c4c4c4")
        self.Entry1_1.configure(selectforeground="black")

        self.Button1 = Button(top)
        self.Button1.place(relx=0.26, rely=0.09, height=24, width=49)
        self.Button1.configure(activebackground="#d9d9d9")
        self.Button1.configure(activeforeground="#000000")
        self.Button1.configure(background="#d9d9d9")
        self.Button1.configure(disabledforeground="#a3a3a3")
        self.Button1.configure(foreground="#000000")
        self.Button1.configure(highlightbackground="#d9d9d9")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(pady="0")
        self.Button1.configure(text='''Submit''')
        self.Button1.bind("<Button-1>", SubmitCurrency)

        self.Button1_2 = Button(top)
        self.Button1_2.place(relx=0.26, rely=0.38, height=24, width=49)
        self.Button1_2.configure(activebackground="#d9d9d9")
        self.Button1_2.configure(activeforeground="#000000")
        self.Button1_2.configure(background="#d9d9d9")
        self.Button1_2.configure(disabledforeground="#a3a3a3")
        self.Button1_2.configure(foreground="#000000")
        self.Button1_2.configure(highlightbackground="#d9d9d9")
        self.Button1_2.configure(highlightcolor="black")
        self.Button1_2.configure(pady="0")
        self.Button1_2.configure(text='''Submit''')
        self.Button1_2.bind("<Button-1>", SubmitCurrency)

        self.Label1 = Label(top)
        self.Label1.place(relx=0.53, rely=0.1, height=21, width=28)
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(text='''USD''')

        self.Label1_3 = Label(top)
        self.Label1_3.place(relx=0.53, rely=0.4, height=21, width=28)
        self.Label1_3.configure(activebackground="#f9f9f9")
        self.Label1_3.configure(activeforeground="black")
        self.Label1_3.configure(background="#d9d9d9")
        self.Label1_3.configure(disabledforeground="#a3a3a3")
        self.Label1_3.configure(foreground="#000000")
        self.Label1_3.configure(highlightbackground="#d9d9d9")
        self.Label1_3.configure(highlightcolor="black")
        self.Label1_3.configure(text='''USD''')

        self.Button2 = Button(top)
        self.Button2.place(relx=0.7, rely=0.08, height=54, width=57)
        self.Button2.configure(activebackground="#d9d9d9")
        self.Button2.configure(activeforeground="#000000")
        self.Button2.configure(background="#d9d9d9")
        self.Button2.configure(disabledforeground="#a3a3a3")
        self.Button2.configure(foreground="#000000")
        self.Button2.configure(highlightbackground="#d9d9d9")
        self.Button2.configure(highlightcolor="black")
        self.Button2.configure(pady="0")
        self.Button2.configure(text='''Convert''')
        self.Button2.configure(width=57)
        self.Button2.bind("<Button-1>", Sum)

        self.Label2 = Label(top)
        self.Label2.place(relx=0.04, rely=0.69, height=26, width=50)
        self.Label2.configure(background="#d9d9d9")
        self.Label2.configure(disabledforeground="#a3a3a3")
        self.Label2.configure(foreground="#000000")
        self.Label2.configure(text='''Amount''')
        self.Label2.configure(width=50)

        self.Entry2 = Entry(top)
        self.Entry2.place(relx=0.31, rely=0.69,height=20, relwidth=0.63)
        self.Entry2.configure(background="white")
        self.Entry2.configure(disabledforeground="#a3a3a3")
        self.Entry2.configure(font="TkFixedFont")
        self.Entry2.configure(foreground="#000000")
        self.Entry2.configure(insertbackground="black")
        self.Entry2.configure(width=144)


def SubmitCurrency(event):
    CurrencyTaken = Entry.get(Entry1)
    print(CurrencyTaken)


def Sum(event):
    print("LOL")
    
    


if __name__ == '__main__':
    vp_start_gui()
Reply
#2
Indent SubmitCurrency so it's part of the class, and add self as it's first parameter. Then, anywhere you bind the function, you'll need to change it to bind to self.SubmitCurrency instead. Once that's done, you can reference the instance variables from within the event handler.
Reply
#3
How to use get() to get the contents of an Entry http://effbot.org/tkinterbook/entry.htm
Reply
#4
(Jun-15-2018, 06:22 PM)nilamo Wrote: Indent SubmitCurrency so it's part of the class, and add self as it's first parameter. Then, anywhere you bind the function, you'll need to change it to bind to self.SubmitCurrency instead. Once that's done, you can reference the instance variables from within the event handler.

So I indent "def SubmitCurrency(event):", but I didn't understand what you meant by "add self as its first parameter".. Also didn't understand what I needed to change to to bind to self.SubmitCurrency. And how to reference that variable..

Can you explain it to me like I'm a 5 year old? haha oopsies.
Reply
#5
What was once def SubmitCurrency(event): should now be def SubmitCurrency(self, event):, as it's now a method of the Converter class. Which also means that any time you want to call it, you need to do so with a reference to a Converter instance.

So, things like this: self.Button1.bind("<Button-1>", SubmitCurrency) should be changed like so: self.Button1.bind("<Button-1>", self.SubmitCurrency)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,477 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,509 Mar-18-2019, 05:36 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020