python code runs on idle but doesn't run on spyder - python

from tkinter import Tk, Frame
from pandastable import Table , TableModel
df = TableModel.getSampleData()
f = Frame(Tk())
f.pack(fill='both',expand=1)
Table(f, dataframe=df, showstatusbar=True).show()
This code will display dataframe in new window. It works on idle, idlex. But doesn't work on spyder ide. It doesn't give any error either. Please help me if it is possible to run it on spyder, ipython notebook.
(spyder have an in built feature to open dataframe in new window, I am just curious to know why this code is not working)
I am using anaconda python 3.5, windows 10

You need to run your code in Spyder's IPython console, but before doing it, you need to run this command:
In [1]: %gui tk
After that, you should be able to see your dataframe displayed as expected.
Note: The previous command activates the Tk event loop, which is necessary to be able to display widgets/applications based on Tk.

Related

Spyder IDE with python 3.10 seems freezing when click run button, but it works fine if run a single line beforehand running the entire script

I have truble with last version of Spyder 5.4.0 with last version of Python 3.10.6.
Spyder version: 5.4.0 (conda)
Python version: 3.10.6 64-bit
Qt version: 5.15.2
PyQt5 version: 5.15.7
Operating System: Windows 10
Even if running a script like
print('Hello world')
when I click on the play green button, the IPython console seems freezing on this script, and it does not run for hours.
If I select this line of code and I run current selection or run the current cell it works fine. From this moment, it seems that spyder works fine, until at a certain point when I run it seems again freezing. I have to restart a new console and before running the script I have to run a single line or a single cell.
It seems that I have to 'activate' in someway the Python console in order to Spyder will run the script.
Does anyone have the same issue? How can I solve it?
I have tried to uninstall and reinstall both spyder and python many times, but it is useless.
After many trials, I noticed that there is something strange with IPython console. I noticed that when it hangs after running a code, if I delete all user variables, it worked fine.
Then I tryed to delete all variables before execution, and it work fine.
Therefore I discovered that a solution that worked for me is to go to preferences -> Run -> and untick the option 'Remove all variables before execution'
It is quite annoying because I have to do it manually every time before running, but in this way the spyder does not appear to hang anymore! I hope that the Spyder developers will solve it soon.
-
I automatically solved by typing at the beginning of any script these lines, inspired from the question Code to clear console and variables in Spyder :
try:
from IPython import get_ipython
get_ipython().magic('clear')
get_ipython().magic('reset -f')
import matplotlib.pyplot as plt
plt.close('all')
except:
pass
similar to Matlab in which you normally start your code with
clc
close all
clear all

Python 3.8.9 MacOS M1 tkinter UI not opening inside of VSCode

so I am trying to use Tkinter inside of VSCode on my M1 mac. When I use Tkinter through IDLE it works fine but once I try to open it inside of VSCode it the UI submenu just stalls. I'm using python 3.8.9 as my kernel but it won't seem to open even when I switch to any other versions. Tkinter is installed fine it is just some issue with how VSCode is running it but I have no idea how to fix it.
import tkinter as tk
window = tk.Tk()
Here I provided some screenshots of what happens
While using Idle
While using VSCode (The app just bounces up and down never opening the UI)
You have not started the event loop. IDLE does mainloop for you, otherwise, you need to call it by yourself.
import tkinter as tk
root = tk.Tk()
root.mainloop()
You can refer to here.

Empty Lines Printing After Using a GUI in Python

The following is something that began happening within the past three (3) months (written March 10, 2021):
Whenever I run a GUI, either using Qt or tkinter in Python, the interpreter window from which I executed the function will begin sending line feeds to the console and will not start until I quit() the interpreter.
1. Code Examples to Reproduce Problem
1a. Qt
from PySide2.QtWidgets import QApplication, QFileDialog
if QApplication.instance():
app = QApplication.instance()
else:
app = QApplication([])
filename = QFileDialog.getOpenFileName()
...OR...
1b. Tkinter
from tkinter import Tk
from tkinter.filedialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
...OR... any usage of tkinter or Qt, then the console in which I am working will continue to print line feeds every 30 seconds (I timed it).
The resultant appearance of the console is something like this:
In[3]: filename = QFileDialog.getOpenFileName()
In[4]:
prin
t("Hello wor
ld")
In[4]: print("Hello world")
Hello world
Due to this, I would say that perhaps, "line feed", isn't the best description, as the interpreter is not reading any '\n' characters
2. Environment Info
I am currently running MS Windows 10 Enterprise (for work)
Python version: 3.9.2
I am pretty sure it was happening at least as soon as a installed 3.9.0, but not sure
I can reproduce this while running from a DOS command prompt (i.e. python run_gui_test.py) and as long as the GUI is open, it will print the line feeds
I can reproduce this from Anaconda virtual environment (conda 4.9.1 with python 3.7.9)
I can reproduce this running from a normal Python interpreter
I can reproduce this running from an iPython interpreter (7.20.0)
Also worth mentioning, I currently suppress the Qt warning related to screen ratios
This was happening before I suppressed the warnings and afterwards.
3. One Exception
It does not occur from within the Spyder iPython console (iPython 7.19.0, python 3.7.9)
Seeing how I do most of my debugging in an iPython console, it gets really annoying after a while.

VSCode Jupyter: is there a way to make python interactive window work as default console?

I've recently switched to VSCode, and am wondering if there's a way to make the Python Interactive Window from the Jupyter support in VSCode work like the console in Spyder where I just have to select code and press ctrl+enter to send it, without having to create cells everytime.
For now I'm resigned to work with the Terminal until my code is clean and then create a cell when I have reusable code, and would like to just work directly with the PIW.
You can always change the default console setting by:
Opening the Command Palette (⇧⌘P)
Typing "Preferences:Open Settings (JSON)
Edit this line:
"python.dataScience.sendSelectionToInteractiveWindow": false
You should be able to do this with the latest python extension. The select the code you want to execute and press shift-enter. Is that not working?
For me (now) interactive mode runs after setting "jupyter.sendSelectionToInteractiveWindow": true
#FranciscoRZ. You should have seen a popup option for this, but if it didn't come up for you it can just be manually set in VSCode options. Just change this guy here:
Python->Data Science: Send Selection To Interactive Window
That should get you what you are looking for.
OP's Note: Accepting this answer because it will be the right anwser starting with the February release of VS Code Python
If you have the notebook saved as a python percentage script (which is more git friendly)
each "cell" will be delimited by # %% and the default run command is "Shift+Ctrl".
Later once you are working in the interactive window, If you want a particular cell you wrote on the fly to be in you script, there is one button which says "Paste code into file" right next to the recently executed cell in the interactive window.
And in case you are using the notebook for the sake of being able to later export it to html or pdf, once executed in the interactive window, there is an export button as well.

TKinter (filedialog.askdirectory) freezing Spyder console

I noticed in other questions that there are (or were) several problems with TKinter in Spyder. I have been using it in IDLE for a while but I am moving to Spyder, and came upon some problems.
I am running Python 3.6.4 with Spyder 3.2.8 from Anaconda 1.8.4, on Windows 7 Enterprise.
When I try to use some TKinter functions (like filedialog.askdirectory) Spyder´s console freeze.
I´ve been reading different forums but still no one has the same problem or a solution to this problem.
Here is a simple code that would work in IDLE, but not in SPYDER:
import os
from tkinter import Tk, filedialog
Tk().withdraw()
print("Done WITHDRAW")
currentdir= os.getcwd()
print("Done GETCWD")
filename= filedialog.askdirectory(title="Select folder", initialdir=currentdir)
print("Done ASKDIRECTORY")
As a result, I get:
runfile('M:/Users/KPK2/.../hello.py', wdir='M:/Users/KPK2/...')
Done WITHDRAW
Done GETCWD
And the console keeps running, waiting for the ASKDIRECTORY to pop a new window to select a file. In IDLE it works just fine.
Does anyone know which could be the problem and some possible solution?
I read on other threads solutions like updating to Spyder 3.0 (I already have 3.2.8) or changing some "External Modules" for the "Console" in Preferences, but there is not such a tab on my Prefereneces window (don´t know how to do that otherwise).
Thank you.
try running this command %gui tk at the console before running your code.
The alternative is it go to Tools > Preferences > IPython Console > Graphics > Graphics backend and select tkinter there.
This worked for me :)
The answer was found here https://groups.google.com/forum/#!topic/spyderlib/rFJhJZgjZTE

Categories

Resources