如何从存储输入字母的 tkinter 按钮创建键盘?
How to create a keyboard from tkinter buttons that stores the entered letter?
我正在使用 tkinter interactive window 编写一个 python 刽子手代码,其中包括一个键盘。我已经创建了键盘,但是我不知道如何找出用户按下了哪个键盘键以及如何将其作为字母存储在变量中。如果有任何帮助,我将不胜感激!
klavesnice = Tk()
klavesnice.geometry("800x700+120+100")
buttons = []
buttons = [
'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'
]
radek=3 #row
sloupec=0 #column
for button in buttons:
command=lambda x=button: select(x)
if button!='Space':
Button(klavesnice,text=button,width=5,font=("arial",14,"bold"),bg='powder blue',command=command,padx=3.5,pady=3.5,bd=5).grid(row=radek,column=sloupec)
if button=='Space':
Button(klavesnice,text=button,command=command).grid(row=5,column=sloupec)
sloupec+=1
#určení rozložení klávesnice
if sloupec>9 and radek==3:
sloupec=0
radek+=1
if sloupec>8 and radek==4:
sloupec=0
radek+=1
上面的代码是显示键盘的代码,下面的代码是我唯一能想到的,它不会将点击的键保存到变量中。
zadane=""
entry=Text(zadane_rm,width=43,height=3)
entry.grid(row=1,columnspan=40)
def select(value):
if value=='Space':
entry.insert(END,'')
else:
entry.insert(END, value)
我想将点击的字母存储在一个名为 zadane
的字符串变量中。
做你想做的事只需要修改 select()
函数,以便它将当前的 value
参数附加到全局 zadane
字符串变量。请注意,我已重新格式化您的代码以更严格地遵循 PEP 8 - Style Guide for Python Code 指南,使其更具可读性。
import tkinter as tk # Avoid `import *`
klavesnice = tk.Tk()
klavesnice.geometry("800x700+120+100")
buttons = [
'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'
]
zadane = ''
entry = tk.Text(klavesnice, width=43, height=3)
entry.grid(row=1, columnspan=40)
def select(value):
global zadane
if value == 'Space':
entry.insert('end', ' ')
else:
entry.insert('end', value)
zadane = zadane + value
print(f'{zadane=!r}')
radek = 3 #row
sloupec = 0 #column
for button in buttons:
command = lambda x=button: select(x)
if button != 'Space':
tk.Button(klavesnice, text=button, width=5, font=("arial", 14, "bold"),
bg='powder blue', command=command, padx=3.5, pady=3.5, bd=5
).grid(row=radek, column=sloupec)
if button == 'Space':
tk.Button(klavesnice, text=button, command=command).grid(row=5, column=sloupec)
sloupec += 1
# Specify the keyboard layout
if sloupec > 9 and radek == 3:
sloupec = 0
radek += 1
if sloupec > 8 and radek == 4:
sloupec = 0
radek += 1
klavesnice.mainloop()
我正在使用 tkinter interactive window 编写一个 python 刽子手代码,其中包括一个键盘。我已经创建了键盘,但是我不知道如何找出用户按下了哪个键盘键以及如何将其作为字母存储在变量中。如果有任何帮助,我将不胜感激!
klavesnice = Tk()
klavesnice.geometry("800x700+120+100")
buttons = []
buttons = [
'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'
]
radek=3 #row
sloupec=0 #column
for button in buttons:
command=lambda x=button: select(x)
if button!='Space':
Button(klavesnice,text=button,width=5,font=("arial",14,"bold"),bg='powder blue',command=command,padx=3.5,pady=3.5,bd=5).grid(row=radek,column=sloupec)
if button=='Space':
Button(klavesnice,text=button,command=command).grid(row=5,column=sloupec)
sloupec+=1
#určení rozložení klávesnice
if sloupec>9 and radek==3:
sloupec=0
radek+=1
if sloupec>8 and radek==4:
sloupec=0
radek+=1
上面的代码是显示键盘的代码,下面的代码是我唯一能想到的,它不会将点击的键保存到变量中。
zadane=""
entry=Text(zadane_rm,width=43,height=3)
entry.grid(row=1,columnspan=40)
def select(value):
if value=='Space':
entry.insert(END,'')
else:
entry.insert(END, value)
我想将点击的字母存储在一个名为 zadane
的字符串变量中。
做你想做的事只需要修改 select()
函数,以便它将当前的 value
参数附加到全局 zadane
字符串变量。请注意,我已重新格式化您的代码以更严格地遵循 PEP 8 - Style Guide for Python Code 指南,使其更具可读性。
import tkinter as tk # Avoid `import *`
klavesnice = tk.Tk()
klavesnice.geometry("800x700+120+100")
buttons = [
'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'
]
zadane = ''
entry = tk.Text(klavesnice, width=43, height=3)
entry.grid(row=1, columnspan=40)
def select(value):
global zadane
if value == 'Space':
entry.insert('end', ' ')
else:
entry.insert('end', value)
zadane = zadane + value
print(f'{zadane=!r}')
radek = 3 #row
sloupec = 0 #column
for button in buttons:
command = lambda x=button: select(x)
if button != 'Space':
tk.Button(klavesnice, text=button, width=5, font=("arial", 14, "bold"),
bg='powder blue', command=command, padx=3.5, pady=3.5, bd=5
).grid(row=radek, column=sloupec)
if button == 'Space':
tk.Button(klavesnice, text=button, command=command).grid(row=5, column=sloupec)
sloupec += 1
# Specify the keyboard layout
if sloupec > 9 and radek == 3:
sloupec = 0
radek += 1
if sloupec > 8 and radek == 4:
sloupec = 0
radek += 1
klavesnice.mainloop()