Python Forum
GUI freezes while executing a callback funtion when a button is pressed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI freezes while executing a callback funtion when a button is pressed
#1
I'm using PySimpleGUI and PyAutoGUI to automate a very tedious Excel to Enovia data migration project, where my task is to look for a part on Enovia, type in specific attributes from excel and save it. I came up with this, it takes these initial conditions, manipulates the mouse and keyboard and edits the part for me.

The makeChanges() function has been tested extensively standalone and is doing great at editing it. However when I use the 'Edit' button, it executes the makeChanges() function except it freezes the GUI and will essentially crash if messed with, unless the entire makeChanges() function has executed, and only then is the GUI manipulatable.

    
    import PySimpleGUI as sg      
    import pyautogui, time, ctypes
    pyautogui.FAILSAFE = True
    pyautogui.PAUSE = 1


    #Setting up lazy variables
    s = time.sleep
    moveToo = ctypes.windll.user32.SetCursorPos#(x, y) coords,
    py = pyautogui


    #Initializing Layout of Window and acquiring initial conditions
    layout = [[sg.Text('Please enter initial conditions(Only the first two letters)')],
              [sg.Text('Desired Lines', size=(14, 1)), sg.Input(size=(20,1), key='lines')],
              [sg.Text('Company Name ', size=(14, 1)), sg.InputText(size=(20,1), key='company')],
              [sg.Text('Filetype', size=(14, 1)), sg.InputText(size=(20,1), key='filetype')],
              [sg.Text('Dashnumbers', size=(14, 1)), sg.InputText(size=(20,1), key='dashnumbers')],
              [sg.Button('Edit'), sg.Button('Quit'), sg.Button('Continue')]]  


    window = sg.Window('Automatic Intern', layout)    


    #Button Callback Functions
    def Bu1(): #Button 1
        makeChanges(values['company'], values['dashnumbers'], values['filetype'])

    def Bu2(): #Button 2
        window.Close()



    #Callback Edit Function
    def makeChanges(company, dashnumbers, filetype):
        for i in range(len(lines)): #Convert to integars
            lines[i] = int(lines[i])
            lines[i] -= 1 #Subtract 1 to get real row numbers
            print(lines[i])
        #Parse through lines and make necessary edits
        for n, val in enumerate(lines): #Iterate through each element
            print(lines[n]) #Print element value
            py.moveTo(497,258,0.5)#Move to First Dropdown Marker, Checkpoint 1
            py.moveRel(0,30*lines[n])
            #Add another function that exectues the edit
            py.click()
            py.moveRel(73, 124)
            py.click()
            s(2.5)
            py.moveTo(249, 256)#Moving to and Clicking Hamburger Menu
            py.click()
            py.moveTo(273, 281)#Moving to and Clicking Edit Details
            py.click()
            py.moveTo(394, 786)#Moving to 'Customer' dropdown
            py.click()
            py.typewrite(company)#Typewrite given Company
            py.hotkey('tab')
            py.typewrite(dashnumbers)#Typein
            py.hotkey('space')
            py.hotkey('tab')
            py.typewrite(filetype)#Given Filetype
            py.hotkey('tab')
            py.hotkey('enter')
            py.moveTo(843, 98)#Move and Click Search Bar
            py.click()
            py.moveTo(806, 125)#Click last search result
            py.click()
            s(2)
            print('Done')


    event, values = window.Read() #Acquriing data in GUI
    lines = values['lines'].split(' ')
    print(event)

    if event == 'Edit': #Pass onto makeChanges()
        Bu1()
        window.Close()
        print('Executed')
    elif event == 'Quit': #Quit the program
        Bu2()
        window.Close()
What I would like is to for the GUI to be running while the edit function executes instead of freezing, and also in the future, to pause/continue the editing at any given time with buttons I intend to add into the GUI. Please do excuse the ugliness of the code. I'm new and I would love some tips on how to be better, so please do grill me on everything that makes you cringe. Big Grin
Reply


Messages In This Thread
GUI freezes while executing a callback funtion when a button is pressed - by abi17124 - Jul-08-2019, 10:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Tkinter callback exception Ben123 2 709 Feb-17-2024, 06:03 PM
Last Post: deanhystad
  TKinter restarting the mainloop when button pressed zazas321 7 16,664 Jan-26-2021, 06:38 AM
Last Post: zazas321
  [PyQt] SSH port forwarding and connection to SQL Server in separate threads freezes jan_pips 25 11,113 Dec-03-2019, 04:28 PM
Last Post: Denni
  [Tkinter] How to bind an event when enter is pressed on a Entry control? Michael4 4 4,021 Aug-29-2019, 10:11 PM
Last Post: Michael4
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,092 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [matpltlib]Basic question about callback function schniefen 3 3,285 May-20-2019, 09:47 PM
Last Post: Yoriz
  [Kivy] Chagne a button's function after its first pressed TheStraying11 2 5,145 Feb-17-2019, 06:16 PM
Last Post: Yoriz
  Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 20,937 Aug-05-2018, 11:01 PM
Last Post: woooee
  [Tkinter] tkinter freezes by clicking button Zatox11 33 26,199 Apr-10-2018, 09:03 AM
Last Post: Zatox11
  Running telnet loop freezes GUI reedhallen 1 3,499 Jan-27-2018, 10:24 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020