keyboard.py
Я поместил код в функцию create()
, которая получает root
и entry
в качестве аргументов - так что теперь я могу использовать import keyboard
это другой файл изапустить keyboard.create(root, some_entry)
.
Используется root
для создания окна Toplevel()
вместо Tk()
- и без секунды mainloop()
.
Используется entry
для использования в select(entry, value)
, чтоиспользуйте entry.insert(...)
В if __name__ == "__main__"
есть код: вы можете запустить его как отдельную программу для тестирования клавиатуры.
Отображаются два Entry
и один Text
(в котором вы можете проверить ключ Enter
).
Я добавил код для Enter
, Tab
, Backspace
для Text
(который требует другого метода). Когда вы нажимаете Shift
или Caps Lock
, он начинает вставлять символы верхнего регистра - но он не меняет текст на кнопках в окне.
В alphabets
Я группировал строки в списках, поэтому я ненужно if/else
, чтобы проверить, должен ли char быть в следующем ряду - я могу использовать два for
-петля.
import tkinter as tk
alphabets = [
['`','1','2','3','4','5','6','7','8','9','0','-','=','Backspace'],
['Tab','q','w','e','r','t','y','u','i','o','p','[',']',"\\"],
['Caps Lock','a','s','d','f','g','h','j','k','l',';',"'",'Enter'],
['Shift','z','x','c','v','b','n','m',',','.','/','Shift'],
['Space']
]
uppercase = False # use uppercase chars.
def select(entry, value):
global uppercase
if value == "Space":
value = ' '
elif value == 'Enter':
value = '\n'
elif value == 'Tab':
value = '\t'
if value == "Backspace":
if isinstance(entry, tk.Entry):
entry.delete(len(entry.get())-1, 'end')
#elif isinstance(entry, tk.Text):
else: # tk.Text
entry.delete('end - 2c', 'end')
elif value in ('Caps Lock', 'Shift'):
uppercase = not uppercase # change True to False, or False to True
else:
if uppercase:
value = value.upper()
entry.insert('end', value)
def create(root, entry):
window = tk.Toplevel(root)
window.configure(background="cornflowerblue")
window.wm_attributes("-alpha", 0.7)
for y, row in enumerate(alphabets):
x = 0
#for x, text in enumerate(row):
for text in row:
if text in ('Enter', 'Shift'):
width = 15
columnspan = 2
elif text == 'Space':
width = 130
columnspan = 16
else:
width = 5
columnspan = 1
tk.Button(window, text=text, width=width,
command=lambda value=text: select(entry, value),
padx=3, pady=3, bd=12, bg="black", fg="white"
).grid(row=y, column=x, columnspan=columnspan)
x += columnspan
# --- main ---
if __name__ == '__main__':
root = tk.Tk()
root.title('Test Keyboard')
label = tk.Label(root, text='Test Keyboard')
label.grid(row=0, column=0, columnspan=2)
entry1 = tk.Entry(root)
entry1.grid(row=1, column=0, sticky='news')
button1 = tk.Button(root, text='Keyboard', command=lambda:create(root, entry1))
button1.grid(row=1, column=1, sticky='news')
entry2 = tk.Entry(root)
entry2.grid(row=2, column=0, sticky='news')
button2 = tk.Button(root, text='Keyboard', command=lambda:create(root, entry2))
button2.grid(row=2, column=1, sticky='news')
text1 = tk.Text(root)
text1.grid(row=3, column=0, sticky='news')
button3 = tk.Button(root, text='Keyboard', command=lambda:create(root, text1))
button3.grid(row=3, column=1, sticky='news')
root.mainloop()
main.py
Импортирует keyboard
и использует keyboard.create(...)
, чтобы показать клавиатуру и назначить ее выбранным Entry
или Text
.
import tkinter as tk
import keyboard
# --- main ---
if __name__ == '__main__':
root = tk.Tk()
root.title('Hello World!')
label = tk.Label(root, text='Form')
label.grid(row=0, column=0, columnspan=3)
#-----
label1 = tk.Label(root, text='Entry 1')
label1.grid(row=1, column=0)
entry1 = tk.Entry(root)
entry1.grid(row=1, column=1, sticky='news')
button1 = tk.Button(root, text='Keyboard', command=lambda:keyboard.create(root, entry1))
button1.grid(row=1, column=2, sticky='news')
#-----
label2 = tk.Label(root, text='Entry 2')
label2.grid(row=2, column=0)
entry2 = tk.Entry(root)
entry2.grid(row=2, column=1, sticky='news')
button2 = tk.Button(root, text='Keyboard', command=lambda:keyboard.create(root, entry2))
button2.grid(row=2, column=2, sticky='news')
#-----
label3 = tk.Label(root, text='Text 1')
label3.grid(row=3, column=0)
text1 = tk.Text(root)
text1.grid(row=3, column=1, sticky='news')
button3 = tk.Button(root, text='Keyboard', command=lambda:keyboard.create(root, text1))
button3.grid(row=3, column=2, sticky='news')
root.mainloop()
Я думал о создании виджета, который имеет Entry + Button Keyboard
, потому что вы используете эту комбинацию очень часто. Может быть, я создам это позже.