Question

The thing is, I've expanded on this code more, and I am having another problem: The binding function is being ignored or something when I run this code (Of course I have my window setup above this as always):

from tkinter import *
#Window setup, ignore this mostly
app = Tk()
app.title('Geometry Calculator')
app.geometry('384x192+491+216')
app.iconbitmap('Geo.ico')
app.minsize(width=256, height=96)
app.maxsize(width=384, height=192)
app.configure(bg='WhiteSmoke')

PointList = []


def AddCheck(event):
    Point = e1.get()
    PointTest = Point
    if PointTest.find(',') is True:
        PTest_X = PointTest[0].split(',')
        PTest_Y = PointTest[1].split(',')
        try:
            PTest_X = float(PTest_X)
            PTest_Y = float(PTest_Y)
            PointList.append(Point)
            l1 = Label(app, text='PointList').grid(row=1, column=0)
            e1.delete(0, END)
        except:
            print('Error: Invalid point format.')
    if PointTest.find(',') is False:
        print('Error: Invalid point format.')

e1 = Entry(app)
e1.grid(row=0, column=0, sticky=W)
Entry.bind(e1, '<Return>', AddCheck)
mainloop()

Basically, my goal was to make a "checker" or whatever you might call it for the string entered into the Entry box. I am dealing with coordinates, so the first thing I wanted to do was to see if the string contained a comma. If it doesn't contain one, I automatically signal an error to the Terminal, and I will to the window later. The comma will split up the two main parts of the string I will be operating on in the ultimate function of this program. So, I split the string from the comma into the two parts. Then I wanted to see if the split parts of the string (PTest_X and PTest_Y) could be converted into floats with a Try statement, which obviously means only integers or floating point numbers that are before or after the comma will work under a conversion. The Except part of the Try statement just tells to return an error to the Terminal as well, like before. So, if PTest_X and PTest_Y can be converted, that's when I finally want to append them to a list, make a label of the list, and do whatever else.

The point of this "checker" system is to give feedback to the user telling them that their coordinate point "syntax", if you will, is incorrect. I would rather implement this system at this point to tell the user they have entered in a point incorrectly rather than telling them them the this after everything has already been processed.

I am doing this for user friendliness, which TRULY always comes first when it comes to user interaction. I hope to alleviate any pain from the user's experience with the program.

Was it helpful?

Solution

The binding function is working fine. The problem is that you are using find() & checking if its True. The find() returns the index if it does find the string else returns -1.

Also,your split doesn't work either as it is just getting the value of number before , at index 0,1.

from tkinter import *
#Window setup, ignore this mostly
app = Tk()
app.title('Geometry Calculator')
app.geometry('384x192+491+216')
app.iconbitmap('Geo.ico')
app.minsize(width=256, height=96)
app.maxsize(width=384, height=192)
app.configure(bg='WhiteSmoke')

PointList = []


def AddCheck(event):
    print ("hello")
    Point = e1.get()
    PointTest = Point
    print (PointTest)
    if ',' in PointTest:
        PTest_X, PTest_Y =PointTest.split(',')
        try:
            PTest_X = float(PTest_X)
            PTest_Y = float(PTest_Y)
            PointList.append(Point)
            l1 = Label(app, text=PointList)
            l1.grid(row=1, column=0)
            e1.delete(0, END)
        except:
            print('Error: Invalid point format.')
    if PointTest.find(',') is False:
        print('Error: Invalid point format.')

e1 = Entry(app)
e1.grid(row=0, column=0, sticky=W)
e1.bind('<Return>', AddCheck)
app.mainloop()

If its different labels you require then you would have to use loops

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top