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.
Related
im trying to build a program (using pysimplegui) which contains a few buttons (play, stop, pause, next, previous) as you can see. and when i press "next" its stopping for some time and then the same music continues (i dont press "previous" because i know that will cause problem 0 - 1 = -1).
`
global n
n = 0
media_player = vlc.MediaPlayer()
url = ['http://cast.radiogroup.com.ua:8000/avtoradio', 'http://listen.rpfm.ru:9000/premium128', 'http://cast.loungefm.com.ua/terrace128']
media_player.set_mrl(url[n])
if event == 'play':
media_player.play()
if event == 'stop':
media_player.stop()
if event == 'pause':
media_player.pause()
if event == 'next':
global n
n + 1
media_player.set_mrl(url[n])
media_player.play()
if event == 'previous':
n - 1
media_player.set_mrl(url[n])
media_player.play()
`
ive tried to google this problem
i didnt find the documentation
im trying to run it in a bit different way, using `
import vlc
playlist_url = ['http://cast.radiogroup.com.ua:8000/avtoradio', 'http://listen.rpfm.ru:9000/premium128', 'http://cast.loungefm.com.ua/terrace128']
n = int(input())
instance = vlc.Instance('--intf dummy')
player = instance.media_list_player_new()
media = instance.media_list_new(playlist_url[n])
player.set_media_list(media)
player.play()
`
but it returns
[0000027223bc32e0] filesystem stream error: cannot open file C:\Users\dadva\Desktop\Project\h (No such file or directory)
[0000027223ba3220] main input error: Your input can't be opened
[0000027223ba3220] main input error: VLC is unable to open the MRL 'file:///C:/Users/dadva/Desktop/Project/h'. Check the log for details.
Demo Code
import vlc
import PySimpleGUI as sg
playlist_url = [
'http://cast.radiogroup.com.ua:8000/avtoradio',
'http://listen.rpfm.ru:9000/premium128',
'http://cast.loungefm.com.ua/terrace128',
]
n, m = 0, len(playlist_url)
player = vlc.MediaPlayer()
player.set_mrl(playlist_url[n])
player.play()
layout = [[sg.Button('PREV'), sg.Button('NEXT')]]
window = sg.Window('VLC Player', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event in ('PREV', 'NEXT'):
n = (n+1) % m if event == 'NEXT' else (n-1) % m
player.set_mrl(playlist_url[n])
player.play()
player.stop()
window.close()
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()
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()
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.
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