[Python] Issue getting keystroke entries to show up in text fields (Python)

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    enter image description here I am spinning my wheels trying to get python to spit the keypad strokes into my textfield Entries. Below is a Min Working Example. If the cursor is within the textfield, any keystroke from the keypad needs to be printed to the field.

    The original code i got from here and attempted to modify it to meet my application needs but i could not identify the issue or major differences.

    I'm obviously not great at coding, so apologies for any poor practices, i can definitely do better if you have any best practices to share. Any advice on the issue will be appreciated! Please let me know if there is any other clues you may need to look into this.

    this is not input from computer keyboards, but from the keypad itself (my end device will be a touch screen).

    Thank you.

    import tkinter as tk
    from tkinter import *
    import tkinter.font as font

    class Keypad(tk.Frame):

    cells = [['1', '2', '3'],
    ['4', '5', '6'],
    ['7', '8', '9'],
    ['0', '.', '-']]

    def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)

    bold_font = font.Font(family="Helvetica", size=16, weight="bold")

    for y, row in enumerate(self.cells):
    for x, item in enumerate(row):
    b = tk.Button(self, text=item, height=3, width=5, relief='ridge', font=bold_font, command=lambda text=item:self.target.append(text))
    b.grid(row=y, column=x, sticky='news')

    x = tk.Button(self, text='Backspace', height=3, width=16, command=self.backspace)
    x.grid(row=0, column=10, sticky='news')

    x = tk.Button(self, text='Clear', height=3, width=16, command=self.clear)
    x.grid(row=1, column=10, sticky='news')

    x = tk.Button(self, text='Exit', command=self.exit_)
    x.grid(row=9, column=0, sticky='news', columnspan='3')

    self.place(relx=0.5, rely=0.5, anchor='c') # place keypad in frame

    def get(self):
    if self.target:
    return self.target.get()

    def append(text):
    print(text)
    self.target.insert('end', text)
    position = self.target.index(tk.INSERT)
    print("Current cursor position: {position}")

    def clear(self):
    if self.target:
    self.target.delete(0, 'end')

    def backspace(self):
    if self.target:
    text = self.get()
    text = text[:-1]
    self.clear()
    self.append(text)

    def exit_(self):
    #TODO: copy to clipboad
    if self.target:
    pass

    root = tk.Tk() # create root window
    root.title("title goes here")
    root.geometry("950x550") # Set the starting size of the window
    root.config(bg="whitesmoke")

    # Create left and right frames
    left_frame = tk.Frame(root, width=275, height=500, bg='silver')
    left_frame.place(x=10, y=10, relx=0.01, rely=0.01)

    right_frame = tk.Frame(root, width=625, height=500, bg='silver')
    right_frame.place(x=290, y=10, relx=0.01, rely=0.01)

    Keypad(right_frame)

    label_bar = tk.Frame(left_frame, pady=5, width=125, height=185, bg='lightgrey')
    label_bar.place(x=5, rely=0.4)

    filter_bar = tk.Frame(left_frame, pady=5, width=125, height=185, bg='lightgrey')
    filter_bar.place(x=140, rely=0.4)

    tk.Label(filter_bar, text="Data", relief='ridge').place(in_=filter_bar, relx=0.5, anchor='n')

    brg_sn_entry = tk.Entry(filter_bar, text="", width=15).place(in_=filter_bar, relx=0.5, rely=0.20, anchor=CENTER)
    brg_lot_entry = tk.Entry(filter_bar, text="", width=15).place(in_=filter_bar, relx=0.5, rely=0.35, anchor=CENTER)
    brg_wo_entry = tk.Entry(filter_bar, text="", width=15).place(in_=filter_bar, relx=0.5, rely=0.50, anchor=CENTER)

    root.mainloop()



    I have tried a large variety of code variations when calling the 'append' function but nothing seems to work. your input is greatly appreciated.

    Continue reading...

Compartilhe esta Página