我正在使用PySimpleGUI和PyAutoGUI自动化一个非常繁琐的Excel到Enovia数据迁移项目,我的任务是在Enovia上寻找一部分,从excel中键入特定属性并保存。我想出了这个方法,它需要这些初始条件,为我操纵鼠标和键盘并编辑零件。
makeChanges()函数已经过广泛的独立测试,并且在编辑方面表现出色。但是,当我使用“编辑”按钮时,它会执行makeChanges()函数,但它会冻结GUI,并且如果弄乱它会导致崩溃,除非,整个makeChanges()函数都已执行,只有这样是可操作的GUI。
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()
我尝试使用事件循环,其结果与不使用事件循环的结果相同。我想要的是能够在运行编辑功能的同时退出该程序,并且将来还可以在任何给定时间暂停/继续编辑。请原谅代码的丑陋之处。我是新手,我想知道一些如何变得更好的提示!