I was trying to understand the Tkinter library code a little bit better, so I've been trying to place breakpoints in different places and understand the flow of code. To my surprise, the debugger does not stop at many points of the library code that I know are being executed.
I'm unclear on whether this is a spyder bug, or something funny in how Tkinter creates a loop for the GUI / interfaces with the OS.
This is the simple test-driving code that I am using:
from IPython import get_ipython
get_ipython().magic('reset -sf')
import tkinter as tk
x = tk.Pack()
I initially modified tkinter's init.py file at line 2144 to introduce a breakpoint here:
pack = configure = config = pack_configure
To my consternation, the debugger never stops here, which is very surprising to me given that commenting this line out causes an error in my test-drive code above.
I then added a simple print statement after line 2144 above,
sys.stdout.write('beep')
sys.stdout.flush()
Again, to my surprise, the 'beep' is only printed occasionally / seemingly stochastically. This is despite me clearing the workspace in the test driving script (the ipython %reset -sf command), as well as closing and re-opening anaconda/spyder.
Is there some kind of remedy to this, so that I can get more consistent behavior?
Thanks.
Related
I wrote this script in python
import pyautogui
import time
time.sleep(.1)
pyautogui.keyDown("ctrl")
pyautogui.press("a")
pyautogui.keyUp("ctrl")
pyautogui.press("c")
pyautogui.press("p")
pyautogui.press("p")
pyautogui.press("t")
pyautogui.press("enter")
pyautogui.keyDown("ctrl")
pyautogui.keyDown("shift")
pyautogui.press(",")
pyautogui.keyUp("ctrl")
pyautogui.keyUp("shift")
pyautogui.press("tab")
And I created a shortcut in ubuntu to run it. python3 Scripts/cpp.py
The script works correctly when I make focus on any text element (on the browser for example). But when I make focus on the sublime text or any other text editor It does not work.
What is the reason for this issue?
(This script makes sense and do something useful for me)
If you’re running your program as administrator, pyautogui won’t be able to interact with it. This can catch people out pretty easily because you can set certain programs to always run as administrator, so it won’t be the first thing you think of. This is the case for Windows anyway.
On Ubuntu, from your experience, it seems like it is actually important to run it as an admin. So I guess in general keep the privileges in mind when you have programs interacting with other programs.
Also, your script can be cleaned up a bit.
# Probably a good idea to have a bit of a slightly longer sleep.
time.sleep(0.3)
#pyautogui.keyDown("ctrl")
#pyautogui.press("a")
#pyautogui.keyUp("ctrl")
# Is equivalent to
pyautogui.hotkey("ctrl", "a")
# The next block looks like you're writing text. So write some text.
#pyautogui.press("c")
#pyautogui.press("p")
#pyautogui.press("p")
#pyautogui.press("t")
pyautogui.write("cppt")
pyautogui.press("enter")
#pyautogui.keyDown("ctrl")
#pyautogui.keyDown("shift")
#pyautogui.press(",")
#pyautogui.keyUp("ctrl")
#pyautogui.keyUp("shift")
# Again, use a hotkey here.
pyautogui.hotkey("ctrl", "shift", ",")
pyautogui.press("tab")
When I run a .py file, it just opens and closes immediately. It doesn't give any output. I turned it into an .exe file but still the same result. I ran it from cmd, the result is the same. I checked the program that runs the code and found it to be Python.
HERE IS THE CODE.
from tkinter import *
app = Tk()
app.geometry("250x250")
app.title("Tkinter!")
mainloop()
I suggest you to kindly share the code(as there may be some codes that does not have an output. For example, if the code simply involves assigning values to a variable(say x=2+4) will not result in printing an output, unless specifically mentioned otherwise(print(x)). Also, ensure that you add the Python's installed location to PATH(Environment Variables). I am assuming that you are using Windows OS(as you mentioned cmd).
I'm getting the error
QPixmap: It is not safe to use pixmaps outside the GUI thread
when manually entering the following statements in Seaborn in the ipython-shell using PyDev in Eclipse:
import matplotlib.pyplot as mpl
import seaborn as sns
import pandas as pd
import numpy as np
# Turn interactive mode off:
mpl.ioff()
# Create some example Data:
df = pd.DataFrame({'A':np.random.rand(20),'B':np.random.rand(20)})
# Create seaborn PairGrid instance:
pg = sns.PairGrid(df)
At this point when I continue the last statement with a dot to e.g. chain a map()-method, like this:
pg = sns.PairGrid(df).
then Eclipse is trying to show a popup of all possible completions but that popup is immediatly getting closed and the console is getting filled with the aforementioned error, 42 lines of it to be precise.
I can continue and do this without problem:
gp = sns.PairGrid(df).map(mpl.scatter)
gp.fig.show()
And I get my plot just fine.
The same happens when doing sns.JointGrid(df.A,df.B). and sns.FacetGrid(df).
While playing around earlier I also got into situations where the console was actually killed by this error, I just can't replicate the steps that lead to this anymore.
Researching on this site it looked like it has to do with threading which I'm not using at all. Does Seaborn use it?
I want to create my plots by first creating a Grid/Figure and doing the plotting later, but this error suggests that this isn't a safe way to do things though the Seaborn doc says it's fine to do it like that:
https://seaborn.github.io/generated/seaborn.FacetGrid.html
EDIT:
When doing the same thing in Spyder I'm not getting the error but this warning when doing gp.fig.show():
C:\Anaconda2\lib\site-packages\matplotlib\figure.py:397: UserWarning:
matplotlib is currently using a non-GUI backend, so cannot show the figure
"matplotlib is currently using a non-GUI backend, "
When interactive mode is off I'm not seeing any graphic. With interactive mode on I'm still seeing the warning but get the graphic inline.
No popup in either case though. In Eclipse I'm getting both the error and the popup.
EDIT 2:
Running the whole thing as a script in Eclipse does not produce any error, only the manual entering like described above does.
I took a look at https://github.com/fabioz/Pydev/blob/master/plugins/org.python.pydev/pysrc/pydevconsole.py and the issue is that the code-completion on PyDev is being triggered in a secondary thread, not in the main (UI) thread.
I.e.: the code completion in the interactive console is not expecting that it'll touch code that'll actually interact with the gui.
For this to work, the completion command has to be queued for the main thread (as the regular commands are queued) and the thread has to wait for it to finish to then return its value.
Please report this as an issue in the PyDev tracker: https://www.brainwy.com/tracker/PyDev/ (i.e.: code-completion in the interactive console should happen in the UI thread).
I'm using the current python-mode to edit my source file in top window and a python inferior shell in bottom window to see the outputs (using C-c C-c from the source file, the cursor stays in the top, source file window).
Is there a way to navigate the traceback errors while still staying in the source code window?
Also - is there a similar way to navigate errors just in the file that was actually sent (i.e. not those errors coming from called external files)?
At the moment I either do M-g M-g to jump to line number, or switch to the python shell window and navigate there to the error I want to have a look at.
This would be a tremendous efficiency boost!
Thank you very much!
It turns out there are the functions [next/previous]-error bound M-g M-n and M-g M-p that do this (for some reason unmentioned in the python mode descriptions...). I'll keep the question open if somebody has a good solution to the second part - cycle only through the errors in the current file.
realgud that I wrote has a command (and mode) for shells called realgud-track-mode. When you invoke that, the debugger you are using will be prompted. For example it could be pdb (but I prefer trepan3k since I wrote that too)
At any rate once all of this is set up, then the command realgud:goto-lang-backtrace-line which is bound to C-c ! ! will parse the line in the shell window and bring the file at the location mentioned up in another buffer.
when I try make a picture with pylab
import pylab
pylab.plot([0,1,2,3],[10,50,30,40])
pylab.savefig('graph.png')
in a wx application, no returned the terminal control.
I imagine that you are calling the wx main-loop in your main (only?) thread. However can you clarify the context of your code snippet? How do you make it into a "wx application" and why do you want terminal control? For example, do you want to see your figure on screen while also entering stuff at the terminal. Or do you want to just save it, without it showing on screen?
Anyway, the code snippet you give runs just fine for me, but if I do
import pylab
pylab.plot([0,1,2,3],[10,50,30,40])
pylab.savefig('graph.png')
pylab.plot()
Then if I run your code ...
as "python snippet.py", then it just writes the file and exits, fine.
from "ipython -wthreads", it shows the plot, saves it and returns control to me.
from "ipython", then it does it's thing and never returns control.
This is because pylab.plot() calls the main loop of a GUI (gtk in my case, I expect wx will do the same).