PyQt QInputDialog.get*() wrappers

By Hal Canary, 2012-01-17 12:00:00 (link)

#!/usr/bin/env python

import PyQt4.QtGui

def getInt(title, label, mini=-2147483647, maxa=2147483647):
  assert mini <= maxa
  default = min(max(0, mini), maxa)
  (ret, ok) = PyQt4.QtGui.QInputDialog.getInt(
    PyQt4.QtGui.QWidget(), title, label, default, mini, maxa)
  if not ok:
    raise Exception('select error')
  return ret

def getItem(title, label, items):
  (ret, ok) = PyQt4.QtGui.QInputDialog.getItem(
    PyQt4.QtGui.QWidget(), title, label, items)
  if not ok:
    raise Exception('select error')
  return str(ret)

def getFloat(title, label, mini=-2147483647, maxa=2147483647,
       default=None, decimals=1):
  assert mini <= maxa
  if default is None:
    default = min(max(0, mini), maxa)
  (ret, ok) = PyQt4.QtGui.QInputDialog.getDouble(
    PyQt4.QtGui.QWidget(), title, label, default, mini, maxa, decimals)
  if not ok:
    raise Exception('select error')
  return ret

def getItem(title, label, items):
  (ret, ok) = PyQt4.QtGui.QInputDialog.getItem(
    PyQt4.QtGui.QWidget(), title, label, items)
  if not ok:
    raise Exception('select error')
  return str(ret)

def getText(title, label, default=''):
  (ret, ok) = PyQt4.QtGui.QInputDialog.getText(
    PyQt4.QtGui.QWidget(), title, label,
    PyQt4.QtGui.QLineEdit.Normal, default)
  if not ok:
    raise Exception('select error')
  return str(ret)

qApplication = PyQt4.QtGui.QApplication([])

print getItem('getItem()', 'select a color',
  ['red','orange','yellow','green','blue','violet'])
print getInt('getInt()', 'give me a number', 0, 100)
print getFloat('getFloat()', 'give me a number', 0.0, 1.0, None, 2)
print getText('getText()','give me some text','string')

(back)