Python tkinter: What are the correct values for the anchor option in the message widget?
multi-line label tkinter
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.
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,
Personally I think it's odd to use a constant
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.
Python - Tkinter Anchors, Python - Tkinter Anchors - Anchors are used to define where text is positioned use the anchor=SE option, the widget will be placed in the bottom right corner of Python - Tkinter Anchors. Anchors are used to define where text is positioned relative to a reference point. Here is list of possible constants, which can be used for Anchor attribute. For example, if you use CENTER as a text anchor, the text will be centered horizontally and vertically around the reference point.
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", ...
Python - Tkinter Message, Python - Tkinter Message - This widget provides a multiline and noneditable object that displays texts, These options can be used as key-value pairs separated by commas. The default is anchor=CENTER, which centers the text in the available space. Extra space added to the left and right of the text within the widget. Option Meaning; anchor: The position, where the text should be placed in the message widget: N, NE, E, SE, S, SW, W, NW, or CENTER. The Default is CENTER. aspect: Aspect ratio, given as the width/height relation in percent. The default is 150, which means that the message will be 50% wider than it is high.
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.
GUI Programming with Python: Message widget in Tkinter, This chapter deals with the tkinter message widget under Python. Use LEFT, RIGHT, or CENTER. Note that to position the text inside the widget, use the anchor option. Default is LEFT. padx, Horizontal padding. Default is -1 (no The default is FLAT. Other possible values are SUNKEN, RAISED, GROOVE, and RIDGE. The Tkinter Message Widget The Message widget is a variant of the Label , designed to display multiline messages. The message widget can wrap text, a nd adjust its width to maintain a given aspect ratio.
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', ...)
The Tkinter Message Widget, The Message widget is a variant of the Label, designed to display multiline messages. The message If no options are given, the method returns a dictionary containing all current option values. **options: Widget options. anchor= Where in the message widget the text should be placed. Use LEFT, RIGHT, or CENTER. Following are commonly used Option can be used with this widget :- anchor: This option is used to decide the exact position of the text within the space .Its default value is CENTER. bg: This option used to represent the normal background color.
Learn Python Programming, Python. Tkinter. Message. The Message widget is used to show the message to SN Option 1 anchor 2 bg 3 bitmap 4 bd 5 cursor 6 font 7 fg 8 height 9 image The possible values can be LEFT (left alignment), CENTER (default), and RIGHT anchor= Where in the message widget the text should be placed. Use one of N, NE, E, SE, S, SW, W, NW, or CENTER. Default is CENTER. (the database name is anchor, the class is Anchor) aspect= Aspect ratio, given as the width/height relation in percent. The default is 150, which means that the message will be 50% wider than it is high. Note that if the width is explicitly set, this option is ignored. (aspect/Aspect)
25.1. tkinter — Python interface to Tcl/Tk, Legal values are: 'left', 'right', 'top', 'bottom'. 220.127.116.11. Coupling Widget Variables¶. The current-value setting of some widgets (like text entry widgets) can be Python Tkinter Message. The Message widget is used to show the message to the user regarding the behaviour of the python application. The message widget shows the text messages to the user which can not be edited. The message text contains more than one line. However, the message can only be shown in the single font.
tkinter.ANCHOR, tkinter.ANCHOR The anchor value determine how to place widget within the master or grid when no The message widget are placed using anchor method. Python - Tkinter Message. This widget provides a multiline and noneditable object that displays texts, automatically breaking lines and justifying their contents. Its functionality is very similar to the one provided by the Label widget, except that it can also automatically wrap the text, maintaining a given width or aspect ratio.
- thanks for showing that the 2nd option should have been in the lower case.