4

so im a beginner in programming, im taking my first programming course. and we a have a final project where we make a full program that does everything we learned. one of the things we learned is how to use libraries. but i want to make a simple game using the graphics.py(http://mcsp.wartburg.edu/zelle/python/graphics.py) beginner library. but it doesn't have a function that gets me the position of a mouse, every time the program refreshes.

so i need help with incorporating that into the graphics.py library, or tell me a very simple game library

i know there is a pygame library that i could use, but my instructor highly discourages it for beginners. unless there are other very simple game libraries out there, i cant really use them

any help would be appreciated

thank you!

  • I have quickly looked through the API. Could you just call self.mouseX and self.mouseY ? – Bridgekeeper Nov 17 '15 at 11:57
  • i tried that its doesnt work. – justin underland Nov 19 '15 at 06:48
  • im not sure if i explained what im trying to do, i want the x, y coordinates, where ever my mouse hovers on the window that it created – justin underland Nov 19 '15 at 06:49
  • i also forgot to give you guys the manual for this library. http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf – justin underland Nov 19 '15 at 06:52
  • def refreshMouse(self): while True: if self.isClosed(): raise GraphicsError("getMouse in closed window") time.sleep(.1) # give up thread self.mouseX = None self.mouseY = None self.update() x,y = self.toWorld(self.mouseX, self.mouseY) return Point(x,y) – justin underland Nov 19 '15 at 06:55
  • i tried this under the getMouse() function, but it still doesnt work. it only gives me the point on click, but i want the point where every my mouse is hovering on the window – justin underland Nov 19 '15 at 06:56
  • Yeah i understood the question. Could you please provide some other bits from your code so i can see how you are using your library. Update your question with, what your'e code is doing right now, and what you are expecting from your code. – Bridgekeeper Nov 20 '15 at 07:54
  • from graphics import * win = GraphWin("Mousey Mouse", 200, 200) while True: update() point = win.refreshMouse() place = [point.getX(), point.getY()] print(place) – justin underland Nov 21 '15 at 05:36
  • this code is in a new file and in the same folder with the graphics.py. – justin underland Nov 21 '15 at 05:38
  • what im trying to do is have the graphics window return me the value of my mouse point when whenever i hover over the graphics window. but it still only returns a value whenever i click on the window. what im expecting is to give me the point wherever my mouse is hovering over the window while my program is running. – justin underland Nov 21 '15 at 05:41

1 Answers1

6

Ok i had the time too look at the code now and do some quick tests.

If you look at your library, graphics are using tkinter. And since there are no function in the graphics.py that gives you your mouse position without needing a mouse click, you must bind your own event that updates your mouse position.

This is an example on how you can manage this, source:

win.bind('<Motion>', motion)

def motion(event):
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))

Another suggestion is that your code inherits from the GraphWin class. This gives you instant access to all functions within that class. Though, inheritance, that is another question.

Best of luck

Community
  • 1
  • 1
Bridgekeeper
  • 354
  • 2
  • 12
  • Is there some kind of "in between" option? One where as long as the mouse is pressed, it will continue to register? For example, I'd like to be able to draw long lines of cells in my Game of Life program just by click-and-drag, without having to click each individual cell. – Patch May 31 '19 at 13:32
  • 1
    @Patch depending on what library you are using. Tkinter has """ The mouse is moved, with mouse button 1 being held down (use B2 for the middle button, B3 for the right button). The current position of the mouse pointer is provided in the x and y members of the event object passed to the callback. "" source https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm – Bridgekeeper Jun 03 '19 at 11:03
  • 1
    @Patch That's a funny coincidence, I am also working on Game of Life with this library, which is how I chanced upon this question! – Hari5000 Mar 29 '21 at 21:35