How do I fix Tkinter in Python-3.7? - python

I've tried to create a window with Tkinter but it does not work...
I am using MacOS with Python 3.7.3 in VSCode.
Maybe you can help me by this:
from tkinter import *
root = tk.Tk()
root.mainloop()
I tried many of these code versions but every time I run any of these programms he tells me:
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I'd really like to use Tkinter but I don't now what to do...
I hope you can help me!
Thank's

I recommend re installation of python from official site.

Related

Tkinter install not working after pip install tk

I need Tkinter to use in one of my projects as a button etc.
i pip installed Tkinter with the code below as i followed a tutorial online and it said to
pip install tk
is this not correct as when I import Tkinter import Tkinter. it says module not found?
I have tried to restart my project and have tried every possible pip install combination ever.
Please help explain Tia.
If you are using Python 3, Tkinter is built in. But you have to use a lowercase letter. Don't use "Tkinter", use "tkinter" instead.
Also, don't use "import tkinter", use "from tkinter import *", to import everything.
Example:
from tkinter import *
def command():
print("Hello, world!")
root = Tk()
root.title("tkWindow")
btn = Button(root, text="Click Me!", command=command)
btn.pack()
root.mainloop()
Also, remember that when you import everything from Tkinter (using from tkinter import *), you don't use tk.Tk() or tk.Button(), you just have to use Tk() and Button().
This should work for you. Good luck, and happy coding!
For me, it depended on what version of python I was using. When I was trying to do turtle graphics for the first time, my Tkinter module wasn't importing. After from tkinter import Tk, it worked. When we moved to python 3.8.5, it clearly had tkinter built into python 3 already, so try pip installing (just in case), then try from tkinter import Tk again. If it isnt working, check to make sure that you're on the latest version of python!

Can Tkinter "uninstall" itself?

I was working on an app with Tkinter and everything was working fine, but after modifying a function (that has nothing to do with the Tkinter environment) all of a sudden I started getting the following error:
from tkinter import *
ImportError: No module named tkinter
I tried opening another Tkinter file to see if it had something to do with my app, but on the other file, I'm also seeing the same error. So I'm guessing it doesn't have to do with my app. Is there any way that Tkinter uninstalled itself if it was working before? I'm also getting the same error when importing Pandas. It's weird because it was all working before.
Has anyone encountered this problem before? What could be the problem?
No python module can uninstall itself. You can type pip install tkinter again in cmd.

No module named 'Tkinter' in Jupyter Notebook.What's the reason behind this?

import Tkinter as tk
Error : ModuleNotFoundError: No module named 'Tkinter'
For python2 it is Tkinter
For python3 it is tkinter
jupyter notebook is mostly python3 so you may want to try
from tkinter import *
this question is similar to this question
Difference between TKinter and tkinter
As others have stated if you are using python 3 the module name has changed to tkinter from Tkinter, so using import tkinter should solve your problem
from Tkinter import *
win=Tk()
win.title('My First GUI')
win.mainloop()

Why does it say that no module named tkinter?

Good day.
I installed python 2 and python 3 in my laptop. And i'm using python 3 interpreter in writing my codes. Here is my code.
#! /usr/bin/python3
from tkinter import *
root = Tk()
theLabel = Label(root, text ="This is too easy")
theLabel.pack()
root.mainloop()
But when I double clicked the save file icon. It will say no module name tkinter. Can some one help me please?
python 2 and python 3 use tkinter in a different way.
Note: Tkinter has been renamed to tkinter in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.
The above lines are from python documentation. Not sure if python is loading tkinter using python 2 or python 3..May be internal PYTHONPATH is
messed up
Rather try this,
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
Note: In these situations where you use multiple versions of same modules, try using virualenv
Virtual Env
you need to check the module name or package name before using it, do this
from Tkinter import *

TTK theme not working, just old-fashioned tkinter

On my computer with Win8.1 when I use following code
from tkinter import *
from tkinter.ttk import *
Tk().mainloop()
I see only the old looking tkinter. Any idea how to fix it?
I tried to use different theme with no result.
For some reason following code:
from tkinter import *
from tkinter.ttk import *
Style().theme_use("alt")
Tk().mainloop()
results with two identical tkinter old-fashioned windows which seems not to be correct.
PS: It seems to be issue related with Win8.1, see here:
Python 2.7 - ttk module seemingly not working in Windows 8.1
But I found no information how to fix it. Any news would be welcome. :)
EDIT1:
After trying following snippet:
from tkinter import *
from tkinter.ttk import *
from functools import partial
root = Tk()
style = Style(root)
def change(name, style):
style.theme_use(name)
for s in style.theme_names():
lb = Button(root, text=s, command=partial(change, s, style))
lb.pack()
I could say the themes are changing not only for color but completely. What is still puzzling me is I cannot see the nice fancy graphic which I expect to see.
I tried to change my windows settings for performance or to visual aspect but it is not the reason why it is not working.
If you have Win 8.1 do you have tkk working as expected?
EDIT2:
This is my look using example from
http://www.tkdocs.com/tutorial/firstexample.html
(Win7 and Win8.1)
The difference can be easily seen on ubuntu between the themes.
So, maybe it is a problem with win8.1. Considered using the ttkthemes module? The ttkthemes module is another set of more "Stylish" styles.
To install:
sudo apt-get install python3-tk
sudo -H pip3 install ttkthemes
To know more, go to:
https://github.com/RedFantom/ttkthemes/wiki/Usage
I have answered another similar question,you can refer to it through this link:
How to make pyinstaller import the ttk theme?
Lemme know in the comments if this also, doesn't work.

Categories