Python Pysimplegui opening wrong screen

Asked

Viewed 120 times

2

I have a Crawler using Pysimplegui to define the data to be collected and Selenium for data collection. After setting the data through a window created by Sg.Window(), I create a Sg.Popupanimated indicating the status of Crawler, but when running in the Pycharm IDE it works properly, after generating the EXE through the Pyinstaller, Auto-py-to-exe or Cx_freeze instead of generating the Popup it again displays the screen, follows the code below:

from selenium import webdriver
import PySimpleGUI as sg
import multiprocessing
import time
import sys
import re
import os

def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)

def getDados():
    global nome
    global email
    global user

    sg.SetOptions(text_justification='right')
    layout =[[sg.Text('Nome',size=(23,1)), sg.Input()],
            [sg.Text('E-mail',size=(23,1)), sg.Input()],
            [sg.Text('Nome e Sobrenome',size=(23,1)), sg.Input()],
            [sg.Submit('OK', pad=((420, 5), 5)), sg.Exit('Cancelar', pad=((5, 0), 5))]]
    window = sg.Window('Gera', layout, finalize=True)
    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED or event == 'Exit' or event == 'Cancelar':
            sys.exit(0)
        elif event == 'OK':
            #Valida se todos estão preenchidos
            if(values[0] =='' or values[1] =='' or values[2] ==''):
                sg.popup('Preencha todos os campos')
                continue
            # Valida formato do email
            elif (re.search("[a-z0-9.]+@[a-z0-9]+\.[a-z]", values[1]) == None or ' 'in values[1]):
                sg.popup('É Necessário Informar um e-mail Válido')
                continue
            # Valida se existe nome e sobrenome
            elif(re.search(". .", values[2]) == None):
                sg.popup('É Necessário Informar Nome e Sobrenome')
                continue
             else:
                 nome = values[0].upper()
                 email = values[1]
                 user = values[2]
                 window.close()
                 del window
                 return

def alerta(msg):
    while(True):
        sg.PopupAnimated('loading.gif', time_between_frames=100, message = msg, font ='Courier 12', keep_on_top=True)

def thread(desc,pare):
    if(pare == None and desc == None):
        pare.terminate()
        return
    if(pare!= None):
        pare.terminate()
    if(desc == None):
        pare.terminate()
        return
    th = multiprocessing.Process(target=alerta,args=(desc,))
    th.start()
    return th

if __name__ == '__main__':
    sg.SetGlobalIcon('logo.ico')
    getDados()
    th = thread('Aguarde', None)

    driver = webdriver.Chrome(executable_path=resource_path('chromedriver.exe'))
    driver.set_window_size(1920, 1080)

    try:
        try:
            th = thread('Logando', th)
        except NoSuchElementException:
            th = thread(None, th)
            sg.popup_error('Erro ao Efetuar o Login')

        time.sleep(60)
        try:
            th = thread('Acesando Perfil', th)
        except NoSuchElementException:
            th = thread(None, th)
            sg.popup_error('Erro ao Acesar Perfil')
    except NoSuchElementException:
        driver.get_screenshot_as_file('erro.png')

When executing the command sg.PopupAnimated() software opens function screen getDados(). However this only occurs after the EXE generation, while I run the . py code by the IDE it works normal, displaying the Popup.

1 answer

0


Good afternoon, I discovered the solution of the problem, it was displaying the same window because I was using the multiprocessing. Process(), because it created a new instance for execution, it ended up running the getDados() function again. To fix the problem I needed to make some changes, one of them was to change multiprocessing by Thread(), which fixed the window opening problem. But since I couldn’t finish Thread, I used only one Thread to run Crawler, and on main kept the window where the status was displayed. This way I created a variable to indicate the status and just checked it in a While inside Main.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.