2017-06-28 48 views
2

我正在制作一个python文本编辑器,现在我正在忙于查找功能。 然后,当找到用户输入的最后一次发生时,它会再次跳回到开头,并显示窗口底部的文本'从顶部发现第一次发生,文件已达到末尾'。将标签添加到tkinter小部件会更改整个布局

但是,显示此更改也会改变所有其他项目的位置,您可以看到here。现在我想从开始,将文本添加到对话框的底部。这里是我的相关代码:

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) 

而当最后一次出现是发现我这样做:

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) 
+0

为了确保我明白什么是“改变整个布局”的意思,看来你真正关心的是,按钮移动一点点在右边。那是正确的吗?我没有看到其他任何在截图中看起来差异很大的东西。 –

+0

按钮和输入都向右移动,我希望它们在打开窗口时从那个位置开始 –

回答

1

最简单的办法是不强制窗口为特定的大小,并且总是有这种标签有。将足够大的宽度设置为包含消息的完整文本。当您准备好显示该值时,请使用configure方法显示文本。

下面是根据你的代码一个完整的例子:

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() 
+0

非常感谢! –

相关问题