Python Forum
[HELP] Nested conditional? double condition followed by another condition.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[HELP] Nested conditional? double condition followed by another condition.
#11
(May-31-2020, 05:39 PM)DreamingInsanity Wrote: I did some testing. If you use 'GetAsyncKeyState' it should work. 'GetAsyncKeyState' is basically 'isKeyPressed' so the moment you let go of a button, it will no longer return true.

def button1_pressed():  # Returns true if the left mouse button is pressed
    button1_state = win32api.GetAsyncKeyState(0x01)
    return bool(button1_state)
   
def button2_pressed():  # Returns true if the right mouse button is pressed
    button2_state = win32api.GetAsyncKeyState(0x02)
    return  bool(button2_state)
   
def button3_pressed():  # Returns true if the M button is pressed
    button3_state = win32api.GetAsyncKeyState(0x4D)
    return bool(button3_state)
   
while True:
	while button1_pressed() and button2_pressed(): # No matter which one is pressed first to me
		print("do some")
		while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
			print("do some2")
			if button2_pressed() == 0:
				break # this only breaks the inner while loop
		break # added an extra break else it would just be stuck in an endless loop

Now it work like my old code:

1 - if user press "button1" and "button2" then "press button3"
Its ok to me "do some 2"

or

2 - if user "press button2" and "button1" then "press button3"
Its ok to me "do some 2"

but

3 - if user press "button"3 before "button1" and "button2"
Its not ok to "do some2", I need just to "do some"

The sequence of which button is pressed first matters to my purpose.. Can you help me with that?
Reply
#12
(May-31-2020, 05:44 PM)penahuse Wrote: Now it work like my old code:

1 - if user press "button1" and "button2" then "press button3"
Its ok to me "do some 2"

or

2 - if user "press button2" and "button1" then "press button3"
Its ok to me "do some 2"

but

3 - if user press "button"3 before "button1" and "button2"
Its not ok to "do some2", I need just to "do some"

The sequence of which button is pressed first matters to my purpose.. Can you help me with that?
Not sure what to say - are you sure you are using the right code? On my machine, if I hold down 'm' without holding down button 1 and/or button 2, it doesn't print anything.
I have to hold down both buttons before holding 'm' will do anything.
Reply
#13
(May-31-2020, 07:02 PM)DreamingInsanity Wrote:
(May-31-2020, 05:44 PM)penahuse Wrote: Now it work like my old code:

1 - if user press "button1" and "button2" then "press button3"
Its ok to me "do some 2"

or

2 - if user "press button2" and "button1" then "press button3"
Its ok to me "do some 2"

but

3 - if user press "button"3 before "button1" and "button2"
Its not ok to "do some2", I need just to "do some"

The sequence of which button is pressed first matters to my purpose.. Can you help me with that?
Not sure what to say - are you sure you are using the right code? On my machine, if I hold down 'm' without holding down button 1 and/or button 2, it doesn't print anything.
I have to hold down both buttons before holding 'm' will do anything.

Yes, Im using your code, its right: if I hold M without button 1 and button 2 its suposed to do nothing indeed.

If I presse button 1 + button 2 the output is "do some"

But if you press M then press button 1 and button 2 too (so M + button 1+ button2 are pressed right now) the output is "do some 2" but I need just "do some" because M was pressed before other buttons.

Different is if I press buttom 1 + 2 and after buttom M, in this case i want "do some 2"
Reply
#14
(May-31-2020, 07:08 PM)penahuse Wrote: Yes, Im using your code, its right: if I hold M without button 1 and button 2 its suposed to do nothing indeed.

If I presse button 1 + button 2 the output is "do some"

But if you press M then press button 1 and button 2 too (so M + button 1+ button2 are pressed right now) the output is "do some 2" but I need just "do some" because M was pressed before other buttons.

Different is if I press buttom 1 + 2 and after buttom M, in this case i want "do some 2"
Right now I understand - sorry!

I hope this works:
import win32api
def button1_pressed():  # Returns true if the left mouse button is pressed
    button1_state = win32api.GetAsyncKeyState(0x01)
    return bool(button1_state)
    
def button2_pressed():  # Returns true if the right mouse button is pressed
    button2_state = win32api.GetAsyncKeyState(0x02)
    return  bool(button2_state)
    
def button3_pressed():  # Returns true if the M button is pressed
    button3_state = win32api.GetAsyncKeyState(0x4D)
    return bool(button3_state)
    
while True:
    while button1_pressed() and button2_pressed() and not button3_pressed(): # No matter which one is pressed first to me
        print("do some")
        while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
            print("do some2")
            if button2_pressed() == 0:
                break # this only breaks the inner while loop
        break # added an extra break else it would just be stuck in an endless loop
it's hard to test since if I hold down 'm' it just types a load of m's so I don't know if it's working.
Reply
#15
(May-31-2020, 08:00 PM)DreamingInsanity Wrote:
(May-31-2020, 07:08 PM)penahuse Wrote: Yes, Im using your code, its right: if I hold M without button 1 and button 2 its suposed to do nothing indeed.

If I presse button 1 + button 2 the output is "do some"

But if you press M then press button 1 and button 2 too (so M + button 1+ button2 are pressed right now) the output is "do some 2" but I need just "do some" because M was pressed before other buttons.

Different is if I press buttom 1 + 2 and after buttom M, in this case i want "do some 2"
Right now I understand - sorry!

I hope this works:
import win32api
def button1_pressed():  # Returns true if the left mouse button is pressed
    button1_state = win32api.GetAsyncKeyState(0x01)
    return bool(button1_state)
    
def button2_pressed():  # Returns true if the right mouse button is pressed
    button2_state = win32api.GetAsyncKeyState(0x02)
    return  bool(button2_state)
    
def button3_pressed():  # Returns true if the M button is pressed
    button3_state = win32api.GetAsyncKeyState(0x4D)
    return bool(button3_state)
    
while True:
    while button1_pressed() and button2_pressed() and not button3_pressed(): # No matter which one is pressed first to me
        print("do some")
        while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
            print("do some2")
            if button2_pressed() == 0:
                break # this only breaks the inner while loop
        break # added an extra break else it would just be stuck in an endless loop
it's hard to test since if I hold down 'm' it just types a load of m's so I don't know if it's working.

Mate, you almost got it..

now if button M is not pressed it do nothing but if the the M is pressed before button1 and button2 it need to "do some"

right now with this code if I presse M before button 1 and button 2 nothing happens
Reply
#16
You need an extra variable to keep track of if do_some was running first or not.
I have tried creating it with tkinter does this give the functionality you are looking for
import tkinter as tk


class Toggle_button(tk.Button):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.button_name = kwargs['text']
        self.button_on = False
        self.set_button_state()
        self.bind('<Button-1>', self.on_button)

    def on_button(self, event):
        self.button_on = not self.button_on
        self.set_button_state()

    def set_button_state(self):
        button_state = 'On'
        if not self.button_on:
            button_state = 'Off'
        self.configure(text=f'{self.button_name} {button_state}')


class running_label(tk.Label):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.label_name = kwargs['text']
        self.running(False)

    def running(self, is_running=True):
        running_state = 'Running'
        if not is_running:
            running_state = 'Not Running'
        self.configure(text=f'{self.label_name}: {running_state}')


class MainFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.button1 = Toggle_button(self, text='Button1')
        self.button1.pack(pady=5)
        self.button2 = Toggle_button(self, text='Button2')
        self.button2.pack(pady=5)
        self.button3 = Toggle_button(self, text='Button3')
        self.button3.pack(pady=5)
        self.do_some_label = running_label(self, text='do some')
        self.do_some_label.pack(pady=5)
        self.do_some2_label = running_label(self, text='do some2')
        self.do_some2_label.pack(pady=5)
        self.pack()
        self.do_some_first = False
        self.update_state()

    def update_state(self):
        do_some = self.button1.button_on and self.button2.button_on
        do_some2 = self.button3.button_on

        if do_some and not do_some2:
            self.do_some_first = True
        elif not do_some:
            self.do_some_first = False

        if not self.do_some_first:
            do_some2 = False

        self.do_some_label.running(do_some)
        self.do_some2_label.running(do_some2)

        self.after(500, self.update_state)


if __name__ == '__main__':
    app = tk.Tk()
    main_frame = MainFrame()
    app.mainloop()
Reply
#17
(May-31-2020, 08:08 PM)penahuse Wrote: Mate, you almost got it..

now if button M is not pressed it do nothing but if the the M is pressed before button1 and button2 it need to "do some"

right now with this code if I presse M before button 1 and button 2 nothing happens
Hopefully the last time now!
def button1_pressed():  # Returns true if the left mouse button is pressed
    button1_state = win32api.GetAsyncKeyState(0x01)
    return bool(button1_state)
     
def button2_pressed():  # Returns true if the right mouse button is pressed
    button2_state = win32api.GetAsyncKeyState(0x02)
    return  bool(button2_state)
     
def button3_pressed():  # Returns true if the M button is pressed
    button3_state = win32api.GetAsyncKeyState(0x4D)
    return bool(button3_state)

isMPressed = False    
while True:
	isMPressed = True if button3_pressed() else False
	while button1_pressed() and button2_pressed() and not isMPressed:
		isMPressed = False
		print("do some")
		while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
			print("do some2")
			if button2_pressed() == 0:
				break # this only breaks the inner while loop
In theory this should work because it checks if 'M' is pressed before it checks if the mouse is pressed. If 'M' is pressed then is stops the while loop from running. However, if 'M' isn't pressed but the mouse is, then it goes into the second while loop which sets 'isMPressed' to false because now it doesn't matter if 'M' is pressed because the mouse already is.
Reply
#18
(May-31-2020, 08:22 PM)DreamingInsanity Wrote:
(May-31-2020, 08:08 PM)penahuse Wrote: Mate, you almost got it..

now if button M is not pressed it do nothing but if the the M is pressed before button1 and button2 it need to "do some"

right now with this code if I presse M before button 1 and button 2 nothing happens
Hopefully the last time now!
def button1_pressed():  # Returns true if the left mouse button is pressed
    button1_state = win32api.GetAsyncKeyState(0x01)
    return bool(button1_state)
     
def button2_pressed():  # Returns true if the right mouse button is pressed
    button2_state = win32api.GetAsyncKeyState(0x02)
    return  bool(button2_state)
     
def button3_pressed():  # Returns true if the M button is pressed
    button3_state = win32api.GetAsyncKeyState(0x4D)
    return bool(button3_state)

isMPressed = False    
while True:
	isMPressed = True if button3_pressed() else False
	while button1_pressed() and button2_pressed() and not isMPressed:
		isMPressed = False
		print("do some")
		while button3_pressed(): # Only if it is pressed after button1 and button2 are pressed
			print("do some2")
			if button2_pressed() == 0:
				break # this only breaks the inner while loop
In theory this should work because it checks if 'M' is pressed before it checks if the mouse is pressed. If 'M' is pressed then is stops the while loop from running. However, if 'M' isn't pressed but the mouse is, then it goes into the second while loop which sets 'isMPressed' to false because now it doesn't matter if 'M' is pressed because the mouse already is.

The idea was good, but got the same result as your last try. If M is pressed before button1 and button2 nothing happens. The rest, however is working like expected!

(May-31-2020, 08:13 PM)Yoriz Wrote: You need an extra variable to keep track of if do_some was running first or not.
I have tried creating it with tkinter does this give the functionality you are looking for
import tkinter as tk


class Toggle_button(tk.Button):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.button_name = kwargs['text']
        self.button_on = False
        self.set_button_state()
        self.bind('<Button-1>', self.on_button)

    def on_button(self, event):
        self.button_on = not self.button_on
        self.set_button_state()

    def set_button_state(self):
        button_state = 'On'
        if not self.button_on:
            button_state = 'Off'
        self.configure(text=f'{self.button_name} {button_state}')


class running_label(tk.Label):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.label_name = kwargs['text']
        self.running(False)

    def running(self, is_running=True):
        running_state = 'Running'
        if not is_running:
            running_state = 'Not Running'
        self.configure(text=f'{self.label_name}: {running_state}')


class MainFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.button1 = Toggle_button(self, text='Button1')
        self.button1.pack(pady=5)
        self.button2 = Toggle_button(self, text='Button2')
        self.button2.pack(pady=5)
        self.button3 = Toggle_button(self, text='Button3')
        self.button3.pack(pady=5)
        self.do_some_label = running_label(self, text='do some')
        self.do_some_label.pack(pady=5)
        self.do_some2_label = running_label(self, text='do some2')
        self.do_some2_label.pack(pady=5)
        self.pack()
        self.do_some_first = False
        self.update_state()

    def update_state(self):
        do_some = self.button1.button_on and self.button2.button_on
        do_some2 = self.button3.button_on

        if do_some and not do_some2:
            self.do_some_first = True
        elif not do_some:
            self.do_some_first = False

        if not self.do_some_first:
            do_some2 = False

        self.do_some_label.running(do_some)
        self.do_some2_label.running(do_some2)

        self.after(500, self.update_state)


if __name__ == '__main__':
    app = tk.Tk()
    main_frame = MainFrame()
    app.mainloop()

I will try to understand it before test because your code is much levels above my knowledge level (and probably above my inteligence too)
Reply
#19
If this doesn't work I'm sorry I couldn't help! But although I keep saying, in theory this should work.
import win32api

def button1_pressed():  # Returns true if the left mouse button is pressed
    button1_state = win32api.GetAsyncKeyState(0x01)
    return bool(button1_state)
     
def button2_pressed():  # Returns true if the right mouse button is pressed
    button2_state = win32api.GetAsyncKeyState(0x02)
    return  bool(button2_state)
     
def button3_pressed():  # Returns true if the M button is pressed
    button3_state = win32api.GetAsyncKeyState(0x4D)
    return bool(button3_state)

isMPressed = False    
while True:
	isMPressed = True if button3_pressed() else False
	while button1_pressed() and button2_pressed():
		print("do some")
		while button3_pressed() and not isMPressed: # Only if it is pressed after button1 and button2 are pressed
			isMPressed = False
			print("do some2")
			if button2_pressed() == 0:
				break # this only breaks the inner while loop 
It's a simple variation from last time however if you hold down 'M' then the mouse buttons, rather than printing nothing, it will print 'do some'.

I don't know if this will be a issue to you but if you are holding down 'M' then button 1 and button 2, it will print 'do some'. If you let go of 'M' and press it again, it will print 'do some' rather than 'do some2'. This may be how you wan't it to work, I'm not sure.
Reply
#20
(May-31-2020, 09:10 PM)DreamingInsanity Wrote: If this doesn't work I'm sorry I couldn't help! But although I keep saying, in theory this should work.
import win32api

def button1_pressed():  # Returns true if the left mouse button is pressed
    button1_state = win32api.GetAsyncKeyState(0x01)
    return bool(button1_state)
     
def button2_pressed():  # Returns true if the right mouse button is pressed
    button2_state = win32api.GetAsyncKeyState(0x02)
    return  bool(button2_state)
     
def button3_pressed():  # Returns true if the M button is pressed
    button3_state = win32api.GetAsyncKeyState(0x4D)
    return bool(button3_state)

isMPressed = False    
while True:
	isMPressed = True if button3_pressed() else False
	while button1_pressed() and button2_pressed():
		print("do some")
		while button3_pressed() and not isMPressed: # Only if it is pressed after button1 and button2 are pressed
			isMPressed = False
			print("do some2")
			if button2_pressed() == 0:
				break # this only breaks the inner while loop 
It's a simple variation from last time however if you hold down 'M' then the mouse buttons, rather than printing nothing, it will print 'do some'.

I don't know if this will be a issue to you but if you are holding down 'M' then button 1 and button 2, it will print 'do some'. If you let go of 'M' and press it again, it will print 'do some' rather than 'do some2'. This may be how you wan't it to work, I'm not sure.

If you let go of 'M' and press it again, it will print 'do some' rather than 'do some2'.

Yes it woulb be a problem, Is there a way to fix it?

If I hold M + button 1 + button 2 (in order) than realease M and press again it would be great if print "do some2".

I did not thougth it before, my bad again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 288 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  unable to remove all elements from list based on a condition sg_python 3 454 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 747 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Sent email based on if condition stewietopg 1 869 Mar-15-2023, 08:54 AM
Last Post: menator01
  Replacing values ​​in Mysql with a condition stsxbel 0 642 Mar-05-2023, 08:20 PM
Last Post: stsxbel
  create new column based on condition arvin 12 2,255 Dec-13-2022, 04:53 PM
Last Post: jefsummers
Question Running an action only if time condition is met alexbca 5 1,320 Oct-27-2022, 02:15 PM
Last Post: alexbca
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 849 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to write the condition for deleting multiple lines? Lky 3 1,148 Jul-10-2022, 02:28 PM
Last Post: Lky
  Can I check multi condition for 1 item in a easy way? korenron 4 1,577 May-01-2022, 12:43 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020