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.
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.
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", ...
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