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
Related
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.
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.
I've been trying to use the pyautogui library but when i try to use the move() the python launcher bounces on the dock then disappears and nothing happens, i don't even get any errors, I saw a post from a while back when catalina was around and the person who made the post ended up going back to an older OS X because they couldn't figure out a solution, i also found the problem on the github repo for pyautogui but the solution which was to add your terminal or python launcher or code editor to accessibility list in privacy didn't work for me. Below i'm providing my code, the github issue link and the other person's post, by the way i've done everything they said they've done. I'd really appreciate some help, thanks.
Is anyone else having PyAutoGUI issues after updating to Catalina?
pyautogui.click() almost seems to work. Clicks work in the window that
I tell it to, but it's as if immediately after a click, the window
focus returns to VS Code or Terminal. And then any
pyautogui.typewrite, keyup, keydown, any of those keystroke commands
are entered into VS Code or Terminal rather than the window I've had
it click on to focus. Doing a command + tab doesn't do the trick
either as those keys are entered into Terminal rather than acting as
hotkeys on OSX. I can't seem to keep the focus on the window I'm
trying to operate on. I've tried reverting from zsh back to bash. I've
tried adding Terminal, zsh, bash, and VS Code in the Accessibility
area of Security & Settings to give full keyboard and mouse control.
Apple does say that their depreciating Quartz (what pyautogui is built
off of to operate OSX) in 10.15 (realized this one too little, too
late). But that's supposed to mean it still works they just won't be
updating it, right? Anyone else experiencing this? I'm hoping for
someone smarter than me who can guide me to a fix. Thanks!
import os, sys, subprocess
import pyautogui
import time
def open_file(filename):
if sys.platform == "win32":
os.startfile(filename)
else:
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.call([opener, filename])
def runZoom():
open_file("/Applications/zoom.us.app")
joinbtn=pyautogui.locateCenterOnScreen("Join_Meeting.png", confidence=.5)
pyautogui.moveTo(joinbtn)
pyautogui.click()
runZoom()
Github issue link:https://github.com/asweigart/pyautogui/issues/247
Try to use PyDirectInput module.
pip install pydirectinput
I was having trouble until I enabled access to accessibility features, thanks!
I also ran into this issue on my mac, you have to manually add Python Launcher to the list of apps that can control your computer.
System preferences --> Security & Privacy --> Accessibilty --> +Python Launcher
i also added terminal to the list.
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.
When using Ubuntu, I am able to play with Tk in real-time, by writing commands in the IDLE.
On Windows, I need to call root.mainloop() to spawn a window, and the loop keeps me from running any code from IDLE.
Is it a matter of platform, or am I doing something wrong?
I don't have a complete solution or answer, but here is something that I found on the subject:
From this page: Thinking in Tkinter
Note that you should not run these programs under IDLE. IDLE is itself a Tkinter application, with its own "mainloop" that will conflict with the mainloop in these programs. If you really want to view and run these programs using IDLE, then -- for each program -- you should comment out the "mainloop" statement in the program before running it.
I'm not too sure about the technical reasons behind it -- but I just don't run Tkinter code from IDLE.