How to check if button exists?

How would I check to see if a button exists?

I tried the if not myButton.isEmpty(): method but I got a global variable not assigned error.

SO how do I check to see if myButton exists?

JB SKaggs

Sounds like you haven’t actually declared myButton yet. I recommend putting myButton = None somewhere during initialization / at the top of your program, and then do:

if myButton != None and not myButton.isEmpty():
    # Button exists

If you don’t want to initialize it to None, you can see if the variable has been declared at all like this:

try:
    myButton
except NameError:
    # Button hasn't been declared

You really helped me out:)

Thanks

JB Skaggs

what about

hasattr(self, myButton)

?

Sure, assuming you’re inside an object. I guess I was assuming it was global scope.

Nemesis, it’s hasattr(self,“myButton”).

Yes I am talking about a global not local.

JB

Then it’s :
if “myButton” in globals()

Although that works, it’s really not a good idea to do it like that. It’s best to preinitialize the variable to None and test if it isn’t None later.