Reputation: 17
Is there an easier or simpler way to enable the submit button, when the user makes the first letter of a word capital? (I don't want the user to submit lower case words)
def enable_sumbit_button(*event):
self.uppercase_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J',
'K','L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z']
self.user_message = self.entry_box.get()
self.send_button.configure(state='disabled')
if self.user_message[0] in self.uppercase_letters:
self.send_button.configure(state='normal')
self.send_button = Button(self.main_window,text="SEND", command=print_user_message, state='disabled')
self.send_button.place(x=410, y=215, width=50, height=28)
self.main_window.bind("Entry","<FocusOut>", enable_sumbit_button)
self.user_input.trace('w', enable_sumbit_button)
Upvotes: 0
Views: 50
Reputation: 10936
Python strings have a method isupper() that tests for upper case. Also your string self.user_message is probably not guaranteed to have any elements (if the user backspaces over the only character in the field). In that case you get an exception when you try to access [0]. So you should check for a non-empty string first before you check the case of the first character.
def enable_sumbit_button(*event):
self.user_message = self.entry_box.get()
self.send_button.configure(state='disabled')
if self.user_message and self.user_message[0].isupper():
self.send_button.configure(state='normal')
Upvotes: 0
Reputation: 4510
Just use the isupper
method of strings.
if self.user_message[0].isupper():
...
Upvotes: 1
Reputation: 2427
An easier alternative would be to force the content of the Entry widget to uppercase, as soon as it looses the focus (or when user validates by pressing the Enter
key), so there is no more need to disable the submit button:
def convert_uppercase(*event):
self.entry_box.set(self.entry_box.get().upper())
and bind the <FocusOut>
event as you did:
self.main_window.bind("Entry","<FocusOut>", convert_uppercase)
You may even totally remove the convert_uppercase
function and perform the corresponding job into the callback function of your submit button.
Upvotes: 1