Jump to content

Python on-screen keyboard test.


Noskiw

Recommended Posts

For my class, I was asked to make an on-screen keyboard that when you clicked a button, the letter would appear in an entry box above. I've done that, but the extension was to create a backspace button.

 

Right now, I'm struggling to see how I can do this.

 

I've tried:

 

if key == "<-":
        entry2 = entry.get()
        pos = entry2.find("")
        pos2 = entry2[pos:]
        entry.delete(pos2, tk.END)

 

But so far, no luck.

 

If anyone has any ideas, they'd be much appreciated.

 

The full code is here:

 

import tkinter as tk

top = tk.Tk()
top.title("On Screen Keyboard")

def click(key):
    if key == "<-":
        entry2 = entry.get()
        pos = entry2.find("")
        pos2 = entry2[pos:]
        entry.delete(pos2, tk.END)
    elif key == " Space ":
        entry.insert(tk.END, ' ')
    else:
        entry.insert(tk.END,key)

button_list = [
'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'
,' Space '
]

entry = tk.Entry(top, width = 84)
entry.grid(row = 1, columnspan = 15)

r = 2
c = 0
for b in button_list:
    rel = 'groove'
    command = lambda x=b: click(x)
    if b != " Space ":
        tk.Button(top, text = b, width = 5, relief = rel, command = command).grid(row = r, column = c)
    if b == " Space ":
        tk.Button(top, text = b, width = 30, relief = rel, command = command).grid(row = 5, columnspan = 10)
    c+=1
    if c > 10 and r == 2:
        c = 0
        r+=1
    if c > 8 and r == 3:
        c = 0
        r+=1
    
top.mainloop()

 

Thanks. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.