python 2.7 - no module named tkinter - python

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!

Tkinter (capitalized) refers to versions <3.0.
tkinter (all lowecase) refers to versions ≥3.0.
Source: https://wiki.python.org/moin/TkInter

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.

For python 2.7 it is Tkinter, however in 3.3.5 it is tkinter.

For python 2.7 use Cap Letters Tkinter but for >3.0 use small letter tkinter

Related

root = tkinter.Tk() or root = Tk()?

I have two scripts that both work:
import tkinter
root = tkinter.Tk()
root.configure(bg='blue')
root.mainloop()
and
from tkinter import *
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello world!")
text.pack()
root.mainloop()
I want to combine the two scripts to print the text on a blue background, but moving anything from one script to another seems to break it.
I can't figure out if it's about root = tkinter.Tk() vs root = Tk(), or import tkinter vs from tkinter import *, or something entirely different. I can't find a successful combination.
I'm using Ubuntu and Python 3.6.9.
Because you use two different styles when importing tkinter, you will need to modify the code from one file when moving to the other. The code in your first example is the preferred way to do it because PEP8 discourages wildcard imports.
When when you copy the code from the second example, you'll need to add tkinter. to every tkinter command (tkinter.Tk(), tkinter.Text(root), tk.INSERT, etc.
Personally I find import tkinter as tk to be a slight improvement. I find tk.Tk() to be a little easier to type and read than tkinter.Tk().
You should know that:
from tkinter import *
will import all the attribute in the tkinter.But if you also define some variable in your script.It will be covered by your new variable.So we don't recommend you to use that.(If you used both from tkinter.ttk import * and from tkinter import *.Some default widgets of tkinter will be covered by ttk widgets.)
Just like Mr.Bryan said,I'd like to use import tkinter as tk,too.

Module 'tkinter' has no attribute 'Tk'

Tkinter doesnt contain any tk attribute.
import tkinter
window = tkinter.Tk()
win.mainloop()
While running this code it gives me an error saying
module 'tkinter' has no attribute 'Tk'
Did you named your python file tkinter.py or Tkinter.py ? Try to rename it. It may be the cause.
if the file name is tkinter.py in program
import tkinter
it will imports the our file name which is overriders the content there is not Tk() module, so it throw the error
Python 3.x
import tkinter
window = tkinter.Tk()
window.mainloop()
import tkinter
raiz= tkinter.Tk()
raiz.mainloop()
remember that the file name cannot be tkinter.py
Try copying the file to the Python path in C drive (in my case)
And the folder should not contain any other file named Tkinter.py or similar for Code click here
try Tk instead of tk
it worked for me, if you think you are importing wrong,try:
import tkinter
tkinter._test()
In my case, the error occurred in top =tk.Tk()
The simple trick I used was to change the uppercase K in 'TK' to lowercase k
import tkinter as tk
import tkinter.filedialog as fd
from tkinter import *
import PIL
from PIL import ImageTk
from PIL import Image
top =tk.Tk()
top.geometry('800x600')
top.title('Image Processing')
top.configure(background='#CDCDCD')
Your python script name is must not be tkinker.py python could prioritize your script as tkinker and that couldn return that error.
it's capital 'T' and small 'k' =>> 'Tk' not capital K make sure, small mistake

Python tkinter 8.5 import messagebox

The following code runs fine within IDLE, but otherwise I get "NameError: global name 'messagebox' is not defined". However, if I explicitly state from tkinter import messagebox, it runs fine from where ever.
from tkinter import *
from tkinter import ttk
root = Tk()
mainFrame = ttk.Frame(root)
messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame)
Why does IDLE not need the explicit import statement but elsewhere it is required?
the messagebox is a separate submodule of tkinter, so simply doing a complete import from tkinter:
from tkinter import *
doesn't import messagebox
it has to be explicitly imported like so:
from tkinter import messagebox
in the same way that ttk has to be imported explicitly
the reason it works in idle is because idle imports messagebox for its own purposes, and because of the way idle works, its imports are accessible while working in idle
IDLE is written in Python and uses Tkinter for the GUI, so it looks like your program is using the import statements that IDLE itself is using. However, you should explicitly include the import statement for the messagebox if you want to execute your program outside the IDLE process.
messagebox.showinfo is defined inside tkinter/showinfo.py but when you use from tkinter import * you only import tkinter/__init__.py which holds the definitions of Label, Entry, Button, ... That is how python imports work.
When you use from tkinter import messagebox it looks for messagebox inside tkinter/__init__.py but it can't find it so it tries to import tkinter/messagebox.py
As for the IDLE anomaly, it is a bug in IDLE and I believe that it was patched.

Python Tkinter - global name Tk() is not defined

I've been following a tutorial for the tkinter interface in python and it uses the following piece of code to declare a root widget for the program which then has children widgets:
root = Tk()
I'm getting the following error when trying to interpret this piece of code:
Global name Tk() is not defined
I'm fairly sure this is because tkinter has changed since the tutorial; I cannot find any other tutorials that do not use snippets of code like this so they wouldn't work either.
The question I have is in context simple, but searching, I cannot find the answer;
How can I bypass this: what has changed to the syntax of tkinter and what is the new method of sorts to declare a root widget? additionally it would be brilliant if anybody has the knowledge of tkinter to warn me whether the way in which you can add children widgets to the root has changed as well.
Thank you for any and all replies ~ Michael
You probably forgot from Tkinter import * at the top.
Alternatively, there's
import Tkinter
or
import Tkinter as tk
Edit: Generally, you want to use the idiom from <library> import <module>, so for your specific example from Tkinter import Tk would work.
which just allows you type tk.Button, for example, rather than Tkinter.Button throughout your code. And if you're using Python 3.x, the library is lowercase.
import tkinter
For general importing questions, I've seen the Importing Python Modules link referenced a lot on SO.
You seem to have named the file tkinter.py. You cannot name a file with the module you are importing. Python will try to import from your existing file instead of tkinter module. There will be module name collison. Try to rename your file name.
from Tkinter import *
root = Tk()

Python Message Box Without huge library dependency

Is there a messagebox class where I can just display a simple message box without a huge GUI library or any library upon program success or failure. (My script only does 1 thing).
Also, I only need it to run on Windows.
You can use the ctypes library, which comes installed with Python:
import ctypes
MessageBox = ctypes.windll.user32.MessageBoxW
MessageBox(None, 'Hello', 'Window title', 0)
Above code is for Python 3.x. For Python 2.x, use MessageBoxA instead of MessageBoxW as Python 2 uses non-unicode strings by default.
There are also a couple prototyped in the default libraries without using ctypes.
Simple message box:
import win32ui
win32ui.MessageBox("Message", "Title")
Other Options
if win32ui.MessageBox("Message", "Title", win32con.MB_YESNOCANCEL) == win32con.IDYES:
win32ui.MessageBox("You pressed 'Yes'")
There's also a roughly equivalent one in win32gui and another in win32api. Docs for all appear to be in C:\Python{nn}\Lib\site-packages\PyWin32.chm
The PyMsgBox module uses Python's tkinter, so it doesn't depend on any other third-party modules. You can install it with pip install pymsgbox.
The function names are similar to JavaScript's alert(), confirm(), and prompt() functions:
>>> import pymsgbox
>>> pymsgbox.alert('This is an alert!')
>>> user_response = pymsgbox.prompt('What is your favorite color?')
A quick and dirty way is to call OS and use "zenity" command (subprocess module should be included by default in any python distribution, zenity is also present in all major linux). Try this short example script, it works in my Ubuntu 14.04.
import subprocess as SP
# call an OS subprocess $ zenity --entry --text "some text"
# (this will ask OS to open a window with the dialog)
res=SP.Popen(['zenity','--entry','--text',
'please write some text'], stdout=SP.PIPE)
# get the user input string back
usertext=str(res.communicate()[0][:-1])
# adjust user input string
text=usertext[2:-1]
print("I got this text from the user: %s"%text)
See the zenity --help for more complex dialogs
You can also use the messagebox class from tkinter:
from tkinter import messagebox
unless tkinter is that huge GUI you want to avoid.
Usage is simple, ie:
messagebox.FunctionName(title, message [, options])
with FuntionName in (showinfo, showwarning, showerror, askquestion, askokcancel, askyesno, askretrycancel).
This one with tkinter.
from tkinter import * #required.
from tkinter import messagebox #for messagebox.
App = Tk() #required.
App.withdraw() #for hide window.
print("Message Box in Console")
messagebox.showinfo("Notification", "Hello World!") #msgbox
App.mainloop() #required.

Categories