Search code examples
pythontkintertkinter-button

How to create a keyboard from tkinter buttons that stores the entered letter?


I am writing a python hangman code with tkinter interactive window, which includes a keyboard. I have created the keyboard, however I cannot figure out how to find out which keyboard key was pressed by the user and how to store it as a letter in a variable. I will be really grateful for any help!

klavesnice = Tk()
klavesnice.geometry("800x700+120+100")
buttons = []  
buttons = [
        'q','w','e','r','t','y','u','i','o','p',
        'a','s','d','f','g','h','j','k','l',
        'z','x','c','v','b','n','m'
    ]

radek=3 #row
sloupec=0 #column

for button in buttons:
    command=lambda  x=button: select(x)
    if button!='Space':
        Button(klavesnice,text=button,width=5,font=("arial",14,"bold"),bg='powder blue',command=command,padx=3.5,pady=3.5,bd=5).grid(row=radek,column=sloupec)
    if button=='Space':
        Button(klavesnice,text=button,command=command).grid(row=5,column=sloupec)
    sloupec+=1
    #určení rozložení klávesnice
    if sloupec>9 and radek==3:
        sloupec=0
        radek+=1
    if sloupec>8 and radek==4:
        sloupec=0
        radek+=1

The code above is what displays the keyboard and the code below is the only thing I was able to come up with, which does not save the clicked key to a variable.

zadane=""
entry=Text(zadane_rm,width=43,height=3)
entry.grid(row=1,columnspan=40)
def select(value):
    if value=='Space':
        entry.insert(END,'')
    else:
        entry.insert(END, value)

I would like to store the clicked letter in a string variable called zadane.


Solution

  • To do what you want only requires modifying the select() function so it appends the current value argument to the global zadane string variable. Note I have reformatted your code to follow the PEP 8 - Style Guide for Python Code guidelines more closely to make it more readable.

    import tkinter as tk  # Avoid `import *`
    
    klavesnice = tk.Tk()
    klavesnice.geometry("800x700+120+100")
    
    buttons = [
        'q','w','e','r','t','y','u','i','o','p',
        'a','s','d','f','g','h','j','k','l',
        'z','x','c','v','b','n','m'
    ]
    
    zadane = ''
    entry = tk.Text(klavesnice, width=43, height=3)
    entry.grid(row=1, columnspan=40)
    
    def select(value):
        global zadane
        if value == 'Space':
            entry.insert('end', ' ')
        else:
            entry.insert('end', value)
            zadane = zadane + value
            print(f'{zadane=!r}')
    
    
    radek = 3 #row
    sloupec = 0 #column
    
    for button in buttons:
        command = lambda x=button: select(x)
        if button != 'Space':
            tk.Button(klavesnice, text=button, width=5, font=("arial", 14, "bold"),
                      bg='powder blue', command=command, padx=3.5, pady=3.5, bd=5
                     ).grid(row=radek, column=sloupec)
        if button == 'Space':
            tk.Button(klavesnice, text=button, command=command).grid(row=5, column=sloupec)
        sloupec += 1
        # Specify the keyboard layout
        if sloupec > 9 and radek == 3:
            sloupec = 0
            radek += 1
        if sloupec > 8 and radek == 4:
            sloupec = 0
            radek += 1
    
    klavesnice.mainloop()