Is there a simple way to create a spinbox in PyQt4 whose varying values are strings? I want to create a spinbox which varies between 'low', 'medium' and 'high'. I've looked around and can't find anything that doesn't look very complicated. In Tkinter it might look trivially like:
self.my_wordy_spinbox = Spinbox(parent,
values = ['Medium', 'High', 'Low'],
textvariable = self.object_strength)
I noticed in the documentation it said "QSpinBox is designed to handle integers and discrete sets of values (eg, month names)" which I thought might lead to strings but I couldn't find any more on that. I'm tempted to use a combobox instead but I feel that the spinbox would be more appropriate.
I don't really see how this would be in any way an improvement over a combo-box, especially if there's more than a few strings.
However, if you really want it, you just need to subclass QSpinBox
and reimplement textFromValue and valueFromText :
class StringBox(QtGui.QSpinBox):
def __init__(self, strings, parent=None):
super(StringBox, self).__init__(parent)
self.setStrings(strings)
def strings(self):
return self._strings
def setStrings(self, strings):
self._strings = tuple(strings)
self._values = dict(zip(strings, range(len(strings))))
self.setRange(0, len(strings) - 1)
def textFromValue(self, value):
return self._strings[value]
def valueFromText(self, text):
return self._values[text]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.