HI Guys,

I am trying to Create a GUI in Python that calculate the average of three numbers. I am having some issues tring to create the three boxes where the user should enter the numbers, so can anybody please give me a hand? Thank you so much!!!!

Recommended Answers

All 4 Replies

Think about your design.
One textbox user enter 3 number.
Then operator / and then user enter new number and you get and average.
Like a normal calculator.

What if user want average of 5 number,should you create 5 textboks?

Here is a generic program templet that takes 2 numbers, checks the input, processes the entered data and shows the result. The Tkinter GUI toolkit is used. You can easily add additional inputs and change the processing to whatever you want ...

# a general Tkinter program to enter two numbers and process them

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

def calculate():
    """ take the two numbers entered and process them """
    try:
        x = float(enter1.get())
        y = float(enter2.get())
        # the process step ...
        s = str(x + y)
        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()

Thank you so much Vegaseat!!That was really helpfull!!!Thanks again!!!!

thank you very much

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.