Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python tkinter: What are the correct values for the anchor option in the message widget?

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.

like image 674
heretoinfinity Avatar asked Jan 20 '17 17:01

heretoinfinity


2 Answers

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.

like image 114
Bryan Oakley Avatar answered Sep 29 '22 16:09

Bryan Oakley


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", ...
like image 44
Kevin Avatar answered Sep 29 '22 18:09

Kevin