Using ".get()" with "entry"(TKinter)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ElBeardo
    New Member
    • Jan 2008
    • 2

    Using ".get()" with "entry"(TKinter)?

    Hello, I´m new to Python.. so this is a newbee question.

    I´d like to put the value enterd in the entryfield in a variable.

    I´m trying to build a calculator with python and TKinter, coding it in just python works good. But making it with TK is a bit hard.

    This is my unfinished code, i have tryed get() in many ways,.but I cant make it work. I´m only need to get knowledge about using get() now,.. I now there is other thing undone in this code.

    #
    import Tkinter
    from tkSimpleDialog import *
    import math
    import string

    def Add():
    xy = x + y


    def Sub():
    xy = x - y


    def Mul():
    xy = x * y


    def Div():
    xy = x / y


    root = Tkinter.Tk()

    x = Entry(root)
    x.grid(row=0, column=0, columnspan=2)

    y = Entry(root)
    y.grid(row=0, column=2, columnspan=2)

    text = Label(root, text="summa")
    text.grid(row=1 , column=0)

    Button(root, text='add', command=Add).gr id(row=2, column=0, columnspan=1)
    Button(root, text='sub', command=Sub).gr id(row=2, column=1, columnspan=1)
    Button(root, text='mul', command=Mul).gr id(row=2, column=2, columnspan=1)
    Button(root, text='div', command=Div).gr id(row=2, column=3, columnspan=1)

    root.mainloop()

    "
  • dazzler
    New Member
    • Nov 2007
    • 75

    #2
    no morjesta :D

    use: Entry.get(x)
    and remember that the text field contains text, so you must convert it to numeric using float() (or double() for greater accuracy)

    [code=python]
    import Tkinter
    from tkSimpleDialog import *
    import math
    import string

    def Add():
    x1 = float(Entry.get (x))
    y1 = float(Entry.get (y))
    xy = x1 + y1
    print xy

    def Sub():
    x1 = float(Entry.get (x))
    y1 = float(Entry.get (y))
    xy = x1 - y1
    print xy

    def Mul():
    x1 = float(Entry.get (x))
    y1 = float(Entry.get (y))
    xy = x1 * y1
    print xy

    def Div():
    x1 = float(Entry.get (x))
    y1 = float(Entry.get (y))
    xy = x1 / y1
    print xy

    root = Tkinter.Tk()

    x = Entry(root)
    x.grid(row=0, column=0, columnspan=2)

    y = Entry(root)
    y.grid(row=0, column=2, columnspan=2)

    text = Label(root, text="summa")
    text.grid(row=1 , column=0)

    Button(root, text='add', command=Add).gr id(row=2, column=0, columnspan=1)
    Button(root, text='sub', command=Sub).gr id(row=2, column=1, columnspan=1)
    Button(root, text='mul', command=Mul).gr id(row=2, column=2, columnspan=1)
    Button(root, text='div', command=Div).gr id(row=2, column=3, columnspan=1)

    root.mainloop()
    [/code]

    Comment

    • ElBeardo
      New Member
      • Jan 2008
      • 2

      #3
      Thanks,.. I know now what I did wrong. =)

      Comment

      • Mandy786
        New Member
        • Oct 2016
        • 2

        #4
        Originally posted by dazzler
        no morjesta :D

        use: Entry.get(x)
        and remember that the text field contains text, so you must convert it to numeric using float() (or double() for greater accuracy)

        [code=python]
        import Tkinter
        from tkSimpleDialog import *
        import math
        import string

        def Add():
        x1 = float(Entry.get (x))
        y1 = float(Entry.get (y))
        xy = x1 + y1
        print xy

        def Sub():
        x1 = float(Entry.get (x))
        y1 = float(Entry.get (y))
        xy = x1 - y1
        print xy

        def Mul():
        x1 = float(Entry.get (x))
        y1 = float(Entry.get (y))
        xy = x1 * y1
        print xy

        def Div():
        x1 = float(Entry.get (x))
        y1 = float(Entry.get (y))
        xy = x1 / y1
        print xy

        root = Tkinter.Tk()

        x = Entry(root)
        x.grid(row=0, column=0, columnspan=2)

        y = Entry(root)
        y.grid(row=0, column=2, columnspan=2)

        text = Label(root, text="summa")
        text.grid(row=1 , column=0)

        Button(root, text='add', command=Add).gr id(row=2, column=0, columnspan=1)
        Button(root, text='sub', command=Sub).gr id(row=2, column=1, columnspan=1)
        Button(root, text='mul', command=Mul).gr id(row=2, column=2, columnspan=1)
        Button(root, text='div', command=Div).gr id(row=2, column=3, columnspan=1)

        root.mainloop()
        [/code]
        Where did print function display the output on screen...
        Means if we want to display them in another entry box how can we put..

        Comment

        Working...