python - Mac OS X 中的绑定(bind)键 - Tkinter

标签 python macos tkinter tk-toolkit key-bindings

我有一个在 Ubuntu 系统中完美运行的代码。这只是基于它的最小代码:

import tkinter as Tk
import tkFileDialog 
import ttk

class AppGUI(Tk.Frame):

    def __init__(self, parent):  
        self.ctrl = False
        self.parent = parent
        self.centerWindow()
        self.initGUI()
        self.plot()

    def initGUI(self):
        # Notebooks
        self.notebook  = ttk.Notebook(self.parent)
        self.frame_one = Tk.Frame(self.notebook)   # first page
        self.frame_two = Tk.Frame(self.notebook)   # second page
        self.notebook.add(self.frame_one, text='Notebook 1')
        self.notebook.add(self.frame_two, text='Notebook 2')
        self.notebook.pack(side = Tk.TOP, fill="both", expand=True)

        # Realization frame
        self.out_frame = Tk.Frame(self.parent, bd=1, relief=Tk.SUNKEN)
        self.out_frame.pack(side=Tk.TOP, fill="both", expand=True)

        # Key events
        self.key()

    def centerWindow(self):
        w = 800
        h = 800

        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        x = (sw - w)/2
        y = (sh - h)/2
        self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))


    def key(self):
        # Key events
        def press(event):
            self.ctrl = True
        def release(event):
            self.ctrl = False

        self.parent.bind('<Control_L>', press)
        self.parent.bind('<KeyRelease-Control_L>', release)

    def plot1(self):
        f = plt.figure(figsize=(1, 1), dpi=300)
        c_plot1 = FigureCanvasTkAgg(f, master=self.frame_one)
        c_plot1.show()
        c_plot1.pack(side=Tk.LEFT, fill="both", expand=True)

        def onClickPlot1(event):
            if event.inaxes is not None:
                if self.ctrl:
                    print "Control key pressed"
                    # Do my stuff

        c_plot1.mpl_connect('button_press_event', onClickPlot1)

    def plot2(self):
        # Same stuff for plot 2

    def plot(self):
        self.plot1()
        self.plot2()


def main():
    root = Tk.Tk()
    app = AppGUI(root)
    root.mainloop()  


if __name__ == '__main__':
    main()

当我在 Ubuntu 机器上运行时,我可以看到我按下的任何 Control 键(左或右控制)都已正确绑定(bind),因为我看到了打印和预期的视觉结果。另一方面,在 Mac 中运行相同的代码,我看不到打印输出,当然,预期的结果也不会发生。

我关注了this执行装订的文档。我不想使用 Mac Command 键,只是使用常规的左侧 Control

不同的系统是否有不同的出价代码?这没有道理,但我找不到问题所在。

最佳答案

共有三种不同类型的 key :

  1. 普通键;例如:s、a、1、9 和 ...

  2. 标点符号键;例如 '左' , '上', 'bracketright'(']') , ! 、% 和...

  3. 特殊键;例如:

    • 'Meta_L'(Mac 中的命令)
    • “Alt_L”(Mac 中的 alt)
    • “Control_L”(在 Mac 中为 ctrl)
    • 'super_L'(Mac 中的 fn)
    • 'Shift_L(Mac 中的 Shift)
    • “Caps_Lock”
    • Fn+F2 等组合键也是标点符号键。
# bind and show a key press event with Tkinter
from tkinter import *
root = Tk()
prompt = '      Press any key      '
label1 = Label(root, text=prompt, width=len(prompt), bg='yellow')
label1.pack()
def key(event):
    if event.char == event.keysym:
        msg = 'Normal Key %r' % event.char
    elif len(event.char) == 1:
        msg = 'Punctuation Key %r (%r)' % (event.keysym, event.char)
    else:
        msg = 'Special Key %r' % event.keysym
    label1.config(text=msg)
root.bind_all('<Key>', key)
root.mainloop()

上面的代码说明了一切;)

关于python - Mac OS X 中的绑定(bind)键 - Tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41900436/

相关文章:

Python - 减少颜色条主要刻度大小

Python Numpy FFT -或- RFFT 来查找波的周期而不是频率?

android - 在 OS X v10.6 Snow Leopard 上安装 Eclipse 和设置 Android ADT

python - _tkinter.Tcl错误: can't pack when trying to add ttkcalendar into tkinter GUI

python - 如何以线程安全的方式快速更新列表列表?

python - 根据顶级域(edu、com、org、in)对列表进行排序

objective-c - 向 MainWindow 添加 View 或窗口

macos - 无法连接到 Neo

无法调用 Python tkinter 标签

python - 将 Python 记录器的输出重定向到 tkinter 小部件