Select to view content in your preferred language

python add-in hanging due to while loop

6168
4
Jump to solution
05-05-2015 02:59 AM
JelmerOosthoek
Deactivated User

I'm building a python add-in for ArcGIS desktop with a button which runs a while loop. Another button should close the while loop. In the while loop there is a continuous process going on which is important for the functionality of the add-in. But unfortunately the while loop causes the add-in, and therefore ArcGIS desktop, to hang.

I have tried time.sleep(.1) in the while loop. And I have tried the threading library. But nothing seems to work.

I was hoping someone maybe had a suggestion? Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
JaiSiva1
Deactivated User

Hi Jelmer ,

I think you got to change the logic that you have written in your program.

I have a similar kind of case which uses Tkinter package , using which you can modify your code.

  python - How do you create a Tkinter GUI stop button to break an infinite loop? - Stack Overflow

Hope this is useful.

View solution in original post

4 Replies
JaiSiva1
Deactivated User

Hi Jelmer,

  Can you please elaborate on this issue with the snippet that you have coded.

  From the information provided, i can suggest you to use event listeners to achieve the trigger ,

  #function that keeps checking if the second button is pressed.
def IsButtonPressed():
    if (<your condition for the event>):
          return True
    else :
          return False


  #function that keeps looping after the first button is pressed.
def Loop():
   while (IsButtonPressed()):
        <do some action>

  

If you can specify which GUI framework you are using , i can help with the events trigger part.

Best,

  Jai

JelmerOosthoek
Deactivated User

Hi Jai, thanks for your reply.

I'm currently not using any other GUI framework than ArcGIS itself, using the python add-in wizard: https://www.arcgis.com/home/item.html?id=5f3aefe77f6b4f61ad3e4c62f30bff3b

And by using a while loop in the code the tool and ArcGIS hangs. Here is a simple snippet example:

import arcpy
import pythonaddins


class ButtonClass1(object):
    """Implementation for tryaddin_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        # the while loop
        global switch
        while switch == 0:
            print 1
            global switch


class ButtonClass2(object):
    """Implementation for tryaddin_addin.button_2 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        # turn off the while loop
        global switch
        switch = 1        
        
switch = 0
0 Kudos
JaiSiva1
Deactivated User

Hi Jelmer ,

I think you got to change the logic that you have written in your program.

I have a similar kind of case which uses Tkinter package , using which you can modify your code.

  python - How do you create a Tkinter GUI stop button to break an infinite loop? - Stack Overflow

Hope this is useful.

JelmerOosthoek
Deactivated User

Hi Jai,

That did the trick! Thanks very much, it works like a charm, and doesn't even need threading.

All the best, Jelmer

0 Kudos