Hello Guys, im really new here and i am still working on the Basics of Python but i got to a Problem, where i do absolutely have no idea, what is wrong here.
I want to assign a QThread Call to 6 Buttons created in QTDesigner. I use a For-Loop with it, because the Number of the Buttons can be variabel. Because i can not call the QThread from a Lambda call directly, i try to put the calls in a List instead of creating a variable for any of them:
so this give me a list with the following content:
[<__main__.move_stepper_thread(0x1ebe2b286f0) at 0x000001EBE3A0BF80>, <__main__.move_stepper_thread(0x1ebe2b25110) at 0x000001EBE39301C0>, <__main__.move_stepper_thread(0x1ebe2b25050) at 0x000001EBE3899F80>, <__main__.move_stepper_thread(0x1ebe2b24c10) at 0x000001EBE3A5FA80>, <__main__.move_stepper_thread(0x1ebe2b24bd0) at 0x000001EBE3A5FF40>, <__main__.move_stepper_thread(0x1ebe2b24bf0) at 0x000001EBE3A5F440>, <__main__.move_stepper_thread(0x1ebe2b24d50) at 0x000001EBE3A70A80>]
now i have another loop, where i assing the list values to the buttons:
so here all the buttons with name gleis_btn_X (x for number) get connected with call for the QThread and the indexx gives the number from the list.
But here begins my Problem, after the For-Loop, every Button gets the last index of the self.move[] list. When i change the entry self.move[indexx] manually to self.move[0] or self.move[1], the correct entry from the list will be assigned to the button. Even when i create the Buttons all manually like:
It works fine! But when i try to assign the Lambda Call via the For-Loop, every Button get only the last Value of the self.move[] List. But Why?
i already tried it with a 1-Liner on the Lambda Call like:
getattr(self, "gleis_btn_" + str(1)).clicked.connect(lambda: move_stepper_thread(gleis[element], 1000).start())
But in this case when i press the Button on the GUI, it crashes and say "QThread: Destroyed while thread is still running". But when i only do the self.move.start() call it works and the Thread runs.
I hope you can help me here, it drives me crazy, because i do not understand why it will not work... the indexx value increases by 1 in every for-loop and it begins on 0, this is why i added the indexx = element - 1 at the beginning, so that the indexx is always one less then the given range.
Thank you very much and greetings from Germany,
Caliban.
I want to assign a QThread Call to 6 Buttons created in QTDesigner. I use a For-Loop with it, because the Number of the Buttons can be variabel. Because i can not call the QThread from a Lambda call directly, i try to put the calls in a List instead of creating a variable for any of them:
1 2 3 4 5 |
a = number_buttons + 1 self .move = [] print ( self .move) for element in range ( 1 , a): self .move.append(move_stepper_thread(anotherlist[element], 1000 )) |
[<__main__.move_stepper_thread(0x1ebe2b286f0) at 0x000001EBE3A0BF80>, <__main__.move_stepper_thread(0x1ebe2b25110) at 0x000001EBE39301C0>, <__main__.move_stepper_thread(0x1ebe2b25050) at 0x000001EBE3899F80>, <__main__.move_stepper_thread(0x1ebe2b24c10) at 0x000001EBE3A5FA80>, <__main__.move_stepper_thread(0x1ebe2b24bd0) at 0x000001EBE3A5FF40>, <__main__.move_stepper_thread(0x1ebe2b24bf0) at 0x000001EBE3A5F440>, <__main__.move_stepper_thread(0x1ebe2b24d50) at 0x000001EBE3A70A80>]
now i have another loop, where i assing the list values to the buttons:
1 2 3 |
for element in range ( 1 , a): indexx = element - 1 getattr ( self , "gleis_btn_" + str (element)).clicked.connect( lambda : self .move[indexx].start()) |
But here begins my Problem, after the For-Loop, every Button gets the last index of the self.move[] list. When i change the entry self.move[indexx] manually to self.move[0] or self.move[1], the correct entry from the list will be assigned to the button. Even when i create the Buttons all manually like:
1 2 3 |
getattr ( self , "gleis_btn_" + str ( 1 )).clicked.connect( lambda : self .move[ 0 ].start()) getattr ( self , "gleis_btn_" + str ( 2 )).clicked.connect( lambda : self .move[ 1 ].start()) getattr ( self , "gleis_btn_" + str ( 3 )).clicked.connect( lambda : self .move[ 2 ].start()) |
i already tried it with a 1-Liner on the Lambda Call like:
getattr(self, "gleis_btn_" + str(1)).clicked.connect(lambda: move_stepper_thread(gleis[element], 1000).start())
But in this case when i press the Button on the GUI, it crashes and say "QThread: Destroyed while thread is still running". But when i only do the self.move.start() call it works and the Thread runs.
I hope you can help me here, it drives me crazy, because i do not understand why it will not work... the indexx value increases by 1 in every for-loop and it begins on 0, this is why i added the indexx = element - 1 at the beginning, so that the indexx is always one less then the given range.
Thank you very much and greetings from Germany,
Caliban.