Reputation: 125
Using Python 2.6.1 or 2.7.9 Tkinter's Entry widget entry.insert seems to destroy left justification. How do I get the text to stay left justified? Specifically, after entry.get() provides what I type into the widget, I insert additional text automatically at the beginning and redisplay it. When redisplayed, the justification ~sometime~ changes.
Specifically, when I enter a string of 20 characters or less and the entry.insert makes it longer, all works okay with some of the string not visible off the right side of the entry widget. When I enter a string of 21 characters or more and the entry.insert makes it longer, it displays as right justified so only the end of the string is visible and "THANK YOU" is never seen off the left side.
Why does this happen and how do I get it to stop? I've considered Entry Widget Justification - text longer than widget but in his case, the OP wasn't doing an insert and the vague question didn't get a good answer.
The default displayed text size for an Enter widget is 20 characters, but the same phenomena happens for whatever Entry width I specify. Phenomena also happens on Raspberry Pi 3 and Mac OSX.
# demo of broken justification 1/14/2017
import Tkinter as tk
master = tk.Tk()
def clear_entry():
entry.delete(0, tk.END)
entry.config(bg='white')
def process_butninput():
if entry.get()=="done":
master.destroy()
else:
entry.insert(0,"THANK YOU ")
datarecord = entry.get()
print( "%s" % datarecord) # debugging only
entry.config(bg='green')
entry.after(1500, clear_entry)
label = tk.Label( master, text="Enter text: ")
label.grid(row=0, padx=20, pady=30)
entry = tk.Entry( master)
entry.grid( row=0, column=1)
button = tk.Button( master, text='Enter', command=process_butninput)
button.grid(row=0, column=3, padx=20, pady=30)
master.mainloop()
Upvotes: 2
Views: 1761
Reputation: 125
Text justify is only relevant for under-sized text strings. If the text string is longer than the Entry width, then justify does nothing and you have to use xview.
I was basing my expectations on how, for example, Word and Excel display justified text, but that is not how Tkinter displays text in the Entry widget.
Upvotes: 1
Reputation: 385900
I don't fully understand what you are asking, but it sounds like you simply need to scroll the contents of the entry widget after inserting "THANK YOU " so that the character at position 0 (zero) is visible.
entry.insert(0,"THANK YOU ")
entry.xview(0)
Upvotes: 0