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!

10
  • I have quickly looked through the API. Could you just call self.mouseX and self.mouseY ? Commented Nov 17, 2015 at 11:57
  • i tried that its doesnt work. Commented Nov 19, 2015 at 6: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 Commented Nov 19, 2015 at 6:49
  • i also forgot to give you guys the manual for this library. mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf Commented Nov 19, 2015 at 6: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) Commented Nov 19, 2015 at 6:55

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 depending on what library you are using. Tkinter has """ <B1-Motion> 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 effbot.org/tkinterbook/tkinter-events-and-bindings.htm
@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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.