I have a StringVar with Tkinter, I want to check that it is not empty. When I run the following condition, it is accepted while the variable is empty.
if self.var:
print(self.var)
The printed variable is PY_VAR1.
if self.var!= 'PY_VAR1':
print(self.var)
The printed variable is also PY_VAR1.
How to check that the value of self.var is not equal to PY_VAR1?
When I use get, it doesn't work either
if self.var.get():
print(self.var.get())
And when I change the value of self.var, with get (), nothing happens.
self.bouton = ttk.Button(self, text="print var", command=self.printvar)
def printvar(self):
print(self.var.get())
When I click on the button, the value of self.var is displayed and even when I change the value of self.var (), the new value is displayed. But I want to display the new value of self.var without clicking on the button. I want it to display dynamically.
Try these:
# no.1
if len(string_variable.get()) == 0:
do_task()
# no.2
'''We use try-except function so we don't get a ValueError if the specified character
isn't present in the if-condition'''
try:
if string_variable.index("Any character in a word that needs to be checked"):
'''If you want to check "PY_Var1", just type('1') or any other character in
the if-condition.'''
do_task()
except:
do_task()
# If none work, try storing the StringVar in another variable.
new_string_variable = string_variable.get()
if len(new_variable_string) == 0:
do_task()
# To check if the string variable(StringVar) isn't equal to "any_string".
if (string_variable.get()) != "any_string":
do_task()
If you still have any problem, feel free to ask any questions!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With