Comments (4)
Hi!
Sorry for the late response. At the moment, there is no 'search this page' method. I am nearly done adding one.
I'll let you know when it is finished.
from tkinterweb.
Hi!
I have finally added a way to search the document for text. Update TkinterWeb and use the find_text
command. For example, to search the webpage for the word 'python', use myframe.find_text("python")
. This will highlight all occurrences of the word python
in the document. The find_text
method also takes the optional select
argument, which selects the specified match . For example, myframe.find_text("python", 2)
will scroll to and select the second match of the word 'python' in the document. The ignore_case
parameter can be set to False
to only match letters if they are exactly identical (otherwise, capital and lowercase letters will be treated as being the same). The highlight_all
parameter can be set to False to only highlight the selected match (otherwise, the selected match will be highlighted green and all other matches will be highlighted pink).
One other tip I can give you is that the find_text
method returns the number of matches. You can store this to show the number of matches to the user. Also, use myframe.find_text("")
to clear all matches. Below is an example of a simple implementation of this command to make a find bar.
from tkinterweb import HtmlFrame
import tkinter as tk
class Page(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.frame = frame = HtmlFrame(self)
self.findbar = findbar = tk.Frame(self)
self.find_select_num = 1
self.find_match_num = 0
self.findbox_var = findbox_var = tk.StringVar()
self.find_box = find_box = tk.Entry(findbar, borderwidth=0, textvariable=findbox_var)
find_up = tk.Button(findbar, text="<<", borderwidth=0, command=self.up_and_find)
find_down = tk.Button(findbar, text=">>", borderwidth=0, command=self.down_and_find)
self.ignore_case_var = ignore_case_var = tk.IntVar(value=1)
ignore_case = tk.Checkbutton(findbar, text="Ignore Cases", variable=ignore_case_var, borderwidth=0, command=self.search_in_page)
self.highlight_all_var = highlight_all_var = tk.IntVar(value=1)
highlight_all = tk.Checkbutton(findbar, text="Highlight All", variable=highlight_all_var, borderwidth=0, command=lambda reset=False: self.search_in_page(reset=reset))
self.find_bar_caption = find_bar_caption = tk.Label(findbar)
frame.pack(expand=True, fill="both")
findbar.pack(side="bottom", fill="x")
findbar.columnconfigure(5, weight=1)
find_box.grid(row=0, column=0, padx=5)
find_up.grid(row=0, column=1)
find_down.grid(row=0, column=2)
ignore_case.grid(row=0, column=3, sticky="ns")
highlight_all.grid(row=0, column=4, sticky="ns")
find_bar_caption.grid(row=0, column=6)
frame.load_url("http://tkhtml.tcl.tk")
findbox_var.trace("w", self.search_in_page)
def up_and_find(self):
self.find_select_num -= 1
if self.find_select_num == 0:
self.find_select_num = self.find_match_num
self.search_in_page(reset=False)
def down_and_find(self):
self.find_select_num += 1
if self.find_select_num == self.find_match_num+1:
self.find_select_num = 1
self.search_in_page(reset=False)
def search_in_page(self, x=None, y=None, reset=True):
if reset:
self.find_select_num = 1
self.find_match_num = self.frame.find_text(self.findbox_var.get(), self.find_select_num, self.ignore_case_var.get(), self.highlight_all_var.get())
if self.find_match_num > 0:
self.find_bar_caption.configure(text="{} of {} matches.".format(self.find_select_num, self.find_match_num))
else:
self.find_bar_caption.configure(text="No matches.")
if __name__ == "__main__":
window = Page()
window.mainloop()
Hopefully this helps!
from tkinterweb.
Thank you very much!!
Oh , and also I've found a typo in htmlframe.md in search the page . You typed "API refrence" instead of "API reference"
from tkinterweb.
No problem!
Thanks for letting me know about the typo, I have fixed it.
from tkinterweb.
Related Issues (20)
- Feature Request: Publish Wheel HOT 2
- Selenium support (Making Contact) HOT 4
- Is it possible to get TKHTML3 to render to postscript?
- [Suggestion] split get_node_attribute into 2 methods HOT 1
- PIL not working. HOT 3
- Почему не поддерживает файл png? HOT 5
- Where exactly did you get the TKHTML binaries & how is their source code compiled? HOT 1
- How to compile htmlwidget.MODIFIED.zip? HOT 10
- Support for img alt attribute? HOT 1
- AttributeError: module 'os' has no attribute 'uname' HOT 2
- "RuntimeError: main thread is not in main loop" when trying to load an image that loads elsewhere HOT 2
- Question: Crashes in Tkhtml30.dylib on Mac (Intel) HOT 1
- HtmlFrame doesn't respect height value HOT 7
- Node for found text HOT 10
- Specify font HOT 3
- HtmlFrame widget is trying to interpret the file path as a web URL instead of a local file path when I try to load a folium map. HOT 1
- crash with signal SIGSEGV during loading html into HtmlFrame HOT 3
- AttributeError: 'FreeTypeFont' object has no attribute 'getsize' HOT 2
- Change scroll bar color HOT 2
- [BUG]: Not loading `linear-gradient` CSS. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tkinterweb.