Can you use graph.move but keep the axis as is?
So, I'm currently about to end a school project, where we have to make a GUI off of a CLI application (we chose ping, it's simple and you can have fun with it), so I introduced myself to Graph. I made a somewhat neat graph (Thank you tutorials, thank you cookbook, thank you GitHub demos) with x- and y-axis.
While that is nice and all, once you go past 500 pings, I've implemented a graph.move which moves it everything, giving it a nice flow.
Now, my question to you guys is like the title states: Are you able to keep the x- and y-axis 'static' whilst utilizing graph.move()? Obviously I can keep redrawing the axis, but I think it would slow down the program as well as create nasty lines where the text-values and the lines are.. I could also reset the window once I go past 500 and just add up the number of pings, going from 501-1000 in the newly drawn graph, 1001-1501 in the third etc., but is there an easier way?
Posted Dec 15, 2019, 1:55 PM UTC
A few ideas come to mind. Redrawing the entire graph isn't as bad as you might think it is, but there are possibly other ways to do it.
I have the feeling that Matplotlib, which uses tkinter, uses a draw polygon or drawing individual lines approach in their line graphs since the grid lines don't move but the lines can. Then again, they could also be redrawing the entire graph every time.
There are matplotlib examples you can see in the demo program area that also implement a moving line graph.
As I'm slowly running out of time, https://gyazo.com/67c93f7a438fa694669f9a76cdd02192 is what I'll have to go with.
It might not look perfect, but as long as it fulfills the requirements, this should suffice.
Thanks a lot for the ideas, Mike! I decided to go with the third one actually, creating a graph to the left of the main graph as well as one below, which worked out quite well!
I did have a problem yesterday as well with some RuntimeError and async handler error whilst pinging, which led me to a 12-hour dive into how threading works, how much TKinter hates it and such.. I eventually realized I had created a layout for the first window (where you input IP address/hostname), to just re-use the variable name for the second window. My program hated that for some reason, even after deleting the first window after closing it. I renamed the variables and all of a sudden it worked - I found one of your comments on another post from, like, a year ago or so, so thank you for that as well!
Now I'll just have to figure out why the Highest Ping value doesn't update..
But yeah, again, THANKS! :)
Awesome that #3 worked!
Your screenshot is fantastic! I'm impressed by the overall look you got.
One things about multithreading and using these windows and such is that you must delete the window after closing it in a multi-threading environment.
I've started adding this line of code to some examples to drive the point.
window.close(); del window
I'm not sure which error you were getting so I can't advise much more than that. You can't make any calls in a thread to PySimpleGUI code. Even with honoring this, there were problems with Python's garbage collect deleting variables from the main thread while running in the context of another thread. This causes tkinter to complain. It could be what you were seeing? Your comment mentions deleting the window though.
I'm interested in seeing the code that causes this problem if you're able to make it available.
I'll post a few Pastebin links, as I think it's the easiest way of sharing my code.
The first version of the code worked (in the state as it was) perfectly on Linux/MacOS, but always came with the error at a certain point on Windows: https://pastebin.com/E4Rz5inr (if your internet connection is debatable, you might run into ValueError in the ping function - this has been fixed later on. Code feels messy, apologies)
The second version of the code worked ~10% of the time on my Windows machine but stopped working every time on Linux/MacOS: https://pastebin.com/JhhBFsHE
This is the current version which is working on both Windows and Linux (haven't been able to test on MAC yet but assuming it is working due to Linux is..), but the current problem is that 'Highest ping:' in the window doesn't update. It shows the pre-set value of 0, but then it disappears.. I'll look into that next: https://pastebin.com/ijTgQTij
Edit: I realized just now I have no comments in my code. I really need to start working on that!
The errors I encountered for 12+ hours straight no matter what I did: https://pastebin.com/rPGrCfAJ
When running the second code on my Linux PC, it showed a similar error, but I think the line 3504 part showed first instead of the usual line 329.
Edit: I found out that the error ALWAYS occurred in the ping function (even if I ran everything outside of functions) on this line:
The matchlist was later replaced by matchClass.matchList, but there was no difference in the end.
A classmate of mine actually found out that by removing the very first 'window.read()' for the first of the two windows, the problem disappeared (well, by not loading the first window at all). That gave me something to work with, which is when I found your comment on another thread about the layout stuff... I felt happy, tired and stupid about not having tried to change names of the layouts earlier..
I've spent weeks on this very same error code.
Notice that delete is part of the error message. This happens when tkinter is asked to delete something and it detects that the deletion is happening in the wrong thread.
I'm now wondering if deleting the layout is also required, not just the window.
One problem is that I wasn't able to reproduce the problem on a consistent basis. I would change something, just like you did, and the problem would go away.
I don't understand why tkinter needs to add this error when it's due not because the user is making calling into tkinter in a thread, which is bad, but rather it's Python itself that is making the call, which is OK.
By the way, did you see the Demo_Graph_Noise.py program that shows a moving line graph? It doesn't have a scale though. I'll try adding one that is redrawn every time to see if it tears or has problems.
That demo was actually the one that inspired me to create the moving graph that I've ended up with! :) Edit: Come to think of it, several demos have actually helped me towards this - I am going to create a Readme-file to link towards your demos when handing in, as it is common courtesy between developers to do so (and we can technically fail if we don't!).
Don't worry about listing the demos. They simply show you design patterns. Other websites about tkinter do the same thing. It's how we tend to learn these packages... take something that works that's not exactly what you want, then use it to learn some techniques that you apply in your code.
I just reworked that demo so that it deletes the line on the far left when it moves the graph. This stops the lines from accumulating over time. I'm not sure what tkinter does in terms of managing large lists of lines. It didn't seem to hurt it at all to do this.
Since I'm saving all the lines now, one approach could be to move each line which will allow the axis to remain in place. I'll keep playing with it.