I have been working with numbers in tkinter using operators but I'm unsure on how to process the input and using maths on them. I also wouldn't mind knowing how to use words either for future reference. Reply if you know anything.

Recommended Answers

All 7 Replies

Here is an example ...

''' Tk_Entry_numbers1.py
a general Tkinter program to enter two data items
process the data and display the result
tested with Python27/33
'''

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def calculate():
    """
    in this case the data items are floating point numbers
    that are simply added in the example data process
    the result is displayed in a label
    """
    try:
        x = float(enter1.get())
        y = float(enter2.get())
        # the process step, customize to your needs ...
        s = str(x + y)
        # display the result
        label3.config(text=s)
    except ValueError:
        label3.config(text='Enter numeric values for x and y')

def setfocus2(event):
    enter2.focus_set()


# create the root window
root = tk.Tk()

# create all the components/widgets
label1 = tk.Label(root, text='Enter number x:', width=28)
enter1 = tk.Entry(root, bg='yellow')
label2 = tk.Label(root, text='Enter number y:')
enter2 = tk.Entry(root, bg='yellow')
button1 = tk.Button(root, text='Add the numbers', command=calculate)
# the result label
label3 = tk.Label(root, text='', bg='green')

# pack widgets vertically in the root window in that order
label1.pack(side='top', fill='x')
enter1.pack(side='top', fill='x')
label2.pack(side='top', fill='x')
enter2.pack(side='top', fill='x')
button1.pack(side='top')
label3.pack(side='top', fill='x')

# cursor in enter1
enter1.focus()
# return key in enter1 sets focus to enter2
enter1.bind("<Return>", func=setfocus2)

# start the event loop
root.mainloop()

When I try it says: 'NoneType' object has no attribute 'get'

Sounds like you are doing something like this ...
enter1 = tk.Entry(root, bg='yellow').pack()

Wit ...

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()

enter1 = tk.Entry(root, bg='yellow').pack()
print(enter1)  # None

enter2 = tk.Entry(root, bg='yellow')
print(enter2)  # .48379408
enter2.pack()

root.mainloop()

Layout manager grid() will give the same problem that pack() will. Use grid() after you assigned the entry object to a variable name or it will be None.

Do not use:
enter1 = tk.Entry(root, bg='yellow').grid()
but:
enter1 = tk.Entry(root, bg='yellow')
enter1.grid()

In other words ...
calls to pack() or grid() will return None

entropic310500 you need to show us some of your code!

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.