Enrico Gherardo
Enrico Gherardo

Reputation: 136

python TUI popup

I need some hints to find a simple solution for inserting a popup window inside a python console app.
This app runs normally unattended, because it's done to be launched from crontab.
It uses everywhere logging to display messages and save them to logfiles.
However, in some cases, the app needs user intervention to choose some options when it is not able to find a suitable one.
That's why I inserted a --interactive option in argparse, and when the app needs user intervention, a popup window in console should appear, allowing the user to choose between some items in a list.
Here's an extract of the output to give you an example :

INFO    : Try to fuzzy-match 'Orange Itbn'
INFO    : Fuzzy-matched alternative entries : ['Orange Is The New Black']
INFO    : Fuzzy matched 'Orange Itbn' as seriesname 'Orange Is The New Black'
INFO    : MOVE /Users/spadazz/testing/orange itbn.s03e10.hdtv.720p.mkv TO:
/Volumes/NAS/TV Shows/Orange Is The New Black/S03/Orange Is The New Black.S03E10.hdtv.720p.mkv
INFO    : Try to fuzzy-match 'Sur'
INFO    : Fuzzy-matched alternative entries : ['Survivors 2008', 'Survivors']
WARNING : 
                    Series 'Sur' not uniquely matched in titles
                    Choose between these titles :
                    ['Survivors 2008', 'Survivors']

WARNING : 
                        ******************************************
                        **** INSERT HERE THE CALL TO THE POPUP ***
                        ******************************************

Now, I've read some documentation about tkinter, curses and npyscreen but I wasn't able to come up with something simple for this purpose.
I don't wanna mess with the app structure or put the log messages in a main window..
I just wanna a popup that allows me to choose between some options, even with a simple keypress like '1' and '2' etc...
This should be a python solution too, possibly without calling external commands from os.

Any ideas ??

Thanks

Upvotes: 2

Views: 1313

Answers (2)

user2449525
user2449525

Reputation: 168

Since npyscreen was written to make that kind of thing really simple, I'd use npyscreen. :)

The example code here is almost exactly what you are asking for.

Upvotes: 0

Enrico Gherardo
Enrico Gherardo

Reputation: 136

With a little help from Nicholas Cole, who wrote npyscreen, I was able to fix this :

import npyscreen as np

class myPop(np.NPSApp):

def setopt(self, title, oList, multi):
    self.title = title
    self.options = oList
    self.multi = multi
    self.height = len(self.options)+1

def main(self):
    F = np.Popup(name="Choose an option")
    if self.multi:
        opt = F.add(np.TitleMultiSelect, name=self.title, max_height=self.height, values=self.options, scroll_exit=True)
    else:
        opt = F.add(np.TitleSelectOne, name=self.title, max_height=self.height, values=self.options, scroll_exit=True)
    F.edit()
    self._values = opt.get_selected_objects()
    self.result = ( self._values if self.multi and len(self._values) > 1 else self._values[0] )

def ChooseOption(title, oList, multi=False):

    pop = myPop()
    pop.setopt(title, oList, multi)
    pop.run()
    return pop.result

# Show a popup with radiobuttons to select 1 item from a list
print ChooseOption('choose a single element', ['a','b','c','d'])

# Show a popup with radiobuttons to multi-select items from a list
print ChooseOption('choose multi-elements', ['a','b','c','d'], True)

Hope this helps. Enrico

Upvotes: 2

Related Questions