Comments (17)
I have already captured the error I had, but this chat was very useful, because I hadn't thought about putting the whole event in the try box (but why sometimes simple things don't come to mind?) and then because I had missed the correct way to display the error in PSG, which you told me indicated in the example link. Thank you very much, and good work!
from pysimplegui.
Folowing code demo another way for it -- Add special code, then no try...except
required.
No more test, so not sure if anything wrong.
import traceback, sys
import PySimpleGUI as sg
def handle_exception(exc_type, exc_value, exc_traceback):
tb = traceback.TracebackException.from_exception(exc_value)
message = ''.join(tb.format())
sg.popup(message, title="Traceback", button_justification="right")
sys.excepthook = handle_exception
layout = [[sg.Text("Hello World")]]
window = sg.Window("Test", layout)
window = sg.Window("Test", layout) # Reuse the layout to test PSG alarm
1/0 # Normal exception
from pysimplegui.
One way I do this is by putting a try around the event loop, or around a piece of code that may be having trouble. This Demo Program shows one way to go about it:
from pysimplegui.
Ah, thank you very much, I hadn't thought about trying the whole event loop! Of course it is strange that among the properties of the shortcut at Windows level there is no option to avoid closing it in case of errors. Anyway, thanks so much for your help, I'll try as you suggested.
from pysimplegui.
The "closure problem" will happen regardless of how you launched your application. Because the program exits, all windows that are currently open will close. The purpose of the try is to delay the program from exiting so an additional window can be shown. Of course, this assumes that you can still open a new window. If something has gone really badly and nothing can be displayed at all, then you can write to a log file (one solution among many).
from pysimplegui.
When I have this type of problem I launch the window from inside an already open command prompt window, at least this way I can see the error... oh yes or otherwise use the logs... ok thanks for the help.
from pysimplegui.
This could mean that it's related to the "Working Directory" or "Current Directory" that you are running inside in your command line prompt. That's one difference that will be present between a double-clicked .py/.pyw file versus manually launched. See if you can capture with a try statement why you're crashing.
from pysimplegui.
was a banal string + None concatenation as string ... they came from database fields ... :-/
from pysimplegui.
Thank you so much for the "thank you"!
It means a lot knowing we're making a difference. I appreciate you taking the time to provide some details. It's really helpful to see/understand problems.
I wrote something about errors a long time ago that is in the Cookbook that may be helpful too:
https://docs.pysimplegui.com/en/latest/cookbook/original/exception_handling/
from pysimplegui.
Thanks for the additional link! I will study it carefully. I have always taken the protection of possible errors lightly (I confess ... but I have the mitigating circumstance that with Python and PSG things go so well that in the end there are few "unforeseen" errors ...) and I simply go to print to understand something about it ... I've never been a phenomenon with try except ... but I'll definitely have to improve, I realize that, otherwise it happens like yesterday, that I go to click a button and before I can do it everything disappears (and between I fall off my chair a bit) :-/
from pysimplegui.
Sounds like you're doing great to me!
I like this software development mantra:
Make it run
Make it right
Make it fast
We're all learning as we go when writing software. I like your "unforeseen errors" description. That's exactly what they were too.... unforeseen.... and now, they've become "seen", and you're learning ways of dealing with them.
I don't see anything you've written as unusual or taking a development path that's "wrong". You're writing software like the rest of us... learning and adding error checking to "make it right". Keep doing what you're doing
from pysimplegui.
Excellent suggestion! I'll look at ways to get this built into PySimpleGUI itself so that it's easy to "enable" making a popup with traceback call using this mechanism. Love it! This is good stuff.
from pysimplegui.
Thank you very much Jason, your suggestion seems to work perfectly for me! i will try in more strutured program where i have a lot of single windows launched from unique menu. I could put this error handling via sys. in just one point of the code and find it active everywhere for all the individual programs I launch ... regardless of the window launched (without changing them) ... but I'll do some tests (then I'll let you know).
from pysimplegui.
since I develop ERP management software, I have many windows that I launch, and this one above would be the best, because I wouldn't have to modify them one by one
from pysimplegui.
Jason's suggestion is great, because compact, and I will use it. But now I would like to delve deeper into the matter. The case that is close to my heart is this: from a menu I launch various independent PSG programs. In each of these an error can obviously occur, and until yesterday - since I didn't manage exceptions - everything disappeared and I didn't see what was happening. Now, with the Jason method, I finally see the error and can take action (video 1). And it's already more than excellent! However after the error, the window goes away. So I went back to the method suggested first but put it internally to the event loop, so that in case of an error, the event loop doesn't break. In fact now I have the whole story of the error, but I close this one, the window continues to live and nothing closes. This is the result I wanted to obtain, I wanted to share it, for any other suggestions you may have. However in this method, I have to insert try except in all programs. I'm totally fine with it, it's really not a lot of effort and I finally have programs that don't break. Would it be possible to achieve the same thing with just the application checkpoint? I'm afraid not, because I believe that not breaking the cycle of events is something at the individual window level, but I ask you to know what you think. I repeat that I didn't have too much need, because python+psg are excellent for developing with few errors, however as the complexity of the window increased, it became important for me and I had to understand how to solve it.
Video 1:
https://www.youtube.com/watch?v=n_mVwxZPx8Y
Video 2:
https://www.youtube.com/watch?v=0w6sRTewy5A
while sw_contina_lettura_eventi:
event, values = window.read()
print(event)
try:
..... a lot of event checks ....
..... a lot of event checks ....
..... a lot of event checks ....
except Exception as e:
tb = traceback.TracebackException.from_exception(e)
message = '\n'.join(tb.format())
sg.Print(message, keep_on_top=True)
self.window.close()
p.s. the Jason tb = traceback.TracebackException.from_exception(e) message = '\n'.join(tb.format())
is very very better clear than the other one in the first link example because is a complete error display point by point
from pysimplegui.
So the best for me is try except in event loop + jason way to display the complete error (was the video2). Further suggestions? improvements? Thank you very much!
from pysimplegui.
the best for me
Put on the spot on this one
I like to claim, "there is no best"... but that only applies to general questions. There may be a best in this detailed of a situation. I like the idea of localized error handling in and around the event loop.
One of my Weather Gadgets, that looks like this:
has an event loop that looks like this:
window = create_window(win_location, settings)
while True: # Event Loop
event, values = window.read(timeout=refresh_in_milliseconds)
if event in (None, '-QUIT-', 'Exit', sg.WIN_CLOSE_ATTEMPTED_EVENT):
sg.user_settings_set_entry('-win location-', window.current_location()) # The line of code to save the position before exiting
break
try:
if event == '-CHANGE-':
x, y = window.current_location()
settings = change_settings(settings, (x + 200, y+50))
window.close()
window = create_window(win_location, settings)
elif event == '-REFRESH-':
sg.popup_quick_message('Refreshing...', keep_on_top=True, background_color='red', text_color='white',
auto_close_duration=3, non_blocking=False, location=(window.current_location()[0]+window.size[0]//2-30, window.current_location()[1]+window.size[1]//2-10))
elif event == 'Edit Me':
sg.execute_editor(__file__)
elif event == 'Versions':
sg.main_get_debug_data()
elif event != sg.TIMEOUT_KEY:
sg.Print('Unknown event received\nEvent & values:\n', event, values, location=win_location)
update_weather()
update_metrics(window)
except Exception as e:
sg.Print('*** GOT Exception in event loop ***', c='white on red', location=window.current_location(), keep_on_top=True)
sg.Print('File = ', __file__, f'Window title: {window.Title}')
sg.Print('Exception = ', e, wait=False)
window.close()
Rather than use a popup that blocks, I print to the Debug Window and keep going. The wait parm is on there so that if I use the same code around the entire main function/event loop, it'll keep the program from exiting if I set wait=True
. Most programs I use the popup with traceback so that I can click the button and have it open PyCharm to that line of code that had the error (I'm quite lazy).
from pysimplegui.
Related Issues (20)
- [Question] Demo_Matplotlib_Ping_Graph_Large.py does not run/compile on Pi4 HOT 26
- Flask click SystemError when used with pySimpleGUI output redirect HOT 1
- [Question] How to trap an event HOT 2
- [Enhancement] CI friendly handling for the first time installation dialog HOT 3
- [Question] How to update class parameter within inherited class HOT 4
- [Question] How To Re-Enter Data For A Specific Field - AND 'CR to act like TAB HOT 1
- [ Bug] sg.ColorChooserButton Unless Additional Tkinter Import HOT 1
- [Bug] Hitting the image-count limit [Fail to create pixmap with Tk_GetPixmap in TkImgPhotoInstanceSetSize] HOT 6
- [Question] Import error HOT 3
- [Question] Change focus highlight when tabbing to button HOT 8
- [ Enhancement/Bug/Question] NOTE - you can also call sg.main() or sg.main_open_github_issue() to post an issue HOT 1
- [ Question] Compiling Errors since License validation HOT 2
- [ Bug] Broken Doc Link HOT 2
- [Question] NOTE - Table get doesn't work HOT 6
- [ Enhancement] allow arrowheads for lines in tkinter graphs HOT 8
- [Question] How to Use PySimpleGui Programs on Other PCs HOT 1
- [ Question] How to detect when we manually enter a value in combo box in Pysimple GUI HOT 1
- [Bug] Table colored rows flashes when updating in 5.0, while they didn't in 4.x HOT 10
- [Question] No right_click_menu in Combo HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pysimplegui.