这个简单的例子使用ButtonnearEntry打开Framewith keypad。它还分配Entry给变量target使用哪些按钮将文本放入正确的Entry.
它用于place放置keypad在窗口的中心。
当您单击Entry并将其显示得更近时,打开键盘需要更多的工作Entry。

使用Class您可以创建小部件Keypad。
import tkinter as tk
def create_keypad(root):
keypad = tk.Frame(root)
for y in range(3):
for x in range(3):
val = y*3 + x
text = str(val)
b = tk.Button(keypad, text=text, command=lambda txt=text:insert_text(txt))
b.grid(row=y, column=x, sticky='news')
x = tk.Button(keypad, text='Hide', command=hide_keypad)
x.grid(row=10, column=0, columnspan=3, sticky='news')
return keypad
def insert_text(text):
target.insert('end', text)
def show_keypad(widget):
global target
target = widget
keypad.place(relx=0.5, rely=0.5, anchor='c')
def hide_keypad():
global taget
target = None
keypad.place_forget()
root = tk.Tk()
root.geometry('600x400')
keypad = create_keypad(root)
target = None
f = tk.Frame(root)
f.pack()
e1 = tk.Entry(f)
e1.grid(row=0, column=0)
b1 = tk.Button(f, text='Keypad', command=lambda:show_keypad(e1))
b1.grid(row=0, column=1)
e2 = tk.Entry(f)
e2.grid(row=1, column=0)
b2 = tk.Button(f, text='Keypad', command=lambda:show_keypad(e2))
b2.grid(row=1, column=1)
root.mainloop()
编辑:我把代码放在类中,所以现在它是小部件。它有“清除”、“退格”和内存“复制”、“粘贴”按钮,但它不使用剪贴板。

import tkinter as tk
# --- classes ---
class Keypad(tk.Frame):
cells = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
['0', '.', '-'],
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.target = None
self.memory = ''
for y, row in enumerate(self.cells):
for x, item in enumerate(row):
b = tk.Button(self, text=item, command=lambda text=item:self.append(text))
b.grid(row=y, column=x, sticky='news')
x = tk.Button(self, text='Backspace', command=self.backspace)
x.grid(row=0, column=10, sticky='news')
x = tk.Button(self, text='Clear', command=self.clear)
x.grid(row=1, column=10, sticky='news')
x = tk.Button(self, text='Copy', command=self.copy)
x.grid(row=9, column=0, sticky='news', columnspan='3')
x = tk.Button(self, text='Paste', command=self.paste)
x.grid(row=9, column=10, sticky='news')
self.label = tk.Label(self, text='memory:')
self.label.grid(row=8, column=0, columnspan=11, sticky='news')
x = tk.Button(self, text='Hide', command=self.hide)
x.grid(row=10, column=0, columnspan=11, sticky='news')
def get(self):
if self.target:
return self.target.get()
def append(self, text):
if self.target:
self.target.insert('end', text)
def clear(self):
if self.target:
self.target.delete(0, 'end')
def backspace(self):
if self.target:
text = self.get()
text = text[:-1]
self.clear()
self.append(text)
def copy(self):
#TODO: copy to clipboad
if self.target:
self.memory = self.get()
self.label['text'] = 'memory: ' + self.memory
print(self.memory)
def paste(self):
#TODO: copy from clipboad
if self.target:
self.append(self.memory)
def show(self, entry):
self.target = entry
self.place(relx=0.5, rely=0.5, anchor='c')
def hide(self):
self.target = None
self.place_forget()
# --- main ---
root = tk.Tk()
root.geometry('600x400')
keypad = Keypad(root)
f = tk.Frame(root)
f.pack()
e1 = tk.Entry(f)
e1.grid(row=0, column=0, sticky='news')
b1 = tk.Button(f, text='Keypad', command=lambda:keypad.show(e1))
b1.grid(row=0, column=1, sticky='news')
e2 = tk.Entry(f)
e2.grid(row=1, column=0, sticky='news')
b2 = tk.Button(f, text='Keypad', command=lambda:keypad.show(e2))
b2.grid(row=1, column=1, sticky='news')
root.mainloop()