2

OS: openSUSE Leap 42.1

I'm using Tkinter in python2.7. When I want to get the coordinates of cursor by accessing "event.x_root" and "event.y_root", the values of them are always 0, which means that the cursor is at the top left corner. This happened when using key-press event, but I can get correct value when using mouse button clicking event event.

So I wonder why the values of x_root and y_root are always 0 when using key-press event but they are correct values when using mouse button clicking event.

What's more, I found that the code worked correctly on Windows 8.

The code is as folows:

from Tkinter import *

def onKeyboardEvent(event):
    print event.x_root, event.y_root
    return True

root = Tk()
root.resizable(False, False)
root.title("colorPicker")


root.bind("<Key>", onKeyboardEvent)

root.mainloop()
gdbb
  • 41
  • 4
  • 1
    what's the use of the mouse pointer for a key event? Your mouse could be hundreds of pixels away from the window. – Bryan Oakley Nov 20 '16 at 18:40
  • FWIW it works for me in Ubuntu Yakkety using the stock python. tcl version is 8.6.0. – Stop harming Monica Nov 20 '16 at 21:19
  • Actually I want to write a color picker. Move the cursor to the point I want and press a key to trigger events. It works well on Windows but I don't know why something wrong happened on openSUSE. The code is just a part of it.@BryanOakley – gdbb Nov 21 '16 at 04:54
  • key event should send key information, not information about other objects ie. mouse, widgets, window, etc. You can always bind mouse motion (``) to function which saves mouse position. – furas Nov 21 '16 at 04:56
  • tkinter gets events probably from "windows/display manager" and different Linux distributions may use different "manager" - even one distribution may use different "manager" if you switch between `KDE`, `Gnome`, etc. And even `Gnome/KDE` may use many different "managers". – furas Nov 21 '16 at 05:10
  • maybe try one of answer on http://stackoverflow.com/questions/22925599/mouse-position-python-tkinter – furas Nov 21 '16 at 05:15
  • Wow, the answer from BryanOakley does work. Thank you! You said that this may have something to do with windows/display manager and I think you are right.@furas – gdbb Nov 21 '16 at 05:44
  • Just like furas' saying, I found your answer in another question. That does work. And this may be the windows/display manager's problem. @BryanOakley – gdbb Nov 21 '16 at 05:49
  • Thank you anyway. I will try it in other distribution. Maybe it's the problem of "windows/display manager", just like furas' saying.@Goyo – gdbb Nov 21 '16 at 05:54

0 Answers0