Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 11-25-2020, 01:12 PM   #1
wrench100
Member
wrench100 began at the beginning.
 
Posts: 18
Karma: 10
Join Date: Mar 2018
Device: Kindle
Button triggering automatically

I've just added a new button to my plug-in interface. For some reason, when I open the plug-in, that button's function fires off automatically. Here's my code for defining the button:

Code:
self.unshelve_TSBL1_button = QPushButton(
            'Unshelve from TSBL 1', self)
        self.unshelve_TSBL1_button.clicked.connect(self.doUnshelve('status:TSBL1shelved'))
        self.l.addWidget(self.unshelve_TSBL1_button)
If I comment out the connect line, the interface appears as normal, with the new button showing properly. As soon as I uncomment the line, the doUnshelve routine gets called as soon as I open the plugin.

Any ideas?
wrench100 is offline   Reply With Quote
Old 11-25-2020, 01:25 PM   #2
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,085
Karma: 1948136
Join Date: Aug 2015
Device: Kindle
try this instead

Code:
from functools import partial

self.unshelve_TSBL1_button = QPushButton(
            'Unshelve from TSBL 1', self)
        self.unshelve_TSBL1_button.clicked.connect(partial(self.doUnshelve,'status:TSBL1shelved'))
        self.l.addWidget(self.unshelve_TSBL1_button)
capink is offline   Reply With Quote
Old 11-25-2020, 02:07 PM   #3
wrench100
Member
wrench100 began at the beginning.
 
Posts: 18
Karma: 10
Join Date: Mar 2018
Device: Kindle
That worked! Thanks so much!

If it's not a hassle, could I trouble you for a quick explanation? Or an explanation to read somewhere else?

Thanks again!
wrench100 is offline   Reply With Quote
Old 11-25-2020, 02:21 PM   #4
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,085
Karma: 1948136
Join Date: Aug 2015
Device: Kindle
You are not supposed to call a function when connecting to a siganl, you just reference the function object and it will be called later when the signal is emitted. So when you do it as below (the wrong way), you are calling the function:

Code:
button.clicked.connect(myfunction())
You simply replace it with this:

Code:
button.clicked.connect(myfunction)
Here you are just referencing the function object to be called later when needed.

But what if the function expects additional arguments? how to pass them without calling the function?

Here is where the partial function comes into play, it takes the function and the args and makes new function like object that have the args built inside (sort of) it so that you do not need to supply them.

Simply searching for "python partial" in google will give enough material for further explanation.
capink is offline   Reply With Quote
Old 11-25-2020, 02:23 PM   #5
wrench100
Member
wrench100 began at the beginning.
 
Posts: 18
Karma: 10
Join Date: Mar 2018
Device: Kindle
Great, thanks. I really appreciate the time and explanation. I'll keep reading.
wrench100 is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
K5 Can a Kindle Touch battery die without triggering the "Critical Battery" error? WaseemAlkurdi Kindle Developer's Corner 32 04-17-2021 05:37 AM
Glo Triggering a library refresh automatically anarcat Kobo Developer's Corner 58 10-08-2019 12:17 PM
Note Case Triggering Auto-Sleep when Open Ragamuffyn Onyx Boox 16 07-16-2018 05:32 PM
[KT]Turn store button on search bar to browser button aditya3098 Kindle Developer's Corner 74 05-19-2017 08:33 PM
Phone triggering sleep mode-kobo glo HD Alice98 Kobo Reader 4 03-05-2017 07:11 AM


All times are GMT -4. The time now is 07:56 AM.


MobileRead.com is a privately owned, operated and funded community.