i am on mac os x 10.8, using the integrated python 2.7. i try to learn about tkinter with tutorials like this for python 2.7 (explicitly not 3) they propose the following code:
from tkinter import *
import tkinter.messagebox
however, this brings up the error:
ImportError: No module named tkinter
using import.Tkinter with a capital t seems to work, but further commands like
import Tkinter.messagebox
don't (neither does tkinter.messagebox). I've had this problem with lots of tutorials. what's the thing with the capital / non-capital "T", and how do i get my python to work like it does in the tutorials? Thanks in advance!
The easiest way to fix this problem is by upgrading your python to use python 3. If upgrading python is not an option, you only need to rename your imports to use Tkinter (uppercase) instead of tkinter (lowercase). Output: As a result, this window proves that your python installation includes the Tkinter module.
If you are on Linux/Mac/similar variant then you need to run sudo apt-get install python3-tk . It is not a pip package and cannot be installed in this way. If the python -m tkinter command fails on Windows then your installation is broken, or if on Linux/Mac/Similar then the command above should work.
To install Tkinter, we need Python pre-installed. Tkinter actually comes along when we install Python. While installing Python, we need to check the td/tk and IDLE checkbox. This will install the tkinter and we need not install it separately.
Tkinter comes pre-installed with the Python installer binaries for Mac OS X and the Windows platform. So if you install Python from the official binaries for Mac OS X or Windows platform, you are good to go with Tkinter. For Debian versions of Linux you have to install it manually by using the following commands.
In Tkinter (uppercase) you do not have messagebox.
You can use Tkinter.Message
or import tkMessageBox
This code is an example taken from this tutorial:
import Tkinter
import tkMessageBox
top = Tkinter.Tk()
def hello():
tkMessageBox.showinfo("Say Hello", "Hello World")
B1 = Tkinter.Button(top, text = "Say Hello", command = hello)
B1.pack()
top.mainloop()
Your example code refers to a python installation >= py3.0. In Python 3.x the old good Tkinter has been renamed tkinter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With