【问题标题】:im trying to allow a button in tkinter to be pressed using the keyboard key我试图允许使用键盘键按下 tkinter 中的按钮
【发布时间】:2017-01-20 12:10:41
【问题描述】:

这部分代码显示了已知在 tkinter 中使用的按钮

up = Radiobutton(frame1, text="up", variable=rows, value=1,indicatoron=0)#button for movingup
up.grid( row=2,column=0 )

down = Radiobutton(frame1, text="down", variable=rows, value=2,indicatoron=0)#button for moving down
down.grid( row=3,column=0 )


left = Radiobutton(frame1, text="left", variable=cols, value=3,indicatoron=0)#button for moving left
left.grid( row=2,column=4 )
right = Radiobutton(frame1, text="right", variable=cols, value=4,indicatoron=0)#button for moving #right
right.grid( row=3,column=4 )

vertical=Entry(frame1,bd=2,width=10)
vertical.grid(row=4,column=0)


horizontal=Entry(frame1,bd=2,width=10)
horizontal.grid(row=4,column=4)

enter=Button(main,text='Enter',command=move) #displays the move on grid once button is pressed
enter.pack()


label = Label(main)
label.pack()

【问题讨论】:

标签: user-interface button tkinter


【解决方案1】:

您可以将函数绑定到键 - 即。

root.bind('m', move) # key `m`
root.bind('<Control-m>', move) # key `Ctrl+m`
root.bind('<Alt-m>', move) # key `Alt+m`
root.bind('M', move) # key `Shift+m`

但它会用额外的参数执行

move(event) 

所以你也必须用额外的参数来定义函数

def move(event=None)

您可以为event(即None)设置默认值,因此它也适用于command=move

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 2017-01-31
    • 2013-03-14
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多