6

I have been learning tkinter through Message widget in Tkinter at Python Courses and Tutorials.

I keep getting an error when I add the anchor option with the options presented on the site. I am being told that NE does not exist but NE is given as an anchor option in the link above:

NameError: name 'NE' is not defined

Here's my code.

import tkinter

root = tkinter.Tk()
message = ("Whatever you do will be insignificant,"
"but it is very important that you do it.\n"
"(Mahatma Gandhi)")

msg = tkinter.Message(root,text = message, anchor = NE, aspect = 1000,
                      foreground='red', background='yellow', 
                      highlightcolor='green', highlightthickness=0,
                      borderwidth=500)
#msg.config(bg='lightgreen', font=('times', 24, 'italic'))
msg.pack()
tkinter.mainloop()

Edit: I also tried typing in 'NE' in single quotes and it didn't work.

martineau
  • 119,623
  • 25
  • 170
  • 301
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33

4 Answers4

11

The values are the string literals "n", "ne", "e", "se", "s", "sw", "w", "nw", or "center". Case is important. They represent the directions of the compass (north, south, east, and west)

In your case you're using tkinter constants that contain these values. Because of the way you are importing tkinter, you must prefix these constants with the module name. For example, tkinter.NE.

Personally I think it's odd to use a constant N or NE that is set "n" or "ne". The constant serves no purpose other than to sometimes cause confusion in cases such as this.

Here is the canonical documentation:

Specifies how the information in a widget (e.g. text or a bitmap) is to be displayed in the widget. Must be one of the values n, ne, e, se, s, sw, w, nw, or center. For example, nw means display the information such that its top-left corner is at the top-left corner of the widget.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • One can also peek at `tkinter` module's [`constants.py`](https://github.com/python/cpython/blob/3.10/Lib/tkinter/constants.py) source file and to what values the module defines. – martineau May 15 '22 at 14:53
4

I think you need to qualify it with the module name.

msg = tkinter.Message(root,text = message, anchor = tkinter.NE, ...

You could also use a string literal, but I'm not sure that's documented behavior.

msg = tkinter.Message(root,text = message, anchor = "ne", ...
Kevin
  • 74,910
  • 12
  • 133
  • 166
1

If anyone else is having trouble with the anchor option try putting it in the pack function:

msg.pack(anchor = 'w')

Instead of inside Message or config which oddly didn't work for me:

msg.Message(anchor = 'w', ...)
msg.config(anchor = 'w', ...)
DonAr
  • 63
  • 5
0

Just clarifying why the code worked if changes were made to the example code on the website link in the question.

After reading answers from Kevin and Bryan Oakley, I realized that the reason why the website had NE versus tkinter.NE and it worked, if you added the anchor option to the example code, is the way they imported the tkinter module.

from Tkinter import * #Python 2

from tkinter import * #Python 3

I just learned this a week ago and Mark Roddy in his response here ('import module' or 'from module import'), discourages the use of import * as it is hard to tell what module the code is coming from. This is the confusion that I had.

Community
  • 1
  • 1
heretoinfinity
  • 1,528
  • 3
  • 14
  • 33