简体   繁体   中英

I can't understand some find_text function in Tkinter

I started read book about Tkinter

(Tkinter GUI Application Development Blueprints)

and when I began to make my text editor with the help of this book I don't really understand one part.

There is a feature than can help you find words in the text.

def find_text(event=None):
search_toplevel = Toplevel(root)
search_toplevel.title('Find Text')
search_toplevel.transient(root)
search_toplevel.resizable(False, False)
Label(search_toplevel, text="Find All:").grid(row=0, column=0, sticky='e')
search_entry_widget = Entry(
    search_toplevel, width=25)
search_entry_widget.grid(row=0, column=1, padx=2, pady=2, sticky='we')
search_entry_widget.focus_set()
ignore_case_value = IntVar()
Checkbutton(search_toplevel, text='Ignore Case', variable=ignore_case_value).grid(
    row=1, column=1, sticky='e', padx=2, pady=2)
Button(search_toplevel, text="Find All", underline=0,
       command=lambda: search_output(
           search_entry_widget.get(), ignore_case_value.get(),
           content_text, search_toplevel, search_entry_widget)
       ).grid(row=0, column=2, sticky='e' + 'w', padx=2, pady=2)

Lines where we create a button really hard for me. I found out that lambda-function is just like normal function but created in other way, but other stufffff is really hard.Can you please explain me what this code is actually do? (button code)

full code:

https://github.com/PacktPublishing/Tkinter-GUI-Application-Development-Blueprints-Second-Edition/blob/master/Chapter%2002/2.05.py

Maybe it will help you to see it written out as a normal function. This code would work just as well:

def find_clicked():
    search_output(
        search_entry_widget.get(), ignore_case_value.get(),
        content_text, search_toplevel, search_entry_widget)

Button(search_toplevel, text="Find All", underline=0,
       command=find_clicked).grid(row=0, column=2, sticky='e' + 'w', padx=2, pady=2)

The only thing lambda is good for is confusing people by stuffing things onto a single line. I recommend you avoid lambda if at all possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM