龙空技术网

Python GUI 编程 06 : 虚拟事件

超人老嗲 288

前言:

现在兄弟们对“苹果系统python调用虚拟键盘”大致比较关切,各位老铁们都需要学习一些“苹果系统python调用虚拟键盘”的相关资讯。那么小编在网上网罗了一些对于“苹果系统python调用虚拟键盘””的相关资讯,希望你们能喜欢,小伙伴们一起来了解一下吧!

用户定义及系统预定义虚拟事件

import tkinter as tkfrom tkinter import ttk, messageboxroot = tk.Tk()root.title("root")root.state(newstate='iconic') # 最小化主窗体def rquit(virtual_event): # 传入虚拟事件    messagebox.showinfo(message='Quit right now')    # print(root.focus_get()) # 获得焦点的部件    root.quit()root.event_add('<<Quit>>', '<Control-q>')  # 增加虚拟键盘映射root.bind(sequence='<<Quit>>', func=rquit) # 绑定虚拟事件和方法将虚拟事件传入rquit方法toolbar=tk.Menu()root['menu'] = toolbarfiles = tk.Menu(toolbar)toolbar.add_cascade(menu=files, label='files')files.add_command(label="Quit",accelerator='   Ctrl+Q', activebackground='yellow', # 激活背景色    underline=0, activeforeground='red', # 激活字体色        command=root.quit)    files.add_command(label='Cut', accelerator='    Ctrl+X', \    underline=2, \        command=lambda: entry.focus_get().event_generate('<<Cut>>')) # 调用系统预定义事件files.add_command(label='Copy', accelerator='    Ctrl+C', \    underline=0, \        command=lambda: entry.focus_get().event_generate('<<Copy>>')) # 调用系统预定义事件files.add_command(label='Paste', accelerator='    Ctrl+V', \    underline=0, \        command=lambda: entry.focus_get().event_generate('<<Paste>>')) # 调用系统预定义事件(仅当获取焦点时)files.add_command(label='Clear', underline=0, \        command=lambda: entry.focus_get().event_generate('<<Clear>>')) files.add_command(label='Undo', underline=0, \        command=lambda: entry.event_generate('<<Undo>>')) #? 无效?'''系统预定义虚拟事件无需重新绑定触发事件'''def getv(event):    lblvar.set(str.get() + '\n')    print(lblvar.get())str = tk.StringVar()entry=ttk.Entry(root, textvariable=str)entry.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E))#// root.bind('<Return>', lambda event: messagebox.showinfo(message='Scan success!'))root.bind('<Return>', lambda event: str.set(str.get() + '\n'))root.grid_columnconfigure(0, weight=1)lblvar = tk.StringVar(value='default content')lbl = ttk.Label(root, textvariable=lblvar, justify='center', anchor='center',\    wraplength=root.winfo_reqwidth()/3)lbl.grid(column=0, row=1, sticky='news')root.grid_rowconfigure(1, weight=1)root.bind('<KeyPress>', getv)#! 设置系统窗口左上角对应菜单def menuSet():    global win     win = tk.Toplevel(root)    win.title("toplevel")    menubar = tk.Menu(win)    win['menu'] = menubar    sysmenu = tk.Menu(menubar, name='system')    menubar.add_cascade(menu=sysmenu, label='Other...')menuSet()#!Contextual Menus 右键弹出菜单contextual = tk.Menu(root)contextual.add_command(label='Cut', command=lambda: entry.focus_get().event_generate('<<Cut>>'))contextual.add_command(label='Copy', command=lambda: entry.focus_get().event_generate('<<Copy>>'))contextual.add_command(label='Paste', command=lambda: entry.focus_get().event_generate('<<Paste>>'))if root.tk.call('tk','windowingsystem') == 'win32': # 判断系统类型    root.bind('<Button-3>', lambda e: contextual.post(e.x_root,e.y_root))     win.bind('<Button-3>', lambda e: contextual.post(e.x_root,e.y_root))     # x_root/y_root相对事件窗体的坐标,不指定则为相对屏幕坐标else:    messagebox.showerror(message='Unknown System')root.mainloop()
常用的系统预定义事件

<<Clear>>, <<Copy>>, <<Cut>>, <<Paste>>, <<PasteSelection>>,<<PrevWindow>>,

<<Redo>>, and <<Undo>>.

For additional information, see the event command reference.

标签: #苹果系统python调用虚拟键盘