按下 tkinter 按钮时打印到空闲外壳



我正在尝试创建一个显示在屏幕角落的小窗口。窗口上将有一个键盘。我希望能够让计算机认为您在按下窗口中的按钮时按下了键盘上的按钮。这是我到目前为止的代码:

from tkinter import *
top = Tk()
frame1 = Frame(top)
btn = Button(frame1,text="q",command=  'print("q",end="")')
btn2 = Button(frame1,text="w",command= 'print("w",end="")')
btn3 = Button(frame1,text="e",command= 'print("e",end="")')
btn4 = Button(frame1,text="r",command= 'print("r",end="")')
btn5 = Button(frame1,text="t",command= 'print("t",end="")')
btn6 = Button(frame1,text="y",command= 'print("y",end="")')
btn7 = Button(frame1,text="u",command= 'print("u",end="")')
btn8 = Button(frame1,text="i",command= 'print("i",end="")')
btn9 = Button(frame1,text="o",command= 'print("o",end="")')
btn10 = Button(frame1,text="p",command='print("p",end="")')
frame2 = Frame(top)
btn11 = Button(frame2,text="a",command='print("a",end="")')
btn12 = Button(frame2,text="s",command='print("s",end="")')
btn13 = Button(frame2,text="d",command='print("d",end="")')
btn14 = Button(frame2,text="f",command='print("f",end="")')
btn15 = Button(frame2,text="g",command='print("g",end="")')
btn16 = Button(frame2,text="h",command='print("h",end="")')
btn17 = Button(frame2,text="j",command='print("j",end="")')
btn18 = Button(frame2,text="k",command='print("k",end="")')
btn19 = Button(frame2,text="l",command='print("l",end="")')
frame3 = Frame(top)
btn20 = Button(frame3,text="z",command='print("z",end="")')
btn21 = Button(frame3,text="x",command='print("x",end="")')
btn22 = Button(frame3,text="c",command='print("c",end="")')
btn23 = Button(frame3,text="v",command='print("v",end="")')
btn24 = Button(frame3,text="b",command='print("b",end="")')
btn25 = Button(frame3,text="n",command='print("n",end="")')
btn26 = Button(frame3,text="m",command='print("m",end="")')
btnArr1 = [btn,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10]
btnArr2 = [btn11,btn12,btn13,btn14,btn15,btn16,btn17,btn18,btn19]
btnArr3 = [btn20,btn21,btn22,btn23,btn24,btn25,btn26]
for x in range(0,len(btnArr1),1):
btnArr1[x].pack(side=LEFT)
frame1.pack()
for x in range(len(btnArr2)-1,-1,-1):
btnArr2[x].pack(side=RIGHT)
frame2.pack()
for x in range(len(btnArr3)-1,-1,-1):
btnArr3[x].pack(side=RIGHT)
frame3.pack()
top.mainloop()

当我运行它时,它什么也不做。我尝试使用不带单引号的纯代码,并尝试在命令参数中使用exec()语句。两次尝试都失败了。

我正在使用python 3.6.2

如果有人知道为什么会这样,我很想听听你要说什么。谢谢你的时间!

command=选项需要一个函数或其他可调用对象 - 字符串不符合条件。 您可以尝试command=lambda: print("q",end="")为每个按钮创建这样的函数。

最新更新