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!
Related
I'm creating a simple UI, my problem is that buttons are always white also if I change all their color properties...I'm new in Tkinter maybe I'm missing something.
This is the code, you can just copy and run:
import tkinter
tk = tkinter.Tk()
def browseFiles():
print("Browsing...")
browseButton = tkinter.Button(
tk, text="Browse files", command=browseFiles, fg='#03045e', bg='#caf0f8', background='#ef233c', activeforeground='blue').pack()
tk.mainloop()
This is a picture of result:
MacOS version : 11.5.2 withPython 3.10.0rc2, tcl-tk 8.6.11_1 and when I run the code this is the warning :
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
Maybe I should downgrade to a python stable version ?
Solved :
pip3 install tkmacosx
This will install some extensions that will fix the problem.
Just one thing be sure to switch interpreter, maybe you're not using the right one.
In my case it worked when I used this one .
Check where tkmacosx has been installed, you should use the same interpreter version I guess.
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.
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()
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 *
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.