Dialogs in PyQt4

::-- zhuyj [2008-06-16 09:48:48]

1. Dialogs in PyQt4

PyQt4中的对话框

Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog is a window which is used to "talk" to the application. A dialog is used to input data, modify data, change the application settings etc. Dialogs are important means of communication between a user and a computer program.

There are essentially two types of dialogs. Predefined dialogs and custom dialogs.

1.1. Predefined Dialogs

预定义对话

   1 #!/usr/bin/python
   2 # inputdialog.py
   3 import sys
   4 from PyQt4 import QtGui
   5 from PyQt4 import QtCore
   6 class InputDialog(QtGui.QWidget):
   7     def __init__(self, parent=None):
   8         QtGui.QWidget.__init__(self, parent)
   9         self.setGeometry(300, 300, 350, 80)
  10         self.setWindowTitle('InputDialog')
  11         self.button = QtGui.QPushButton('Dialog', self)
  12         self.button.setFocusPolicy(QtCore.Qt.NoFocus)
  13         self.button.move(20, 20)
  14         self.connect(self.button, QtCore.SIGNAL('clicked()'), self.showDialog)
  15         self.setFocus()
  16         self.label = QtGui.QLineEdit(self)
  17         self.label.move(130, 22)
  18     def showDialog(self):
  19         text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
  20         if ok:
  21             self.label.setText(unicode(text))
  22 app = QtGui.QApplication(sys.argv)
  23 icon = InputDialog()
  24 icon.show()
  25 app.exec_()

The example has a button and a line edit widget. The button shows the input dialog for getting text values. The entered text will be displayed in the line edit widget.

 text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')

This line displays the input dialog. The first string is a dialog title, the second one is a message within the dialog. The dialog returns the entered text and a boolean value. If we clicked ok button, the boolean value is true, otherwise false.

Input Dialog

Figure: Input Dialog

1.2. QColorDialog

The QColorDialog provides a dialog widget for specifying colors.

   1 #!/usr/bin/python
   2 # colordialog.py
   3 import sys
   4 from PyQt4 import QtGui
   5 from PyQt4 import QtCore
   6 class ColorDialog(QtGui.QWidget):
   7     def __init__(self, parent=None):
   8         QtGui.QWidget.__init__(self, parent)
   9         color = QtGui.QColor(0, 0, 0)
  10         self.setGeometry(300, 300, 250, 180)
  11         self.setWindowTitle('ColorDialog')
  12         self.button = QtGui.QPushButton('Dialog', self)
  13         self.button.setFocusPolicy(QtCore.Qt.NoFocus)
  14         self.button.move(20, 20)
  15         self.connect(self.button, QtCore.SIGNAL('clicked()'), self.showDialog)
  16         self.setFocus()
  17         self.widget = QtGui.QWidget(self)
  18         self.widget.setStyleSheet("QWidget { background-color: %s }"
  19             % color.name())
  20         self.widget.setGeometry(130, 22, 100, 100)
  21     def showDialog(self):
  22         color = QtGui.QColorDialog.getColor()
  23         self.widget.setStyleSheet("QWidget { background-color: %s }"
  24             % color.name())
  25 app = QtGui.QApplication(sys.argv)
  26 cd = ColorDialog()
  27 cd.show()
  28 app.exec_()

The application example shows a push button and a QWidget. The widget background is set to black color. Using the QColorDialog, we can change it's background.

 color = QtGui.QColorDialog.getColor()

This line will pop up the QColorDialog.

self.widget.setStyleSheet("QWidget { background-color: %s }"
     % color.name())

We change the background color using stylesheets.

Color dialog

Figure: Color dialog

1.3. QFontDialog

The QFontDialog is a dialog widget for selecting font.

   1 #!/usr/bin/python
   2 # fontdialog.py
   3 import sys
   4 from PyQt4 import QtGui
   5 from PyQt4 import QtCore
   6 class FontDialog(QtGui.QWidget):
   7     def __init__(self, parent=None):
   8         QtGui.QWidget.__init__(self, parent)
   9         hbox = QtGui.QHBoxLayout()
  10         self.setGeometry(300, 300, 250, 110)
  11         self.setWindowTitle('FontDialog')
  12         button = QtGui.QPushButton('Dialog', self)
  13         button.setFocusPolicy(QtCore.Qt.NoFocus)
  14         button.move(20, 20)
  15         hbox.addWidget(button)
  16         self.connect(button, QtCore.SIGNAL('clicked()'), self.showDialog)
  17         self.label = QtGui.QLabel('Knowledge only matters', self)
  18         self.label.move(130, 20)
  19         hbox.addWidget(self.label, 1)
  20         self.setLayout(hbox)
  21     def showDialog(self):
  22         font, ok = QtGui.QFontDialog.getFont()
  23         if ok:
  24             self.label.setFont(font)
  25 app = QtGui.QApplication(sys.argv)
  26 cd = FontDialog()
  27 cd.show()
  28 app.exec_()

In our example, we have a button and a label. With QFontDialog, we change the font of the label.

 hbox.addWidget(self.label, 1)

We make the label resizable. It is necessary, because when we select a different font, the text may become larger. Otherwise the label might not be fully visible.

 font, ok = QtGui.QFontDialog.getFont()

Here we pop up the font dialog.

 if ok:
     self.label.setFont(font)

If we clicked ok, the font of the label was changed.

Font dialog

1.4. QFileDialog

The QFileDialog is a dialog that allows users to select files or directories. The files can be selected for both opening or saving.

   1 #!/usr/bin/python
   2 # openfiledialog.py
   3 import sys
   4 from PyQt4 import QtGui
   5 from PyQt4 import QtCore
   6 class OpenFile(QtGui.QMainWindow):
   7     def __init__(self, parent=None):
   8         QtGui.QMainWindow.__init__(self, parent)
   9         self.setGeometry(300, 300, 350, 300)
  10         self.setWindowTitle('OpenFile')
  11         self.textEdit = QtGui.QTextEdit()
  12         self.setCentralWidget(self.textEdit)
  13         self.statusBar()
  14         self.setFocus()
  15         exit = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)
  16         exit.setShortcut('Ctrl+O')
  17         exit.setStatusTip('Open new File')
  18         self.connect(exit, QtCore.SIGNAL('triggered()'), self.showDialog)
  19         menubar = self.menuBar()
  20         file = menubar.addMenu('&File')
  21         file.addAction(exit)
  22     def showDialog(self):
  23         filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
  24                     '/home')
  25         file=open(filename)
  26         data = file.read()
  27         self.textEdit.setText(data)
  28 app = QtGui.QApplication(sys.argv)
  29 cd = OpenFile()
  30 cd.show()
  31 app.exec_()

The example shows a menubar, centrally set text edit widget and a statusbar. The statusbar is shown only for desing purposes. The file menu item shows the QFileDialog which is used to select a file. The contents of the file are loaded into the text edit widget.

class OpenFile(QtGui.QMainWindow):
...
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)

The example is based on the QMainWindow widget, because we centrally set the text edit widget. This is easily done with the QMainWindow widget, without resorting to layouts.

 filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file','/home')

We pop up the QFileDialog. The first string in the getOpenFileName method is the caption. The second string specifies the dialog working directory. By default, the file filter is set to All files (*).

 file=open(filename)
 data = file.read()
 self.textEdit.setText(data)

The selected file name is read and the contents of the file are set to the text edit widget.

File Dialog

Figure: File dialog

Dialogs_对话框 (last edited 2009-12-25 07:15:13 by localhost)