0

Just started learning Tkinter and was hoping someone could help me. I've been trying to bind a keyboard character (Enter button) to a tk button following this example and not getting anywhere. Say I take the button (Enter) and try bind it nothing happens: Enter.bind('<Return>', lambda:self.retrieve_Input(t)) If I bind to self instead using Lambda nothing happens also. I can get it to trigger if I remove the lambda but that's not the desired outcome self.bind('<Return>', lambda:self.retrieve_Input(t))

My Code:

import sys
import tkinter as tk
from tkinter import ttk

class windows(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.wm_title("Test Application")
        self.lift() #Bringing the GUI to the front of the screen
        main_frame = tk.Frame(self, height=400, width=600) #Creating a main Frame for all pages
        main_frame.pack(side="top", fill="both", expand=True)
        main_frame.grid_rowconfigure(0, weight=1) #Configuring the location of the main frame using grid
        main_frame.grid_columnconfigure(0, weight=1)

        # We will now create a dictionary of frames
        self.frames = {}
        for F in (MainPage, CompletionScreen): #Add the page components to the dictionary.
            page = F(main_frame, self)
            self.frames[F] = page #The windows class acts as the root window for the frames.
            page.grid(row=0, column=0, sticky="nsew")
        self.show_page(MainPage) #Method to switch Pages



    def show_page(self, cont):
            frame = self.frames[cont]
            frame.tkraise()


##########################################################################


class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        #switch_window_button = tk.Button(self, text="Go to the Side Page", command=lambda: controller.show_page(SidePage))
        #switch_window_button.pack(side="bottom", fill=tk.X)

        tk.Label(self, text="Project Python Search Engine", bg='white').pack()
        tk.Label(self, text="", bg='white').pack()
        tk.Label(self, text="Song", bg='white').pack()
        tk.Label(self, text="", bg='white').pack()
        t = tk.Entry(self, bg='white', width = 50)
        t.pack()
        tk.Label(self, text="", bg='white').pack()
        Enter = tk.Button(self, text='Search', command= lambda:self.retrieve_Input(t))
        Enter.pack()
        tk.Button(self, text="Latest Popular Songs", command=lambda:self.Popular_Songs(t)).pack() #Line 210 onwards

        Enter.bind('<Return>', lambda:self.retrieve_Input(t))

    def retrieve_Input(self, t):
        print ("work")
        print (t)


class CompletionScreen(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Completion Screen, we did it!")
        label.pack(padx=10, pady=10)
        switch_window_button = ttk.Button(
            self, text="Return to menu", command=lambda: controller.show_page(MainPage)
        )
        switch_window_button.pack(side="bottom", fill=tk.X)


if __name__ == "__main__":
    App = windows()
    App.mainloop()

I'm not really sure what I'm missing

Answer: The button probably doesn't have the keyboard focus. When I run your code and then use the keyboard to move the focus to the button, your binding works. You probably want to bind to the entry widget rather than the button since that's what will have the keyboard focus. – Thanks Bryan Oakley

  • The button probably doesn't have the keyboard focus. When I run your code and then use the keyboard to move the focus to the button, your binding works. You probably want to bind to the entry widget rather than the button since that's what will have the keyboard focus. – Bryan Oakley Nov 11 '22 at 00:05
  • `lambda e:self.retrieve_Input(e)`, look at this [answer](https://stackoverflow.com/a/62742314/13629335) for an explanation. – Thingamabobs Nov 11 '22 at 00:05

0 Answers0