Giter Club home page Giter Club logo

Comments (4)

Andereoo avatar Andereoo commented on August 23, 2024

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.

Andereoo avatar Andereoo commented on August 23, 2024

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.

maxim3007 avatar maxim3007 commented on August 23, 2024

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.

Andereoo avatar Andereoo commented on August 23, 2024

No problem!

Thanks for letting me know about the typo, I have fixed it.

from tkinterweb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.