python-在文本小部件中搜索单词/字母(tkinter)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-在文本小部件中搜索单词/字母(tkinter),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1755字,纯文字阅读大概需要3分钟。
内容图文
![python-在文本小部件中搜索单词/字母(tkinter)](/upload/InfoBanner/zyjiaocheng/690/bc590a5d4d314691aa277c7a80bdcedb.jpg)
我将如何添加在文本小部件中搜索文本的搜索功能?
*从用户输入中搜索
def openFile():
global text
artiststxt = tkinter.Tk()
artiststxt.title('Artists')
artiststxt.geometry('300x360')
artiststxt.minsize(300,360)
artiststxt.maxsize(500,360)
file = open('Artists.txt','r', encoding='utf-8')
lines = file.read()
scrollbar = Scrollbar(artiststxt, jump = 1)
text = Text(artiststxt, yscrollcommand = scrollbar.set)
scrollbar.configure(command=text.yview)
text.insert(INSERT, lines)
text.config(font=('Fixedsys', 15), fg = 'darkblue', bg = 'lightgray')
menu = tkinter.Menu(artiststxt,tearoff=0)
menu.add_command(label='Save', command = saveFile)
artiststxt.config(menu=menu)
scrollbar.pack(side=RIGHT, fill=BOTH)
text.pack()
编辑:
好的,我发现了如何用此来搜索文本:
def get(event):
global searchent
text.tag_remove('found', '1.0', END)
s = searchent.get()
if s:
idx = '1.0'
while 1:
idx = text.search(s, idx, nocase=1, stopindex=END)
if not idx: break
lastidx = '%s+%dc' % (idx, len(s))
text.tag_add('found', idx, lastidx)
idx = lastidx
text.tag_config('found', foreground='red')
searchent.focus_set()
现在,假设搜索到的文本进一步下降.如何使滚动条向下转到搜索到的文本?
解决方法:
好吧,我知道了.花了一些时间,但值得.
首先,我们在窗口中创建一个输入框,然后将其与Enter键绑定,并放置一个.get事件
searchent.bind(“< Return>”,获得)
当按下回车键时,我们进入def get(event):
def get(event):
global searchent
text.tag_remove('found', '1.0', END)
s = searchent.get() # Grabs the text from the entry box
if s:
idx = '1.0'
while 1:
idx = text.search(s, idx, nocase=1, stopindex=END)
if not idx: break
lastidx = '%s+%dc' % (idx, len(s))
text.tag_add('found', idx, lastidx)
idx = lastidx
text.see(idx) # Once found, the scrollbar automatically scrolls to the text
text.tag_config('found', foreground='red')
searchent.focus_set()
内容总结
以上是互联网集市为您收集整理的python-在文本小部件中搜索单词/字母(tkinter)全部内容,希望文章能够帮你解决python-在文本小部件中搜索单词/字母(tkinter)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。