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 *
Related
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!
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.
I'm using the solitaire.py file and trying to convert it to Python 3. It was originally 2.7 and works fine in that version but need it in 3 so I can make an AI with PyTorch to play it but I'm having an issue where:
from Canvas import Rectangle, CanvasText, Group, Window
shows the error:
ModuleNotFoundError: No module named 'Canvas'
Does anyone have any idea as to why or if there is something similar I could use in python 3 to make it work?
Canvas was deprecated in Python 3.
Instead you can use tkinter by doing: [docs]
from tkinter import Canvas
Or do this:
from tkinter import *
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.
I have downloaded python 2.5. I would like to know if Tkinter is included with python or is it a separate download?
Yes, it is included.
http://wiki.python.org/moin/TkInter
If you are using linux just open your terminal and type python and in the python interpreter type from Tkinter import* if it doesn't show any error messages you are good to go. You can try this to check every package of python like Pygame just replace Tkinter by Pygame