Python Forum
[xbmc] How to block the code from executed twice under the loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[xbmc] How to block the code from executed twice under the loop
#1
Hi all,
I need some help with my code. I have a problem with running the code through on the loop because when I pressed on the right arrow button of the keyboard, it will make my code executing like twice. Sad

Example: I have got two buttons with two different button size which it is 691 and 167. The variables I use which it have a button size 691 and nextprogram_width have a button size 167. I want to check the button size 691 using with program_width when I checking with the program id under the loop. I have changed the button size from 691 to 344, but on my code it will executed and changed the button size from 691 to 344 and then changed it from 344 to 167. I only want to change the button size from 691 to 344 not to 167.

When I try this:
ACTION_MOVE_RIGHT = 2

def onAction(self, action):
    if action == ACTION_MOVE_Right:
      if allchannels_enabled:
         if self.channels_Index != len(self.program_buttons) - 1:
            self.GoRight()


def GoRight(self):
    for program_id in self.program_id:
        program_width = self.getControl(int(program_id)).getWidth()
        programX = self.getControl(int(program_id)).getX()


        if int(programX) == 375 and int(program_width) == 691:
           programs_width = 344
           self.getControl(int(program_id)).setWidth(int(programs_width))
           program_width = self.getControl(int(program_id)).getWidth()


        elif int(programX) == 375 and int(program_width) == 167:
            print "you are working on 167 button"
            self.getControl(int(program_id)).setLabel(nextprogram_label)
            self.getControl(int(program_id)).setWidth(nextprogram_width)
When I pressed on the right arrow button of the keyboard, it will call the GoRight function and it will find the list of program id from the self.program_id list then find the button size I want to change before it will executed twice when I use to call the GoRight function only once a time. I can't be able to find out what the trouble is or where it is coming from. I believe the trouble is coming from either under the loop or the if statement that I did not write it correctly.

I have tried to use the if and elif statement to try to block the code from executed twice, but it didn't help.


How I can block the code from executed twice to stop the button size from changed twice when I want to change the button size as only once when I use to call the GoRight function at once a time?
Reply
#2
Hello,

did you want:
if action == ACTION_MOVE_Right:
Or:
if action == ACTION_MOVE_RIGHT:
Also,
 You have two functions here, but no code executing anything. Is something missing?
Reply
#3
which it works great, but I cant be able to block the code from excuted twice when I want to change the button using the button ids. Do you know how I can block the code from excuted twice when checking program_width under the loop with two different if statements?
Reply
#4
What gui are you using?
Shouldn't you of posted this in the gui part of the forum?
What does
self.program_id
contain?

Why do you call
self.getControl(int(program_id))
so many times when you could save the control in a variable and just call the methods on it?
ctrl = self.getControl(int(program_id))
Have you tried putting print statements in to see what's happening with the variables inside the loop also put a print statement before the for loop to see if
GoRight
is actually being called more than once.
Reply
#5
Here is the contain for self.program_id:

['3001', '3011', '3021', '3031', '3041', '3051', '3061']
Well I have to call
self.getControl(int(program_id))
so many times under the each if statement because I want to change the button size I want when I check the variable program_width at the same time I use to call the
self.getControl(int(program_id))
to find the id and get the width size I want to get.

I have putting the print inside the loop, it will print like 7 times as I have got 7 program ids that I stored in the self.program_id list. What do you expect me to do to put the print statements with the variables inside the loop?

I have put the print before the loop, it did not called more than one. I cant be able to use the statements before the loop because I need to use the program_id to get the program ids when I stored the list of ids in the self.program_id list.
Reply
#6
Also found to be calling people idiots on stack overflow and loosing temper. Edit: stack overflow post has been deleted before op blew his top.
I wonder if this will happen again

So it looks like the same id is not being loop more than once because the program_id's are unique

The program_id does not change as it goes through the code in the loop so you only need to obtain the control once.

I expect you to debug it properly with print statements as no one else has the ability to run the code to reproduce the error and track down what is happening.
Reply
#7
(Oct-25-2016, 12:11 AM)Yoriz Wrote: Also found to be calling people idiots on stack overflow and loosing temper. Edit: stack overflow post has been deleted before op blew his top.
I wonder if this will happen again

I am calling people idiots on stack overflow because they went downvoted on me when I rewrote the clear explaintion so I gave up and deleted so I use this instead. Please don't try to spoil this for me as I find that you are not treated me fairly.

Did you see my post above?

How I can use these id from the list ['3001', '3011', '3021', '3031', '3041', '3051', '3061'] to input the id in the self.control(int(program_id)) at a time to get the width size and change the size for each id without executing the code twice??
Reply
#8
This doesn't solve your calls twice problem but should get rid of the duplicate calls to obtain the control.
def GoRight(self):
   for program_id in self.program_id:
       ctrl = self.getControl(int(program_id)) # from here on the program_id is the same, so the same control throughout the loop
       program_width = ctrl.getWidth()
 
       if int(ctrl.getX()) != 375:
           continue
       
       if int(program_width) == 691:
           programs_width = 344
           ctrl.setWidth(int(programs_width)) # int not required , an int was assigned to a variable in the previous line
           program_width = ctrl.getWidth() # this is doing nothing unless there is more code we cant see that uses this variable
 
       elif int(program_width) == 167:
           print "you are working on 167 button"
           ctrl.setLabel(nextprogram_label)
           ctrl.setWidth(nextprogram_width)
Reply
#9
I believe we went through this on the old forum. You need to post runnable code which reproduces your problem and is minimal in doing so.
Reply
#10
@Yoriz: Thank you very much for this, I have changed the code and I ran the test, but I still get the same results. It will keep looping when I use the same program ids as it will change the object texts and size from two different objects button where I want to change the size from one of those object size.

Here is what I use:

def GoRight(self):
   for program_id in self.program_id:
       nextprogram = int(program_id) + 1
       nextprogram1 = int(nextprogram) + 1
       nextprogram2 = int(nextprogram1) + 1
       nextprogram3 = int(nextprogram2) + 1
       program_button_1 = self.getControl(int(program_id))
       program_button_2 = self.getControl(int(nextprogram))
       program_button_3 = self.getControl(int(nextprogram1))
       program_button_4= self.getControl(int(nextprogram2))
       program_button_5 = self.getControl(int(nextprogram3))
       program_width = program_button_1.getWidth()
       programX = program_button_1.getX()
       pos_X = program_button_1.getX()
       pos_Y = program_button_1.getY()                  
       nextprogramX = program_button_2.getX()
       nextprogram_width = program_button_2.getWidth()
       nextprogram_label = program_button_2.getLabel()
       nextprogram1X = program_button_3.getX()
       nextprogram1_width = program_button_3.getWidth()
       nextprogram1_label = program_button_3.getLabel()
       nextprogram2X = program_button_4.getX()
       nextprogram2_width = program_button_4.getWidth()
       nextprogram2_label = program_button_4.getLabel()
       nextprogram3X = program_button_5.getX()
       nextprogram3_width = program_button_5.getWidth()
       nextprogram3_label = program_button_5.getLabel()
                        
       if int(ctrl.getX()) != 375:
           continue
        
      
       if int(program_width) == 691:
         programs_width = 344
         program_button_1.setWidth(int(programs_width))
         program_width = program_button_1.getWidth()
         nextprogram_width = program_button_2.getWidth()
         print "catch this goright 1"


       elif int(program_width) >= 342 and int(program_width) <= 344:
         #print "you are working on 342 and 344 button"
         print "catch this goright 2"
         program_button_1.setLabel(nextprogram_label)
         program_button_1.setWidth(nextprogram_width)

         program_button_2.setLabel(nextprogram1_label)
         program_button_2.setWidth(nextprogram1_width)
         program_button_3.setLabel(nextprogram2_label)
         program_button_3.setWidth(nextprogram2_width)
I have found the problem is coming from the loops, because I got three buttons that required to change the texts and size, so it will executing like three times. It would be much easier if I could get a single program id at a time to change the text and size at a time without looping like 3 times.

How I can get the program id at a time as I use this to store the program id?

 
   program_button = [elem.control for elem in self.program_buttons]
    programs_id = list()
    positions_X = list()

    for elem in program_button:
        programs_id.append(elem.getId())
        positions_X.append(elem.getX())
    programs_id = map(str, programs_id)
    posX = map(str, positions_X)


    #Store the list of strings in the lists
    for i in range(len(posX)):
        pos_X = posX[i]

        if pos_X == '375':
            self.program_id.append(programs_id[i]
)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] messagebox is not being executed please help erwinsiuda 2 2,240 Apr-02-2020, 01:56 AM
Last Post: Larz60+
  [Tkinter] Window unresponsive when executed. fawazcode 2 3,749 Sep-11-2017, 12:29 AM
Last Post: Larz60+
  [WxPython] Which def is executed? merlem 10 7,717 Feb-10-2017, 11:28 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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