I just recently began programming and am attempting to create a simple mean/median/mode calculator with a simple GUI. I have only found how to save strings to a variable, however I want to save only int's to my existing 'UserNumbers' variable. I'm not sure if my code order is correct either.
mGui = Tk()
mGui.geometry('300x300+600+200')
mGui.title('Mean Median Mode')
mlabel = Label(text='Enter number sequence \n seperated by spaces', fg='red').place(x=7,y=25)
mbutton = Button(text = "Calculate" ).place(x=220, y=250)
mEntry = Entry(mGui, ).place(x=150,y=30)
UserNumbers=input("Enter number sequence separated by spaces: ")
nums = [int(i) for i in UserNumbers.split()]
Average = mean(nums)
print ("The mean is ", Average)
Middle = median(nums)
print ("The median is ", Middle)
if len(set(nums)) == len(nums):
print("There is no mode ")
else:
Most = mode(nums)
print ("The mode is ", Most)
Ok, so you have a GUI going, but have inserted UserNumbers = input(...)
in the middle of
it, this will cause an input window to come up, and then the GUI window, I don't think this is what you wanted. This is a GUI way to get an integer value:
import tkinter
root = tkinter.Tk()
nos = tkinter.Entry()
nos.pack()
def getnum():
nums = int(nos.get())
print (nums)
but = tkinter.Button(text = 'enter', command = getnum).pack()
root.mainloop()
This is an input way to get an integer value:
UserNumbers = int(input('whatever you want it to ask...'))
I think that the GUI way would probably be better for you, as you can split the numbers up with the enter button, and include in the getnum
function an if statement to test for whether a certain number of values has been reached, or have another button for when the user has reached the end of their list, this would stop the mainloop
. But you can do either.
Hope this helps, if you need anything else just say.