Reputation: 740
I am making a python text editor, and I am now busy with the find function. Then when the last occurance of the users input is found, it jumps back to the beginning again, and it shows the text 'Found 1st occurance from the top, end of file has been reached' at the bottom of the window.
But showing this changes the position of all the other items too, as you can see here. Now I want to start out with the layout I get after adding the text to the bottom of the dialog. Here's my relevant code:
find_window = Toplevel()
find_window.geometry('338x70')
find_window.title('Find')
Label(find_window, text='Enter text to find:').grid(row=0, column=0, sticky=W)
find_text = Entry(find_window, highlightcolor='blue', highlightbackground='blue', highlightthickness=1)
find_nextbutton = Button(find_window, text='Find Next', command=find_next)
find_allbutton = Button(find_window, text='Find All')
find_text.grid(row=0, column=1, sticky=W)
find_nextbutton.grid(row=0, column=2, sticky=W)
find_allbutton.grid(row=1, column=2, sticky=W)
And when the last occurance is found I do this:
file_end = Label(find_window, text='Found 1st occurance from the top, end of file has been reached.')
file_end.grid(row=2, columnspan=4, sticky=W)
Upvotes: 2
Views: 524
Reputation: 385860
The simplest solution is not force the window to a specific size, and to always have that label there. Set the width large enough to include the full text of the message. When you are ready to show the value, use the configure
method to show the text.
Here's a complete example based on your code:
from tkinter import *
root = Tk()
text = Text(root)
text.pack(fill="both", expand=True)
with open(__file__, "r") as f:
text.insert("end", f.read())
def find_next():
file_end.configure(text='Found 1st occurance from the top, end of file has been reached.')
find_window = Toplevel()
#find_window.geometry('338x70')
find_window.title('Find')
Label(find_window, text='Enter text to find:').grid(row=0, column=0, sticky=W)
find_text = Entry(find_window, highlightcolor='blue', highlightbackground='blue', highlightthickness=1)
find_nextbutton = Button(find_window, text='Find Next', command=find_next)
find_allbutton = Button(find_window, text='Find All')
file_end = Label(find_window, width=50)
find_text.grid(row=0, column=1, sticky=W)
find_nextbutton.grid(row=0, column=2, sticky=W)
find_allbutton.grid(row=1, column=2, sticky=W)
file_end.grid(row=2, columnspan=4, sticky="w")
find_window.lift(root)
root.mainloop()
Upvotes: 1