My program should check if the first three letters of the input word are similar to a predefined word.
I’ve made a GUI with Tkinter and want to get the letters of the input field.
Somehow I can’t implement it in like I would do without Tkinter.
That’s how I do it just for the shell:
text = raw_input('Enter a word: ')
if (text[0] + text[1] + text[2] == 'sag'):
print "sagen"
else:
print "error"
So, when I input the word “sagst” it checks the first three letters and should put out “sagen”. Works fine.
I learned that e.g. inputfield.get() gets the input of the entry “inputfield”.
But how can I check the first letters of that “inputfield”?
A small selection:
from Tkinter import*
root = Tk()
def check():
if (text[0] + text[1] + text[2] == 'sag'):
print "True"
else:
print "False"
inputfield = Entry(root)
inputfield.pack()
but = Button(root,text='Check!', command = check)
but.pack()
text = inputfield.get()
root.mainloop()
Does not work…
I hope you can understand my question and will answer soon. (Sorry for my bad english and my bad Python skills) 😉
Thanks!
Your
checkfunction will have to retrieve the textfield after the button has been pressed:I’ve changed your test a little, using
.startswith(), and directly printing the result of that test (print will turn booleanTrueorFalseinto the matching string).What happens in your code is that you define
inputfield, retrieve it’s contents (obviously empty), and only then show the TKInter GUI window by running the mainloop. The user never gets a chance to enter any text that way.