I
want to enable keyboard enter key to function as a submit button in Python gui
for a simple windows desktop application.
I’ve not built many Tkinter apps, but I do have some notes about such, so, from my notes, I have this:
import tkinter as tk
app = tk.Tk()
app.geometry("200x100")
def key_return(event):
label["text"] = "You pressed Enter"
def key_h(event):
label["text"] = "You pressed h"
app.bind('<Return>', key_return)
app.bind('<h>', key_h)
label = tk.Label(app, text="")
label.pack()
app.mainloop()
… which demonstrates what (I think) you’re trying to do.
from tkinter import *
import pandas as pd
import tkinter.messagebox
from tkinter import ttk # for treeview
import tkinter as tk
from tkinter import Tk, BOTH, Menu
root = tk.Tk()
root.geometry(“550x400” ) # width x height
root.title(“Dispatch Assist”) # Adding a title
root.configure(background=‘#ccccff’)
root.minsize(540, 300)
root.maxsize(650, 450)
trv = ttk.Treeview(root)
df = pd.read_excel(“C:\Users\fr\pythonProject7\data\data.xlsx”) # create DataFrame
trv.column(“#0”, minwidth=650)
l1 = tk.Label(root, text=“Enter Word”, background=‘#ccccff’, width=10, font=(‘georgia 14’)) # add one Label
l1.place(x=50,y=35)
e1 = tk.Entry(root, width=25, font=18 ) # added one Entry box
e1.place(x=165,y=40)
b1 = tk.Button(root, text=“Search”, width=7, background=“#b3b3ff”, font=('georgia 14 bold '), command=lambda: my_search())
b1.place(x=400,y=30)
#text = tk()
def pop_menu(event):
menu.tk_popup(event.x_root, event.y_root)
def copy():
e1.event_generate(“<>”)
def cut():
e1.event_generate(“<>”)
def paste():
e1.event_generate(“<>”)
def select_all():
e1.event_generate(“<>”)
#Right Click Menu
menu = Menu(e1, tearoff=0, bg=“black”, fg=“white”)
#options
menu.add_command(label=“Copy”, command=copy)
menu.add_command(label=“Cut”, command=cut)
menu.add_separator()
menu.add_command(label=“Paste”, command=paste)
menu.add_separator()
menu.add_command(label=“Select All”, command=select_all)
#Make the menu pop up
e1.bind(“<Button - 3>”, pop_menu)
def ex() :
tkinter.messagebox.showinfo(“Program”,‘Exit’)
exit()
def hel():
help(“C:\Users\fr\pythonProject7\help.txt”)
def my_search():
#file_menu = Menu(menubar, tearoff=False)
l1 = list(df) # List of column
root.bind(‘’, my_search)
# names as headers
query= e1.get().strip().lower() # get user entered string
if query.isdigit(): # if query is number
str1 = df["id"] == int(query) #
else:
str1 = df.Key_Word.str.contains(query, na=False) # name column value matching
df2 = df[(str1)] # combine all conditions using | operator
r_set = df2.to_numpy().tolist() # Create list of list using rows
trv = ttk.Treeview(root, selectmode="browse") # selectmode="browse" or "extended"
trv.grid(row=10, column=2, columnspan=2, padx=20, pady=110) #
trv["height"] = 10 # Number of rows to display, default is 10
trv["show"] = "headings"
# column identifiers
trv["columns"] = l1
for i in l1:
trv.column(i, width=250, anchor="c")
#Headings of respective columns
trv.heading(i, text=i)
for dt in r_set:
v = [r for r in dt] # creating a list from each row
trv.insert( “”, “end”,iid=v[0], values=v) # adding row
del df[‘list’] #delete ‘list’ column
#keyboard.press(‘enter’)
menu = Menu(root)
root.config(menu=menu)
subm = Menu(menu)
menu.add_cascade(label=“File”, menu=subm)
subm.add_command(label=“Exit”, command=ex)
subml1 = Menu(menu)
menu.add_cascade(label=“Tools”,menu=subml1)
subml1.add_command(label=" Help", command=hel)
root.mainloop()