0

I have a simple script that uses tkinterweb to display a search result that is stored as html. When I try to bind the search function to the Enter key using the "button.bind('',function())" method, it executes the function immediately on mainloop and doesn't execute it when the Enter key is pressed. Is this a problem with tkinterweb? Minimum reproducible is below:

import tkinter
from tkinter import ttk
from tkinter import messagebox
import pickle
import tkinterweb
from tkinterweb import *

proj = tkinter.Tk()
proj.geometry("550x550")
proj.title("Offline Info Files")
proj.bind('<Escape>', quit) 


def infosearch(Event=None):
    target = "Search"
    codesnip = "<h1>HELLO THERE!</h1>"
    popup = tkinter.Toplevel()
    popup.geometry("1100x700") 
    popup.title("Info " + str(target))
    frame = HtmlFrame(popup)
    frame.pack(fill="both",expand=True)
    frame.load_html(codesnip) 
    
Title = tkinter.Entry(bd=1, width = 20,font=("Courier New", 12), textvariable="infofile")
Title.pack()
searchbutton = tkinter.Button(text=" Search ", font=("Courier New",12), command = infosearch)
searchbutton.pack() 
searchbutton.bind('<Return>',infosearch()) 
Title.focus_set()
proj.mainloop()
4
  • 1
    Pro tip, and just an aside: you don't need from tkinterweb import * if you're already using import tkinterweb - you can just prefix tkinterweb objects like so: tkinterweb.HtmlFrame. You generally want to avoid "star imports" as best practice. Commented May 22 at 16:01
  • 1
    You don't need the event binding as you already use command option of the button. Commented May 22 at 16:04
  • 1
    It should be searchbutton.bind('<Return>', infosearch). Commented May 22 at 16:08
  • bind() similar to command= and after() needs function's name without () - and it will later use () to execute this command. This name in function often is called "callback". Commented May 23 at 17:22

1 Answer 1

0

Just saw your question and referred your code. You are almost at the right track. The reason why you are getting a Null value or object is that you are invoking the 'infosearch()' function immediately (this is because of the paranthesis). So, in reality, instead of waiting for the enter key, it immediately runs the function, which was causing you the trouble. Just remove the paranthesis so it will be converted into a callback function, and you are good to go!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.