how to create a simple menu in python
How to create an interactive text menu in Python?
I'm not sure what to call it. I want to know how to make one of those menus where you use the arrow keys to highlight your options and press enter to accept it.
The questioner acknowledged being unsure how to clearly state what they're after (I know the feeling!) and it is a little while after the question was posted, but in light of their comment suggesting they were after text , I believe something like python-prompt-toolkit, which is used to provide text-based autocomplete in a number of Python projects, may offer a solution.
There is documentation here which has this example of using a WordCompleter:
from prompt_toolkit import prompt from prompt_toolkit.completion import WordCompleter html_completer = WordCompleter(['<html>', '<body>', '<head>', '<title>']) text = prompt('Enter HTML: ', completer=html_completer) print('You said: %s' % text) That produces output like this:
The example above is still relatively like a ComboBox merely rendered in text, but there are ways to produce other styles of menu as shown in the gallery here.
If that isn't sufficient then another option is looking into something that wraps ncurses, like https://bitbucket.org/npcole/npyscreen or http://urwid.org/ .
The answer to your question including the examples you provided is curses. This package is very much dependent on the underlying operating system. So if platform independence is key then you will get issues. There is for example a Windows port UniCurses but your implementation has to handle this switch if necessary.
There are also tools built on top of curses. Four example Urwid.
I personally have some experience with curses and if you have Linux as the underlying system this can be fun if your requirements are robust and simple. Like your menu requirement. I have to say though that the learning curve is pretty steep.
Hope this helps. But given the broad question thus is the only level of detail to provide at this stage.
I think you mean Combobox. Here is a short example based on this and PyQt:
from PyQt4 import QtGui, QtCore import sys class CheckableComboBox(QtGui.QComboBox): def __init__(self): super(CheckableComboBox, self).__init__() self.view().pressed.connect(self.handleItemPressed) self.setModel(QtGui.QStandardItemModel(self)) def handleItemPressed(self, index): item = self.model().itemFromIndex(index) if item.checkState() == QtCore.Qt.Checked: item.setCheckState(QtCore.Qt.Unchecked) else: item.setCheckState(QtCore.Qt.Checked) class Dialog_01(QtGui.QMainWindow): def __init__(self): super(QtGui.QMainWindow,self).__init__() myQWidget = QtGui.QWidget() myBoxLayout = QtGui.QVBoxLayout() myQWidget.setLayout(myBoxLayout) self.setCentralWidget(myQWidget) self.toolbutton = QtGui.QToolButton(self) self.toolbutton.setText('Select Categories ') self.toolmenu = QtGui.QMenu(self) for i in range(3): action = self.toolmenu.addAction("Category " + str(i)) action.setCheckable(True) self.toolbutton.setMenu(self.toolmenu) self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup) myBoxLayout.addWidget(self.toolbutton) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) combo = Dialog_01() combo.show() combo.resize(480,320) sys.exit(app.exec_()) how to create a simple menu in python
Source: https://tipsfordev.com/how-to-create-an-interactive-text-menu-in-python
Posted by: snyderthand1938.blogspot.com

0 Response to "how to create a simple menu in python"
Post a Comment