Is there any way to condense my code so it does not take up so many lines?
When refactoring code that is not DRY then try to identify the parts that actually do change and put those parts into some kind of data structure like a list or dictionary. Then wrap everything into a function.
apps = {'app1': r&quot;C:\Users\red\Desktop\app1&quot;,
        'app2': r&quot;C:\Program Files (x86)\app2&quot;,
        ...
       }
       
selected_app = clicked1.get()

def click():
    os.startfile(apps.get(selected_app))
    i = 0
    while i &lt; 20:
        try:
            app.connect(title_re=&quot;.*&quot; + selected_app)
            break
        except pywinauto.findwindows.ElementNotFoundError:
            time.sleep(1)
            i += 1
    app1 = app.top_window()
    app1.move_window(x1, y1)
    time.sleep(.5)
    app1.maximize()