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

30,700

Solution 1

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.

Solution 2

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", ...
Share:
30,700

Related videos on Youtube

heretoinfinity
Author by

heretoinfinity

Trying to teach myself and explore what's out there. Trying to learn more about coding to solve interesting problems.

Updated on February 04, 2020

Comments

  • heretoinfinity
    heretoinfinity about 4 years

    I have been learning tkinter through http://www.python-course.eu/tkinter_message_widget.php

    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.

  • heretoinfinity
    heretoinfinity over 7 years
    thanks for showing that the 2nd option should have been in the lower case.