how to capture mouse click event in python

Post here if you need help with creating a Graphical User Interface in Python.

how to capture mouse click event in python

Postby bluephoenix » Tue Jan 14, 2014 1:39 pm

Although I post this question in GUI, indeed, I really don't need a GUI as the application background, CLI is also OK for me.
I just don't know how to capture mouse click event in simple way even I checked the documents of Python.
I prefer to use Python 3.3 instead of 2.7.
I know there is a way by using pygame. But I expect a simpler way to implement because the program is not for game for sure.
So who can teach me a little? Thank you so much!
bluephoenix
 
Posts: 4
Joined: Tue Dec 24, 2013 2:48 pm

Re: how to capture mouse click event in python

Postby metulburr » Tue Jan 14, 2014 3:17 pm

I am not aware of a method to catch a mouse click event without an associated window to catch it from a GUI library.

I know there is a way by using pygame. But I expect a simpler way to implement because the program is not for game for sure.

It doesnt *have* to be a game to use pygame. In reality its just another GUI library with tools geared for gaming. Simpler? I mean its less than 15 lines of code.
Code: Select all
import pygame as pg

running = True
pg.init()
screen = pg.display.set_mode((600,400))
while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
        elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
            print('mouse button 1 pressed down')
        elif event.type == pg.MOUSEBUTTONUP and event.button == 1:
            print('mouse button 1 released')
    pg.display.update()

but you have to have the window up and in focus for it to catch the mouse click
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1094
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: how to capture mouse click event in python

Postby bluephoenix » Wed Jan 15, 2014 12:54 pm

Thanks metulburr!

So let me just use pygame to make a program firstly and do the optimization later.

Thank you!
bluephoenix
 
Posts: 4
Joined: Tue Dec 24, 2013 2:48 pm

Re: how to capture mouse click event in python

Postby Yoriz » Wed Jan 15, 2014 1:15 pm

You might beable to just use a package that can do that if you didnt want to use pygame.
I found PyUserInput by searching for mouse on pypi,
it has PyMouseEvent and PyKeyboardEvent.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 565
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests