我在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
创建窗口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
的小部件,因为您经常使用此组合。也许以后再创建。