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

2024/11/17 2:29:09

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 tkinterroot = 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.

Answer

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.

https://en.xdnf.cn/q/71597.html

Related Q&A

Why isnt Pickle calling __new__ like the documentation says?

The documentation for Pickle specifically says:Instances of a new-style class C are created using:obj = C.__new__(C, *args)Attempting to take advantage of this, I created a singleton with no instance a…

Remove more than one key from Python dict

Is there any efficient shortcut method to delete more than one key at a time from a python dictionary?For instance;x = {a: 5, b: 2, c: 3} x.pop(a, b) print x {c: 3}

Install poppler in AWS base python image for Lambda

I am trying to deploy my docker container on AWS Lambda. However, I use pdf2image package in my code which depends on poppler. To install poppler, I need to insert the following line in the Dockerfile.…

Cant change state of checkable QListViewItem with custom widget

I have a QListWidget where I want to add a bunch of items with a custom widget:listWidget = QListWidget()item = QListWidgetItem()item.setFlags(item.flags() | Qt.ItemIsUserCheckable)item.setCheckState(Q…

Custom table for str.translate in Python 3

If I run this code:s.translate(str.maketrans({as: dfg, 1234: qw}))I will get:ValueError: string keys in translate table must be of length 1Is there a way to replace multiple characters at once using st…

Sending form data to aspx page

There is a need to do a search on the websiteurl = rhttp://www.cpso.on.ca/docsearch/this is an aspx page (Im beginning this trek as of yesterday, sorry for noob questions)using BeautifulSoup, I can get…

What is the difference between imregionalmax() of matlab and scipy.ndimage.filters.maximum_filter

I need to find the regional maxima of an image to obtain foreground markers for watershed segmentation. I see in matlab use the function imregionalmax(). As I dont have the matlab software, I use the f…

crontab: python script being run but does not execute OS Commands

I have this crontab configuration setup and the following script.MAILTO="[email protected]" 41 15 * * * /usr/bin/python /home/atweb/Documents/opengrok/setup_and_restart.py > /home/at…

concatenating arrays in python like matlab without knowing the size of the output array

I am trying to concatenate arrays in python similar to matlab array1= zeros(3,500); array2=ones(3,700); array=[array1, array2];I did the following in python:array1=np.zeros((3,500)) array2=np.ones((3,7…

How to save the result of a comparison using Djangos with template tag?

I would like to create new variable in django template, which will have a value of comparison obj.site.profile.default_role == objUnfortunately none of this code works: {% with obj.site.profile.default…