Tkinter: Maus Events anzeigen...

Code-Stücke können hier veröffentlicht werden.
Antworten
Benutzeravatar
jens
Moderator
Beiträge: 8461
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Dienstag 25. November 2014, 10:05

Code: Alles auswählen

#!/usr/bin/env python


try:
    # Python 3
    import tkinter
except ImportError:
    # Python 2
    import Tkinter as tkinter


event_map = {2: "KeyPress",
    3: "KeyRelease",
    4: "ButtonPress",
    5: "ButtonRelease",
    6: "Motion",
    7: "Enter",
    8: "Leave",
    9: "FocusIn",
    10: "FocusOut",
    12: "Expose",
    15: "Visibility",
    17: "Destroy",
    18: "Unmap",
    19: "Map",
    21: "Reparent",
    22: "Configure",
    24: "Gravity",
    26: "Circulate",
    28: "Property",
    32: "Colormap",
    36: "Activate",
    37: "Deactivate",
    38: "MouseWheel"
}


class TkMainWindow(object):
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title("Example")
        self.root.geometry("+500+300")

        self.text = tkinter.Text()
        self.text.pack(fill=tkinter.BOTH, expand=tkinter.YES)

        for event_name in event_map.values():
            self.root.bind("<%s>" % event_name, self._update_status)

    def _update_status(self, event):
        txt = event_map[int(event.type)]
        msg = "%s [ %sx%s ]\n" % (txt, event.x, event.y)
        self.text.insert(tkinter.INSERT, msg)
        self.text.see(tkinter.INSERT)

    def mainloop(self):
        self.root.mainloop()


if __name__ == "__main__":
    tk_win = TkMainWindow()
    tk_win.mainloop()

Macht ein Fenster auf und füllt das mit allen vorkommenden Events.

Bsp:

Code: Alles auswählen

Map [ ??x?? ]
Configure [ 0x0 ]
Visibility [ ??x?? ]
Configure [ 500x300 ]
FocusIn [ ??x?? ]
Configure [ 0x0 ]
Map [ ??x?? ]
Visibility [ ??x?? ]
Expose [ 0x0 ]
Expose [ 0x0 ]
Enter [ 301x4 ]
Enter [ 301x4 ]
Motion [ 302x15 ]
Motion [ 303x26 ]
Motion [ 308x43 ]
Motion [ 307x38 ]
Motion [ 305x31 ]
Motion [ 303x24 ]
Motion [ 301x17 ]
Motion [ 298x8 ]
Motion [ 294x1 ]
Leave [ 270x-37 ]
ButtonPress [ 207x46 ]
FocusIn [ ??x?? ]
ButtonRelease [ 207x46 ]
KeyPress [ 207x46 ]
KeyRelease [ 207x46 ]

CMS in Python: http://www.pylucid.org
GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Antworten