我目前正在开发一个用户界面,我遇到了以下问题 - 当点击鼠标的右键时,我出现了一个上下文菜单 - 当我在Windows中运行界面时它完全正常,但是当我在Linux中运行它,我尝试单击鼠标右键 - 菜单消失得非常快,我只能在选择我需要的选项时将手指放在按钮上,然后小心地按下它,使用左侧按钮。这是我用于右键菜单的代码:
def rClicker(e):
try:
def rClick_Select(e,apnd=0):
e.widget.event_generate('<Control-a>')
def rClick_Copy(e):
e.widget.event_generate('<Control-c>')
def rClick_Cut(e):
e.widget.event_generate('<Control-x>')
def rClick_Paste(e):
e.widget.event_generate('<Control-v>')
e.widget.focus()
def rClick_Undo(e):
e.widget.event_generate('<Control-z>')
def rClick_Redo(e):
e.widget.event_generate('<Control-y>')
nclst=[
(' Cut ', lambda e=e: rClick_Cut(e)),
(' Copy ', lambda e=e: rClick_Copy(e)),
(' Paste ', lambda e=e: rClick_Paste(e)),
(' Undo ', lambda e=e: rClick_Undo(e)),
(' Redo ', lambda e=e: rClick_Redo(e)),
(' Select All ', lambda e=e: rClick_Select(e))
]
rmenu = Menu(None, tearoff=0, takefocus=0)
for (txt, cmd) in nclst:
rmenu.add_command(label=txt, command=cmd)
rmenu.tk_popup(e.x_root+40, e.y_root+10,entry="0")
except TclError:
print ' - rClick menu, something wrong'
pass
return "break"
def rClickbinder(r):
try:
for b in [ 'Text', 'Entry', 'Listbox', 'Label']: #
r.bind_class(b, sequence='<Button-3>',
func=rClicker, add='')
except TclError:
print ' - rClickbinder, something wrong'
pass
#bind context menu to a specific element
txt.bind('<Button-3>',rClicker, add='')
#or bind it to any Text/Entry/Listbox/Label element
#rClickbinder(master)