I'm writing a script in Python with PySimpleGUI, however I'm facing issues in how to implement conditionals when a button is clicked.
When the button "Generate Proxy" is clicked, it first needs to verify if there is a valid token, if the token is invalid or there is no token, it should finish the iteration. However, I cannot use return in it because it is not a function, I tried to define come conditions in check_token() and check_country(), however, not how to correct implement it.
Considering the logic a valid token is necessary to execute the button before check country and other elements (as counter), is there any way to stop the button iteration, to allow the user to Get a Token, or correct the fields for country or others, before all "if" are checked in the workflow? How could be the best way to do it?
(Tried to check with ChatGPT, and it recommended to use return or break in button's workflow. What doesn't help because [1] return is only used in functions, [2] break will close the whole application when it is running.)
I tried to implement conditionals directly in the button, however, in order to try return button, I write two functions to help me to get information. I'm afraid that I'm not retrieving the boolean from the functions correctly to use it as reference in the conditional.
[PySimpleGUI Button]
elif event == "Generate Proxy":
output_element = window['output']
proxy_list_element = window['proxy_list']
timestamp = app.log_time_stamp()
output_element.update(f"[{timestamp}] 'Generate Proxy' clicked\n", append=True)
country = values['country'].upper()
region = values['region']
city = values['city']
count = values['count']
# CONDITIONS
if not check_token(token):
print("Output inválido. Culpa do token que não existe. TEstando break.")
break
if check_country(country):
pass
if not region or not city:
sleep(2)
output_element.update(f"[{timestamp}] City and region are both optional fields. You don't need to define it.\n", append=True)
pass
if not count or not count.isdigit():
sleep(2)
output_element.update(f"[{timestamp}] The 'Count' field is empty. Please, provide the number of proxy that you want in your list.\n", append=True)
sleep(2)
# break
else:
sleep(2)
output_element.update(f"[{timestamp}] The Count is: {count}.\n", append=True)
try:
if values['sticky_session'] == 'Keep IP as long as possible':
session_type = 'sticky'
else:
session_type = 'custom'
protocol = 'socks5' if values['SOCKS5'] else 'HTTP'
sleep(2)
output_element.update(f"[{timestamp}] The request is: Country: {country}, Region: {region}, City: {city}, Sticky Session: {session_type}, Protocol: {protocol}, Count: {count}.\n", append=True)
sleep(2)
output_element.update(f"[{timestamp}] Sending the request... Please wait.\n", append=True)
sleep(2)
proxy_list = mlx.get_proxy(
country, region, city, session_type, protocol, count)
for proxy in proxy_list:
proxy_list_element.update(f"{proxy}\n")
except Exception as e:
sg.Print("Log window initialized...", do_not_reroute_stdout=False)
sg.Print(f"An error ocurred: {e}.\n")
sg.Print(traceback.format_exc())`
[CHECKING TOKEN]
def check_token(token):output_element = window['output']timestamp = mlx.log_time_stamp()
if not token:
output_element.update(f"[{timestamp}] You must use your username and password in order to GET TOKEN first.\n", append=True)
sleep(1)
output_element.update(f"[{timestamp}] It's not possible to proceed without a token. Cancelling the request.\n", append=True)
sleep(1)
output_element.update(f"[{timestamp}] Try again.\n", append=True)
return False
output_element.update(f"[{timestamp}] There is a valid token. Starting the request.\n", append=True)
return True
[check_country() function)]
def check_country(country):
output_element = window['output']
timestamp = mlx.log_time_stamp()
if not country:
sleep(2)
output_element.update(f"[{timestamp}] [ERROR] Please, insert a country in Country Field. It's a required field. You need to use ISO 3166-1 alpha-2 format (e.g. US, FR, BR).\n", append=True)
sleep(1)
return
if len(country) != 2 or country not in iso_country_codes:
sleep(2)
sg.Print("[ERRORRROROROR] The country inserted in the field isn't a valid country.\n", append=True) sg.Print(traceback.format_exc())
output_element.update(f"[{timestamp}] The {country} is not a valid value for country. You need to use ISO 3166-1 alpha-2 format (e.g. US, FR, BR).\n", append=True)
sleep(1)
return
sleep(2)
output_element.update(f"[{timestamp}] {country} is valid. Proceeding with the request\n", append=True) sleep(1)
return True
What I'm expecting?
When click the button "Generate Proxy", it verify if there is a valid token. If not, it stop the button to allow the user generate a token. If there is a valid token, it will check country, region, city, session, protocol and count. Session and Protocol already have default values while region and city are not mandatory. The token, country and count are mandatory (user provides it) to send in the API request to get the response (and it will be show in the multiline element "proxy_list".