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
https://forums.phpfreaks.com/topic/252256-python-on-screen-keyboard-test/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.