Get event to run when Input in empty - python

I want an event to run when I type in the input, that works.
But only when I type something, not when the field is empty, I want that too
Here's a little template code I wrote that resembles my issue
import PySimpleGUI as sg
list1 = ['a', 'b', 'c']
matches = []
out = []
layout = [
[sg.InputText('', key='-search-', change_submits=True)],
[sg.Listbox(values=out, key='-list-', size=(10,10))]
]
window = sg.Window('Search', layout, size=(300, 300))
while True:
event, values = window.read()
if values['-search-']:
SearchTerm = values['-search-']
SearchTerm.replace("['", "")
SearchTerm.replace("']", "")
print("Search Term = " + SearchTerm)
if SearchTerm == "":
out = list1
window.Element('-list-').Update(values=list1)
if SearchTerm != "":
for i in list1:
if SearchTerm in i:
if i not in matches:
matches.append(i)
outlist = matches
window.Element('-list-').Update(values=outlist)
if event == sg.WIN_CLOSED or event == 'Exit':
break

There's re lot of events in event loop, so you have to decide how to check what the event is. For example, if values['-search-']: will be failed it you close the window and values maybe None, then you will get exception TypeError: 'NoneType' object is not subscriptable.
In your event loop, you didn't check the case values['-search-']=='', but just check SearchTerm == "" under the event values['-search-']!=''.
The pattern for event loop maybe like this
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
if event == '-search-':
if values[event]:
""" Code if Input element not empty """
else:
""" Code if Input element is empty """
window.close()

Related

Error while getting the index from an empty Listbox .get_indexes()

When I want to get the index of a selected item in a listbox, and the listbox is empty i get a error.
window['Listbox'].get_indexes()[0]
------------------------------------
IndexError: tuple index out of range
The original list that I use in my program is not empty, but it's changing so it may be empty and in that case when I press on the listbox the program crashes.
Code:
import PySimpleGUI as sg
list1 = []
layout = [[sg.Listbox(list1, s=(13, 6), enable_events=True, key='Listbox')]]
window = sg.Window("listbox test 1", layout=layout, size=(100, 100))
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
if event == 'Listbox':
print(window['Listbox'].get_indexes()[0])
Is there maybe a simple fix for that?
If no, then I'd have to add a check if the listbox is empty or no.
May this work in your case?
if event == 'Listbox':
try:
print(window['Listbox'].get_indexes()[0])
except:
print("Empty list")
All that's required is an if statement.
if values['Listbox']: # if something was selected
first_entry = values['Listbox'][0]
window['Listbox'].get_indexes()[0] # if you want the index... it'll be safe because values says there are entries

My program freezes the moment my spammer made with pysimplegui, pyautogui and timer starts

when i press "submit" the program freezes can you guys please help me. I am new to pysimplegui and am trying to turn one of my practice programs into a gui.
Also could someone tell me how to make the arrows of the "spin" element functional?
If i made some stupid mistakes please do let me know, like i said I am new to this stuff so any help is really appreciated
import time
import PySimpleGUI as sg
import pyautogui as pyautogui
# img_viewer.py
layout=[
[
sg.Text("What do you want to do?"),
sg.Button("Spammer"),sg.Button("Encrypter"),sg.Button("Decrypter"),sg.Button("Password generator"),
sg.Button("Exit")
]
]
window = sg.Window("Multifunctiontool", layout)
layoutspammer=[
[
[sg.Text("Press Start to start the bot.")],
[sg.Text("Press Stop and then enter to stop the bot.")],
[sg.Button("Start"),sg.Button("Stop")]
]
]
layoutspammerwindow=[
[
[sg.Text("How many messages do you want to send?"),sg.Spin(0,key="spammer")],
[sg.Text("How long ist the cooldown? if not there just leave on 0"),sg.Spin(0,key="cooldown")],
[sg.Text("What is your message?"),sg.Input(key="message")],
[sg.Text("Do you want to leave a final message(Write down your message, if no just leave blank)?"),sg.Input(key="final_message")],
[sg.Button("submit"),sg.Button("Exit")]
]
]
layoutspammerwindow2=[
[
[sg.Text("please enter a valid parameter")],
[sg.Button("Continue"), sg.Button("Exit")]
]
]
spammerwindow= sg.Window("spammer",layoutspammer)
spammerwindow2= sg.Window("spammer",layoutspammerwindow,force_toplevel= True)
spammerwindow3= sg.Window("spammer",layoutspammerwindow2)
while True:
event,values=window.read()
if event== "Exit" or event== sg.WIN_CLOSED:
break
if event== "Spammer":
spammer_active = True
while spammer_active is True:
event, values = spammerwindow.read()
if event == "Start":
event, values = spammerwindow2.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
if event == "submit":
spammer = int(values['spammer'])
cooldown = int(values['cooldown'])
message = (values['message'])
final_message = (values['final_message'])
time.sleep(5)
for xi in range(int(spammer)):
pyautogui.typewrite(message)
time.sleep(int(cooldown))
pyautogui.press('enter')
print(xi)
pyautogui.typewrite(final_message)
pyautogui.press('enter')
break
elif event == "Stop" or event == sg.WIN_CLOSED:
spammer_active = False
else:
event, values = spammerwindow3.read()
if event == "Exit" or sg.WIN_CLOSED:
break
elif event == "Continue":
spammer_active = True
while spammer_active is True:
event, values = spammerwindow.read()
if event == "Start":
event, values = spammerwindow2.read()
if event == "submit":
spammer = int(values['spammer'])
cooldown = int(values['cooldown'])
message = (values['message'])
final_message = (values['final_message'])
time.sleep(5)
for xi in range(int(spammer)):
pyautogui.typewrite(message)
time.sleep(int(cooldown))
pyautogui.press('enter')
print(xi)
pyautogui.typewrite(final_message)
pyautogui.press('enter')
break
window.close()

Calculator in PySimpleGUI

I have a simple calculator with a fixed constant and another entry to be added to the constant. I am struggling to deal with these entries(VARIABLE) being negative ie -1234 since i cast an int() on the input so clearly this leads to problems when trying to calculate the sum.
import PySimpleGUI as sg
columns = ["FIXED","VARIABLE","RESULT"]
param = (20,3) # size of the main window
def CALCULATOR():
sg.theme('Dark Brown 1')
listing = [sg.Text(u, size = param) for u in columns]
core = [
sg.Input(f"{10}", size = param),
sg.Input(size = param,enable_events=True,key='NUMBER'),
sg.Input(size = param)]
mesh = [[x,y] for (x,y) in list(zip(listing, core))]
window = sg.Window('CALCULATOR', mesh, font='Courier 12').Finalize()
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == "SEND":
break
elif event == "NUMBER":
variable = int(values['NUMBER'])
fixed= int(values[0])
for element, value in zip(core[2:], [variable+fixed]):
element.update(value=value)
else:
print("OVER")
window.close()
CALCULATOR()
You can wrap your if-statements in a try-except statement, and then handle the updates for when there is a ValueError:
while True:
event, values = window.read()
try:
if event == sg.WINDOW_CLOSED:
break
elif event == "SEND":
break
elif event == "NUMBER":
variable = int(values['NUMBER'])
fixed = int(values[0])
core[2].update(variable+fixed)
else:
print("OVER")
except ValueError:
core[2].update("ValueError!")
The above code "catches" the ValueError that is triggered once you input something that doesn't translate well into an integer, after that you can then update the field to inform the user why the results didn't output.

Stopping Statement Execution Until true

I am trying to handle user input using pysimplegui.
If the user enters any value in the input box I need to make sure it's an integer type.
If successful, I need to replace user input value with default value for it.
I tried to use try except within a while loop. My concern here is if the user enters a string value I need to give a retry option.
However, the if else block below the try block is getting executed.
How can I make sure to give the option for the user to retry entering a value?
import PySimpleGUI as sg
sg.theme('SandyBeach')
layout = [
[sg.Text('Please enter your Phone')],
[sg.Text('Phone', size=(15, 1)), sg.InputText(key="lp1", do_not_clear=False)],
[sg.Button("e"), sg.Quit()]
]
window = sg.Window('Simple data entry window', layout)
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, "Quit"):
break
elif event == "e":
text = values['lp1']
log_timer = 1200
user_input = None
if text == '':
log_timer = 1200
else:
while True:
try:
user_input = int(text)
break
except ValueError:
sg.popup_error("Only Integer Allowed")
break
I need to stop the below block if a string value is entered.
if type(user_input) == int:
log_timer = user_input
print(log_timer)
elif log_timer == 1200:
print("no cha")
window.close()
There is a lot of noise in your code.
When you reduce it to only what you need it becomes a lot clearer.
import PySimpleGUI as sg
sg.theme('SandyBeach')
layout = [
[sg.Text('Please enter your Phone')],
[sg.Text('Phone', size=(15, 1)), sg.InputText(key="lp1", do_not_clear=False)],
[sg.Button("e"), sg.Quit()]
]
window = sg.Window('Simple data entry window', layout)
# Add some defaults for user_input and log_timer
user_input = None
log_timer = 1200
while True:
event, values = window.read()
text = values['lp1']
# break on Quit
if event in (sg.WINDOW_CLOSED, "Quit"):
break
elif event == "e" and text != '':
# Attempt to convert to int
try:
user_input = int(text)
except ValueError:
sg.popup_error("Only Integers Allowed")
# If the attempt was unsuccessful user_input will remain None
if user_input is not None:
# the only place log_timer is changed from the default
log_timer = user_input
break
print(log_timer)
window.close()
The two breaks in your code mean it will only stop running on 2 conditions.
A valid number is entered. Or the quit button was pressed.
Having one place that log_timer is reassigned means the only way it can change from the default is if a valid number is entered.
There are obviously improvements to be made once you learn functions and so on. But this is a good starting point.

how to make a savable ui in python

I have a project that allows the user to make a quiz on their own using some buttons and input
well i even want the user to be able to save their quiz in a file so they can load it in
i don't want something BIG!! a txt file will do..
i am using PySimpleGui and not Tkinter or anything..
i really don't know what i have made till now yet?(sorry i am not great with GUI)
i have a :
Main window
Editor window
And a form window
main window links the editor
editor links the form window
thanks for help in advance
if you need my code too then here
import pysimplegui as sg
layout = [
[sg.Button("Make new Form")],
[sg.Button("Open Form")]
]
window = sg.Window("Python Forms", layout)
def Form_Make():
layout_for_make_form = [
[sg.Button("Add multiple choice question")],
[sg.Button("Save Form")]
# some more items here..
]
make_form_window = sg.Window("Make a Form..", layout_for_make_form)
while True:
events, values = make_form_window.read()
if events == "Add multiple choice question":
pass # this should add a new multi choice question(working on it!)
elif events == "Save Form":
# save a form.. i am stuck on this.. :|
while True:
event,values = windows.read()
if event == "Make new Form":
Form_M()
i really don't know what it is doing yet i will have to make a new file and start from scratch :|
this will work for you :
import PySimpleGUI as sg
import os.path
layout = [
[sg.Button("Make new Form")],
[sg.Button("Open Form")],
[sg.Button("Exit")],
]
windows = sg.Window("Python Forms", layout)
questions = []
def Form_Make():
layout_for_make_form = [
[sg.Button("Add multiple choice question", key='add')],
[sg.Text('file path',size=(10,1)),sg.FileBrowse(key='filepath')],
[sg.Button("Save Form",key='save',visible=False)]
# some more items here..
]
make_form_window = sg.Window("Make a Form..", layout_for_make_form)
while True:
count = False
events, values = make_form_window.read()
if events == "add":
layout_for_question = [
[sg.Text('Must Enter all the filed for save question in file.')],
[sg.Text('Enter Question : ',size=(10,1)),sg.Input(key='question')],
[sg.Text('option1',size=(10,1)),sg.Input(key='option1')],
[sg.Text('option2',size=(10,1)),sg.Input(key='option2')],
[sg.Text('option3',size=(10,1)),sg.Input(key='option3')],
[sg.Text('option4',size=(10,1)),sg.Input(key='option4')],
[sg.Button('add')]
]
make_question_window = sg.Window('question ', layout_for_question)
while True:
events, values = make_question_window.read()
if events == None:
break
elif events == 'add' :
if values['question'] != '' and values['option1'] != '' and values['option2'] != '' and values['option3'] != '' and values['option4'] != '':
questions.append([values['question'],values['option1'],values['option2'],values['option3'],values['option4']])
print('value addded ')
count = True
if count == True:
make_form_window['save'].update(visible=True)
elif events == "save":
print(values['filepath'])
file = values['filepath']
if file != None:
f=open(file,'w+')
for x in questions:
for y in x:
f.write(y + '\n')
f.close()
print('save a form.. i am stuck on this.. :')
elif events == None:
break
while True:
event,values = windows.read()
if event == "Make new Form":
Form_Make()
elif event == 'Exit' or event == None :
break

Categories

Resources