Cannot import Tkinter on visual studio code? - python-3.x

Ok, I am having a weird issue going on. From what I understand Tkinter is supposed to be built in with python 2 and 3.
I can import and use Tkinter just fine in my terminal under python3 as well as with IDLE3. However, when I try to import Tkinter in Visual Studio Code I get an "ImportError: No module named 'tkinter'.
The same issue was happening in Pycharm also but I had my interpreter set to the same as my terminal and not a project-specific interpreter.
I have tried the following:
import tkinter
from tkinter import *
try:
import tkinter
except ImportError:
import Tkinter as tkinter #Even though I'm using python3
None of this is working, Any idea why?
Also I am using Linux Mint.

I've been stuck with the same issue for weeks now where my VSC says there are no tkinter module but in shell and terminal it runs fine.
Solution: I found out that in the left buttom corner there is an interpreter that runs as Python 2 just click and select Python 3

In windows you could have selected to not install the needed tkinter components when python was installed (its optional in the installer).
Try running the installer again, and make sure these components are selected.

If your interpreter is PHP2 it is spelled "Tkinter" in the code. (uppercase)
If your interpreter is PHP3 then you should spell it like "tkinter"

When you were installing python there is a option. Install tkinter and ide just check it and then reinstall python after that close and open vs code. Here you go!

Related

Why does Tkinter work in the terminal but not in Pycharm?

Using Pycharm on Linux mint.
I installed the "future" package for the python interpreter which I'm using. Heres the script.
from tkinter import *
top = Tk()
top.mainloop()
Didn't work. It returns "ModuleNotFoundError: No module named 'tkinter'". Tkinter is infact installed. "python3 -m tkinter" confirms it. And when I compile the same code in the terminal, it displays.
As Bryan says, you're probably not using the Python version you think you're using. PyCharm tends to install its own version of Python. Once you have more than one version of Python installed, things get trickier.
To see what's happening, try running this script:
import sys
print(sys.executable, sys.version)
Or run those similar commands from the command line. That should help clarify matters.
The sys.executable will show you the full path to your Python executable. Great for seeing where the used Python installation is located.
I don't use Python on Linux, but perhaps one of your Python installations is version 2, in which case you would need to use:
from Tkinter import *
which is another way to confirm that the Python is version 2 rather than 3. If this is the case, you'll want to move to Python 3. I don't think anyone writes new projects in Python 2 anymore. It's defunct, purely legacy.
It's also possible that Python is installed on Linux without Tkinter. There are other posts on how to install Tkinter on Linux. For instance, you can check out ImportError: No module named 'Tkinter'
Thanks guys for the help I really appreciate it. But I found out the problem was because of Linux Mint's Software Manager. I initially downloaded pycharm using said software manager but it didnt work which is why I created the post. Then I deleted it, and downloaded pycharm through the tar.gz file from the jetbrains website. After doing that, it seems to work.

ImportError: No module named tkinter. Mac OS Terminal

I have created a Tkinter window in a python script. The script runs perfectly in the python IDLE and generates the GUI. However, when I run this file outside of the IDLE by clicking on it on my Desktop, the terminal responds with ImportError: No module named tkinter. Even though tkinter is installed as its a default module when python is installed. Any help is much appreciated. Im working on python 3.7
Make sure your default python , the one that the terminal calls has tkinter installed.
So run 'python --version' in the terminal and if that yields 3.7 then your default python is 3.7.
Now try to import tkinter using that same python version.
open the terminal and run python then run import tkinter as tk
If that does not yield an error saying that the module was not found then most probably you are running the executable with the wrong permissions.

How do I link PyCharm to PyQt4?

I use PyCharm 2016.2.3 (32-bit), Python 2.7.12 (32-bit), PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32, Windows 7 Home Premium.
I would be grateful for the procedure to link PyCharm to PyQT4. I'm not a techie.
I linked to the Python interpreter using File > Settings > Project Interpreter, by simply pointing to "python.exe"
For PyQt4, I can see the folder in
C:\Python27\Lib\site-packages\PyQt4
But which file do I actually "point to"?
Thanks.
Here's how I solved this for Python 2.7.12 and Python 3.4.4:
File > Settings > Project > Project Interpreter > <Python version>
Double-click setup tools
In Search, type PyQt4
In left pane, don't select PyQT4. This will give the error
Could not find a version that satisfies the requirement PyQt4
Instead, select PyQt4_windows_whl. For me, this was successful.
Test in editor by typing:
from PyQt4 import QtGui
As soon as I typed
from P
PyQt4 appeared in the dropdown.
As soon as I typed
from PyQt4 import Q
QtGui appeared in the dropdown.
I'm no expert, so I don't know if these tests are conclusive.

compling python 3.5 program with pyinstall - missing tkinter

I am trying to compile a python 3.5 program, which uses tkinter as a GUI. To do that I am using pyinstall, but I run into a problem during compliation process I get warning messages" tkinter not found" and the program does not work afterwards (as a dist version). It seems pyinstaller is looking for tkinter.py but from what I undersant python 3.x uses __init__py. How should I proceed compiling this program? I have ran through the documentation on pyinstaller page, but it wasn't helpful, or I missed something...
enter image description here
You probably need the hidden import feature when compiling. Add the following as an option when compiling your script:
--hidden-import tkinter

Hide console window with Tkinter and cx_Freeze

I am using cx_freeze to freeze a tkinter app. When I run the exe I get a wonderfully USELESS console window along with my tkinter GUI.
I would like to remove/hide this useless black window.
I've seen threads that suggest the following:
root = tkinter.Tk()
root.withdraw()
The above code does the opposite of what I want. It hides my GUI, while the useless black window remains. I would like it to be the other way around.
I remember reading somewhere that on Windows if you specify your file extension as .pyw, it will launch with pythonw.exe (without a console window). Does that work for you?
This question is very similar, but for wxPython and cx_Freeze. Fortunately, it turns out that the appearance of the console can be configured from the build script, rather than source code. Borrowing from the top two answers, the trick is setting the base variable in your cx_Freeze build script:
import sys
from cx_Freeze import setup, Executable
base = None
if (sys.platform == "win32"):
base = "Win32GUI" # Tells the build script to hide the console.
# <The rest of your build script goes here.>
Here is the relevant documentation (although it does not explicitly mention that base controls the console option).
Also, just because it's interesting, an answer to a different question solves the issue of creating a GUI app with or without a console mode option, which I thought was very cool.
Do exactly just like gary said, then:
setup(name="ur package name",
version="ur package version",
description="as above",
executables=[Executable("ur_script.py", base=base)]
This will work cx_Freeze
If using pyinstaller use pyinstaller-gui.py
In Windows command line type
python pyinstaller-gui.py
This will first say "Please use just 'pyinstaller.py'. Gui is not maintained." Change the code l'il bit and you will be able to run this.
It will show pop up a window to select your script and some checkboxex. Check on 'no console(windows only)
That's it. You are done!
Another option: use --noconsole option while building. i.e:
python pyinstaller.py --noconsole yourscript.py
I had the same problem today
What i was using to compile my python programs was py2exe and the fix was very simple modify the setup file as shown below. My interface is written with Tkinter
modify the "setup.py" py2exe script from:
Old Python Code:
from distutils.core import setup
import py2exe
setup(console=['app.py'])
New Python Code:
from distutils.core import setup
import py2exe
setup(windows=['app.py'])
After i did this and reran my setup script the application loaded and did not show the console window. The only thing with this is if you have your application sending print commands to the console window you will not see theme. I hope this helps.
For me using the option --base-name Win32GUI works. Here is an example:
cxfreeze your_python_file.py --base-name Win32GUI --target-dir your_target_dir
I'm assuming by "black window" you are referring to the terminal window. In order to disable this from popping up, save your file as a .pyw extension instead of .py

Resources