456mb
Posts: 12
Joined: Sat Jul 23, 2016 1:52 am

Detect mouse click

Wed Jun 27, 2018 7:08 am

Hi i start to create a simple gui, but i test "detect" the mouse with pynput but no luck, and see guides for detect keyboard but no is my case

Code: Select all

from guizero import App, PushButton, Slider
import body

def body_central():
       [color=#40BF00] while[/color]
       '''body.cuerpo_general(a,b) '''
       [color=#00BF00] end while[/color]


app = App(title="TEST", height=480, width=640)
button = PushButton(app, command=body_central, text="CUERPO")
app.display()

you have a idea how capture a click ? i use raspberry pi with gui desktop and the program run here (visual mode)

thanks for you time

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Detect mouse click

Wed Jun 27, 2018 8:39 am

Where would you like to click? Guizero uses Tkinter and will only allow you to detect mouse clicks somewhere inside the GUI window. You can choose anywhere in the GUI window or a specific widget.

Code: Select all

def body_central():
       [color=#40BF00] while[/color]
       '''body.cuerpo_general(a,b) '''
       [color=#00BF00] end while[/color]
This isn't valid python. What is it supposed to do?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Detect mouse click

Wed Jun 27, 2018 8:51 am

A simple example to bind a mouse click to a function

Code: Select all

from guizero import App, PushButton, Slider
#import body

def body_central():
    print("Clicked on CUERPO")
    pass
    """[color=#40BF00] while[/color]
    '''body.cuerpo_general(a,b) '''
    [color=#00BF00] end while[/color]"""

def clickedAnywhere(e):
    print("You clicked anywhere")



app = App(title="TEST", height=480, width=640)
button = PushButton(app, command=body_central, text="CUERPO")
app.tk.bind("<Button-1>",clickedAnywhere)
app.display()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

456mb
Posts: 12
Joined: Sat Jul 23, 2016 1:52 am

Re: Detect mouse click

Wed Jun 27, 2018 10:17 pm

scotty101 wrote:
Wed Jun 27, 2018 8:39 am
Where would you like to click? Guizero uses Tkinter and will only allow you to detect mouse clicks somewhere inside the GUI window. You can choose anywhere in the GUI window or a specific widget.

Code: Select all

def body_central():
       [color=#40BF00] while[/color]
       '''body.cuerpo_general(a,b) '''
       [color=#00BF00] end while[/color]
This isn't valid python. What is it supposed to do?
its the a idea

----------------

I did what the code says, try different ways that the while detects when a click is pressed but I have not had success

Code: Select all

from guizero import App, PushButton, Slider
import body
import threading

estate_mouse = 0

def state_change(e):
	global estate_mouse 
	if estate_mouse == 1:
		estate_mouse = 2

def body_central():
       global estate_mouse 
       estate_mouse = 1
       def callback():
            body.cuerpo_general(A,B)

       while estate_mouse == 1:
            t = threading.Thread(target=callback)
            t.start()

      estate_mouse = 0

app = App(title="TEST", height=480, width=640)
button = PushButton(app, command=body_central, text="CUERPO")
app.tk.bind("<Button-1>",state_change)
app.display()

try this way but the program is frozen
Last edited by 456mb on Wed Jun 27, 2018 11:25 pm, edited 1 time in total.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Detect mouse click

Wed Jun 27, 2018 10:30 pm

I'm sorry your English is pretty bad so I don't know what you want to do. Perhaps post on a forum in your native language.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

456mb
Posts: 12
Joined: Sat Jul 23, 2016 1:52 am

Re: Detect mouse click

Wed Jun 27, 2018 11:25 pm

now my friend is clearer

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Detect mouse click

Thu Jun 28, 2018 8:42 am

Perhaps you can try to describe what you would like to program to do. Aside from detecting the click of a mouse button I have no idea what you are trying to achieve.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Detect mouse click

Thu Jun 28, 2018 2:21 pm

I should have given you this version of the code instead.

Code: Select all

from guizero import App, PushButton, Slider
#import body

def body_central():
    print("Clicked on CUERPO")
    pass
    """[color=#40BF00] while[/color]
    '''body.cuerpo_general(a,b) '''
    [color=#00BF00] end while[/color]"""

def clickedAnywhere(e):
    print("You clicked anywhere")



app = App(title="TEST", height=480, width=640)
button = PushButton(app, command=body_central, text="CUERPO")
app.when_clicked =clickedAnywhere
app.display()
This version correctly uses the guizero "when_clicked" rather than the tkinter "bind" method.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

456mb
Posts: 12
Joined: Sat Jul 23, 2016 1:52 am

Re: Detect mouse click

Fri Jun 29, 2018 5:03 am

thank you very much for your time friend, I will try;)

Return to “Python”