1

Its supposed that you can store the return of a function into variables so the function is called once and when you use the variable you only use the value, so i tried to get the difference in position by calling "GetPosion()" twice in different times,and storing the two values then gets the difference but the difference is always zero !!!!!!!!

my problem is why the function is called one time for the two variables? and correct me if i'm wrong.


pos1=event.GetPosition()

pos2=event.GetPosition()

deff=pos2-pos1

print(pos1, pos2, deff,'\n')

Tariq1948
  • 39
  • 1
  • 6

2 Answers2

0

Since you did not post a MWE is hard to tell why your code is not working as you expect.

One way to calculate the difference between the coordinates of two consecutive left clicks is shown below.

Code with comments:

import wx
class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, title='Mouse Position', 
            pos=(300, 150), size=(320, 250))

        ### To start the x, y variable. self is used so the value is available
        ### for all methods
        self.x = None
        self.y = None

        self.Show(True)

        self.Bind(wx.EVT_LEFT_UP, self.OnClick)

    def OnClick(self, event):

        #### Last click coordinates
        x, y = event.GetPosition()

        if self.x == None:
            #### First click? then just store the value of x, y
            self.x = x
            self.y = y
            print('First click. Click one more time.')
        else:
            #### Not the first click? then calculate the difference and
            #### update self.x and self.y so they always contain the last
            #### set of coordinates
            xd = x - self.x
            yd = y - self.y

            self.x = x
            self.y = y

            print('deltaX: ' + str(xd) + '  deltaY: ' + str(yd))

app = wx.App()
window = MyFrame()
app.MainLoop()
kbr85
  • 1,416
  • 12
  • 27
  • the same is happening in using date time ex: from datetime import datetime t1= datetime.now() t2= datetime.now() print(t1, t2, t2-t1) the answer is zero , why it calls the function one time for both variables?? – Tariq1948 Jun 24 '19 at 13:16
  • 1
    @TariqAbdElghani Can you show a complete minimal working example of your code? Without seen this I can only speculate about why you have this problem. – kbr85 Jun 24 '19 at 13:25
  • i solved the problem of getting the position but the problem was why python run the function and refer to the same return value by all variables ex:`from datetime import datetime import time strat = time.time() t1= datetime.now() t2= datetime.now() t3= datetime.now() t4= datetime.now() t5= datetime.now() print(t1, t2, t3, t4, t5, t5-t1) print('it took:',time.time()-strat) ` both differences are zero in this example – Tariq1948 Jun 24 '19 at 13:37
  • @TariqAbdElghani t1, t2, ... t5 are not zero. They hold the datetime corresponding to the moment they were created. They have different values. Just add time.sleep(1) between them so you can see a bigger difference between their values. Depending on how fast you call datetime.now() and the formatting of the output you can see 0 for the difference. – kbr85 Jun 24 '19 at 13:47
-1

I found the problem its about the minimum difference in time or position that can be expressed so if consecutive lines of code will run in small time if this time is less than that minimum that can be expressed it will show zero execution time or zero difference in time and the same for the position of mouse cursor that's why it gave me zeros. as here and this what i tried and worked

    from datetime import datetime

    import time
    start = time.time()
    for i in range(10000):
        pass
    #with out some delay it will always zero
    print('it took:',time.time()-start)
Tariq1948
  • 39
  • 1
  • 6