在另一个 python 程序中使用虚拟键盘输入条目
Typing on entry using virtual keyboard in another python program
我在输入 Tkinter 条目时遇到问题。我做了一个注册码,我的代码上的每个条目都会调用一个虚拟键盘,虚拟键盘是分开的 python 程序。我成功调用了另一个 python 程序,但我无法使用我调用的虚拟键盘在我的主程序 Tkinter 条目上键入内容。
有什么办法可以做到这一点?
这是我的代码
def register(self):
self.master_register = Toplevel()
self.first_name = StringVar()
self.middle_name = StringVar()
self.last_name = StringVar()
self.sex = StringVar()
self.birth_day = StringVar()
self.civil_status = StringVar()
self.label_head = Label(self.master_register, text = "Please fill up all informations below")
self.label_head.pack()
self.label_first_name = Label(self.master_register, text = "First Name")
self.label_first_name.pack()
self.entry_first_name = Entry(self.master_register, textvariable = self.first_name)
self.entry_first_name.pack()
self.entry_first_name.bind('<ButtonPress-1>',self.keyboard)
self.label_middle_name = Label(self.master_register, text = "Middle Name")
self.label_middle_name.pack()
self.entry_middle_name = Entry(self.master_register, textvariable = self.middle_name)
self.entry_middle_name.pack()
self.entry_middle_name.bind('<ButtonPress-1>',self.keyboard)
self.label_last_name = Label(self.master_register, text = "Last Name")
self.label_last_name.pack()
self.entry_last_name = Entry(self.master_register, textvariable = self.last_name)
self.entry_last_name.pack()
self.entry_last_name.bind('<ButtonPress-1>',self.keyboard)
self.label_sex = Label(self.master_register, text = "Sex")
self.label_sex.pack()
self.radio_button_sex1 = Radiobutton(self.master_register, text = "Male", variable = self.sex, value = "Male" )
self.radio_button_sex1.pack()
self.radio_button_sex2 = Radiobutton(self.master_register, text = "Female", variable = self.sex, value = "Female")
self.radio_button_sex2.pack()
self.label_birthday = Label(self.master_register, text = "Birth Day")
self.label_birthday.pack()
self.entry_birthday = Entry(self.master_register, textvariable = self.birth_day)
self.entry_birthday.pack()
self.entry_birthday.bind('<ButtonPress-1>',self.keyboard)
self.label_civil_status = Label(self.master_register, text = "Civil Status")
self.label_civil_status.pack()
self.entry_civil_status = Entry(self.master_register, textvariable = self.civil_status)
self.entry_civil_status.pack()
self.entry_civil_status.bind('<ButtonPress-1>',self.keyboard)
self.button_submit = Button(self.master_register, text = "Submit", command = lambda: self.registered(self.first_name, self.middle_name,
self.last_name, self.sex, self.birth_day,
self.civil_status))
self.button_submit.pack()
def keyboard(self, event):
exec(open("keyboardclass.py").read());
这是我的 keyboardclass.py
def select(value):
if value == "Space":
entry1.insert(tkinter.END, ' ')
elif value == "Backspace":
entry1.delete(len(entry1.get())-1,tkinter.END)
else:
entry1.insert(tkinter.END, value)
root = Tk()
root.configure(background = "cornflowerblue")
root.wm_attributes("-alpha", 0.7)
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']
Row = 2
Column = 0
for alphabet in alphabets:
command = lambda x=alphabet: select(x)
if alphabet != 'Space':
Button(root, text = alphabet,
command = command,width = 5, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = Row, column = Column)
if alphabet == 'Enter':
Button(root, text = alphabet,
command = command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = Row, column = Column, columnspan = 2)
if alphabet == 'Shift':
Button(root, text = alphabet,
command = command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = Row, column = Column, columnspan = 2)
if alphabet == 'Space':
Button(root, text = alphabet,
command = command, width = 130, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = 6, columnspan = 16)
Column +=1
if Column > 13 and Row == 1:
Column = 0
Row += 1
if Column > 13 and Row == 2:
Column = 0
Row +=1
if Column > 13 and Row == 3:
Column = 0
Row +=1
if Column > 12 and Row == 4:
Column = 0
Row +=1
root.mainloop()
keyboard.py
我将代码放在函数 create()
中,它获取 root
和 entry
作为参数 - 所以现在我可以使用 import keyboard
其他文件和 运行 keyboard.create(root, some_entry)
。
它使用 root
创建 window Toplevel()
而不是 Tk()
- 并且没有第二个 mainloop()
.
它使用 entry
在 select(entry, value)
中使用它将使用 entry.insert(...)
if __name__ == "__main__"
中有代码:因此您可以运行将其作为独立程序来测试键盘。
它显示两个Entry
和一个Text
(其中可以测试键Enter
)。
我为 Enter
、Tab
、Backspace
添加了 Text
的代码(这需要不同的方法)。当您按 Shift
或 Caps Lock
时,它会开始插入大写字符 - 但它不会更改 window.
按钮上的文本
在 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
的小部件,因为您经常使用这种组合。也许我稍后会创建它。
> import PySimpleGUI as py
>
> statecolor = ['lightgrey', 'tan', 'yellow', 'lightblue', 'orange',
> 'limegreen', 'pink', 'red', 'darkgray'] py.theme('Python') key_setup =
> [[py.Text('Title')],
> [py.Button('Q'), py.Button('W'), py.Button('E'), py.Button('R'), py.Button('T'), py.Button('Y')],
> [py.Button('U'), py.Button('I'), py.Button('O'), py.Button('P'), py.Button('A'), py.Button('S')],
> [py.Button('D'), py.Button('F'), py.Button('G'), py.Button('H'), py.Button('J'), py.Button('K')],
> [py.Button('L'), py.Button('Z'), py.Button('X'), py.Button('C'), py.Button('V'), py.Button('B')],
> [py.Button('N'), py.Button('Enter'), py.Button('M'), py.Button('Exit', button_color=('White', statecolor[7]), key='Exit')],
> [py.Button('Space', button_color=('Red', statecolor[6]), key='Space')]] window = py.Window('Keyboard', key_setup) while True:
> event, values = window.read()
> if event == 'Q':
> print('Q')
> if event == 'W':
> print('W')
> if event == 'E':
> print('E')
> if event == 'R':
> print('R')
> if event == 'T':
> print('T')
> if event == 'Y':
> print('Y')
> if event == 'U':
> print('U')
> if event == 'I':
> print('I')
> if event == 'O':
> print('O')
> if event == 'P':
> print('P')
> if event == 'A':
> print('A')
> if event == 'S':
> print('S')
> if event == 'D':
> print('D')
> if event == 'F':
> print('F')
> if event == 'G':
> print('G')
> if event == 'H':
> print('H')
> if event == 'J':
> print('J')
> if event == 'K':
> print('K')
> if event == 'L':
> print('L')
> if event == 'Z':
> print('Z')
> if event == 'X':
> print('X')
> if event == 'C':
> print('C')
> if event == 'V':
> print('V')
> if event == 'B':
> print('B')
> if event == 'N':
> print('N')
> if event == 'M':
> print('M')
> if event == 'Enter':
> print(' ')
> if event == 'Exit':
> break
> if event == 'Space':
> print('\b ', end="", flush=True)
> if event == py.WIN_CLOSED:
> exit() window.close()
>
> Blockquote
我在输入 Tkinter 条目时遇到问题。我做了一个注册码,我的代码上的每个条目都会调用一个虚拟键盘,虚拟键盘是分开的 python 程序。我成功调用了另一个 python 程序,但我无法使用我调用的虚拟键盘在我的主程序 Tkinter 条目上键入内容。 有什么办法可以做到这一点? 这是我的代码
def register(self):
self.master_register = Toplevel()
self.first_name = StringVar()
self.middle_name = StringVar()
self.last_name = StringVar()
self.sex = StringVar()
self.birth_day = StringVar()
self.civil_status = StringVar()
self.label_head = Label(self.master_register, text = "Please fill up all informations below")
self.label_head.pack()
self.label_first_name = Label(self.master_register, text = "First Name")
self.label_first_name.pack()
self.entry_first_name = Entry(self.master_register, textvariable = self.first_name)
self.entry_first_name.pack()
self.entry_first_name.bind('<ButtonPress-1>',self.keyboard)
self.label_middle_name = Label(self.master_register, text = "Middle Name")
self.label_middle_name.pack()
self.entry_middle_name = Entry(self.master_register, textvariable = self.middle_name)
self.entry_middle_name.pack()
self.entry_middle_name.bind('<ButtonPress-1>',self.keyboard)
self.label_last_name = Label(self.master_register, text = "Last Name")
self.label_last_name.pack()
self.entry_last_name = Entry(self.master_register, textvariable = self.last_name)
self.entry_last_name.pack()
self.entry_last_name.bind('<ButtonPress-1>',self.keyboard)
self.label_sex = Label(self.master_register, text = "Sex")
self.label_sex.pack()
self.radio_button_sex1 = Radiobutton(self.master_register, text = "Male", variable = self.sex, value = "Male" )
self.radio_button_sex1.pack()
self.radio_button_sex2 = Radiobutton(self.master_register, text = "Female", variable = self.sex, value = "Female")
self.radio_button_sex2.pack()
self.label_birthday = Label(self.master_register, text = "Birth Day")
self.label_birthday.pack()
self.entry_birthday = Entry(self.master_register, textvariable = self.birth_day)
self.entry_birthday.pack()
self.entry_birthday.bind('<ButtonPress-1>',self.keyboard)
self.label_civil_status = Label(self.master_register, text = "Civil Status")
self.label_civil_status.pack()
self.entry_civil_status = Entry(self.master_register, textvariable = self.civil_status)
self.entry_civil_status.pack()
self.entry_civil_status.bind('<ButtonPress-1>',self.keyboard)
self.button_submit = Button(self.master_register, text = "Submit", command = lambda: self.registered(self.first_name, self.middle_name,
self.last_name, self.sex, self.birth_day,
self.civil_status))
self.button_submit.pack()
def keyboard(self, event):
exec(open("keyboardclass.py").read());
这是我的 keyboardclass.py
def select(value):
if value == "Space":
entry1.insert(tkinter.END, ' ')
elif value == "Backspace":
entry1.delete(len(entry1.get())-1,tkinter.END)
else:
entry1.insert(tkinter.END, value)
root = Tk()
root.configure(background = "cornflowerblue")
root.wm_attributes("-alpha", 0.7)
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']
Row = 2
Column = 0
for alphabet in alphabets:
command = lambda x=alphabet: select(x)
if alphabet != 'Space':
Button(root, text = alphabet,
command = command,width = 5, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = Row, column = Column)
if alphabet == 'Enter':
Button(root, text = alphabet,
command = command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = Row, column = Column, columnspan = 2)
if alphabet == 'Shift':
Button(root, text = alphabet,
command = command, width = 15, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = Row, column = Column, columnspan = 2)
if alphabet == 'Space':
Button(root, text = alphabet,
command = command, width = 130, padx=3, pady=3,bd=12,bg = "black", fg="white").grid(row = 6, columnspan = 16)
Column +=1
if Column > 13 and Row == 1:
Column = 0
Row += 1
if Column > 13 and Row == 2:
Column = 0
Row +=1
if Column > 13 and Row == 3:
Column = 0
Row +=1
if Column > 12 and Row == 4:
Column = 0
Row +=1
root.mainloop()
keyboard.py
我将代码放在函数 create()
中,它获取 root
和 entry
作为参数 - 所以现在我可以使用 import keyboard
其他文件和 运行 keyboard.create(root, some_entry)
。
它使用 root
创建 window Toplevel()
而不是 Tk()
- 并且没有第二个 mainloop()
.
它使用 entry
在 select(entry, value)
中使用它将使用 entry.insert(...)
if __name__ == "__main__"
中有代码:因此您可以运行将其作为独立程序来测试键盘。
它显示两个Entry
和一个Text
(其中可以测试键Enter
)。
我为 Enter
、Tab
、Backspace
添加了 Text
的代码(这需要不同的方法)。当您按 Shift
或 Caps Lock
时,它会开始插入大写字符 - 但它不会更改 window.
在 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
的小部件,因为您经常使用这种组合。也许我稍后会创建它。
> import PySimpleGUI as py
>
> statecolor = ['lightgrey', 'tan', 'yellow', 'lightblue', 'orange',
> 'limegreen', 'pink', 'red', 'darkgray'] py.theme('Python') key_setup =
> [[py.Text('Title')],
> [py.Button('Q'), py.Button('W'), py.Button('E'), py.Button('R'), py.Button('T'), py.Button('Y')],
> [py.Button('U'), py.Button('I'), py.Button('O'), py.Button('P'), py.Button('A'), py.Button('S')],
> [py.Button('D'), py.Button('F'), py.Button('G'), py.Button('H'), py.Button('J'), py.Button('K')],
> [py.Button('L'), py.Button('Z'), py.Button('X'), py.Button('C'), py.Button('V'), py.Button('B')],
> [py.Button('N'), py.Button('Enter'), py.Button('M'), py.Button('Exit', button_color=('White', statecolor[7]), key='Exit')],
> [py.Button('Space', button_color=('Red', statecolor[6]), key='Space')]] window = py.Window('Keyboard', key_setup) while True:
> event, values = window.read()
> if event == 'Q':
> print('Q')
> if event == 'W':
> print('W')
> if event == 'E':
> print('E')
> if event == 'R':
> print('R')
> if event == 'T':
> print('T')
> if event == 'Y':
> print('Y')
> if event == 'U':
> print('U')
> if event == 'I':
> print('I')
> if event == 'O':
> print('O')
> if event == 'P':
> print('P')
> if event == 'A':
> print('A')
> if event == 'S':
> print('S')
> if event == 'D':
> print('D')
> if event == 'F':
> print('F')
> if event == 'G':
> print('G')
> if event == 'H':
> print('H')
> if event == 'J':
> print('J')
> if event == 'K':
> print('K')
> if event == 'L':
> print('L')
> if event == 'Z':
> print('Z')
> if event == 'X':
> print('X')
> if event == 'C':
> print('C')
> if event == 'V':
> print('V')
> if event == 'B':
> print('B')
> if event == 'N':
> print('N')
> if event == 'M':
> print('M')
> if event == 'Enter':
> print(' ')
> if event == 'Exit':
> break
> if event == 'Space':
> print('\b ', end="", flush=True)
> if event == py.WIN_CLOSED:
> exit() window.close()
>
> Blockquote