Редактирование: PyQt FAQ Dialogs
Материал из Wiki.crossplatform.ru
Внимание: Вы не представились системе. Ваш IP-адрес будет записан в историю изменений этой страницы.
Правка может быть отменена. Пожалуйста, просмотрите сравнение версий, чтобы убедиться, что это именно те изменения, которые вас интересуют, и нажмите «Записать страницу», чтобы изменения вступили в силу.
Текущая версия | Ваш текст | ||
Строка 1: | Строка 1: | ||
+ | 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. | ||
+ | =Predefined Dialogs= | ||
+ | == QInputDialog == | ||
+ | The <i>QInputDialog</i> provides a simple convenience dialog to get a single value from the user. | ||
+ | The input value can be a string, a number or an item from a list. | ||
+ | <source lang="python"> | ||
+ | #!/usr/bin/python | ||
+ | |||
+ | # inputdialog.py | ||
+ | |||
+ | import sys | ||
+ | from PyQt4 import QtGui | ||
+ | from PyQt4 import QtCore | ||
+ | |||
+ | |||
+ | class InputDialog(QtGui.QWidget): | ||
+ | def __init__(self, parent=None): | ||
+ | QtGui.QWidget.__init__(self, parent) | ||
+ | |||
+ | self.setGeometry(300, 300, 350, 80) | ||
+ | self.setWindowTitle('InputDialog') | ||
+ | |||
+ | self.button = QtGui.QPushButton('Dialog', self) | ||
+ | self.button.setFocusPolicy(QtCore.Qt.NoFocus) | ||
+ | |||
+ | self.button.move(20, 20) | ||
+ | self.connect(self.button, QtCore.SIGNAL('clicked()'), self.showDialog) | ||
+ | self.setFocus() | ||
+ | |||
+ | self.label = QtGui.QLineEdit(self) | ||
+ | self.label.move(130, 22) | ||
+ | |||
+ | |||
+ | def showDialog(self): | ||
+ | text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:') | ||
+ | |||
+ | if ok: | ||
+ | self.label.setText(unicode(text)) | ||
+ | |||
+ | |||
+ | app = QtGui.QApplication(sys.argv) | ||
+ | icon = InputDialog() | ||
+ | icon.show() | ||
+ | app.exec_() | ||
+ | </source> | ||
+ | |||
+ | 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. | ||
+ | |||
+ | <source lang="python"> | ||
+ | text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:') | ||
+ | </source> | ||
+ | |||
+ | 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. | ||
+ | |||
+ | [[image: pyqt_faq_inputdialog.jpg | center]] | ||
+ | |||
+ | == QColorDialog == | ||
+ | The <i>QColorDialog</i> provides a dialog widget for specifying colors. | ||
+ | <source lang="python"> | ||
+ | #!/usr/bin/python | ||
+ | |||
+ | # colordialog.py | ||
+ | |||
+ | import sys | ||
+ | from PyQt4 import QtGui | ||
+ | from PyQt4 import QtCore | ||
+ | |||
+ | |||
+ | class ColorDialog(QtGui.QWidget): | ||
+ | def __init__(self, parent=None): | ||
+ | QtGui.QWidget.__init__(self, parent) | ||
+ | |||
+ | color = QtGui.QColor(0, 0, 0) | ||
+ | |||
+ | self.setGeometry(300, 300, 250, 180) | ||
+ | self.setWindowTitle('ColorDialog') | ||
+ | |||
+ | self.button = QtGui.QPushButton('Dialog', self) | ||
+ | self.button.setFocusPolicy(QtCore.Qt.NoFocus) | ||
+ | self.button.move(20, 20) | ||
+ | |||
+ | self.connect(self.button, QtCore.SIGNAL('clicked()'), self.showDialog) | ||
+ | self.setFocus() | ||
+ | |||
+ | self.widget = QtGui.QWidget(self) | ||
+ | self.widget.setStyleSheet("QWidget { background-color: %s }" | ||
+ | % color.name()) | ||
+ | self.widget.setGeometry(130, 22, 100, 100) | ||
+ | |||
+ | |||
+ | def showDialog(self): | ||
+ | color = QtGui.QColorDialog.getColor() | ||
+ | |||
+ | self.widget.setStyleSheet("QWidget { background-color: %s }" | ||
+ | % color.name()) | ||
+ | |||
+ | app = QtGui.QApplication(sys.argv) | ||
+ | cd = ColorDialog() | ||
+ | cd.show() | ||
+ | app.exec_() | ||
+ | </source> | ||
+ | |||
+ | The application example shows a push button and a <i>QWidget</i>. The widget background is set to black color. | ||
+ | Using the <i>QColorDialog</i>, we can change it's background. | ||
+ | |||
+ | <source lang="python"> | ||
+ | color = QtGui.QColorDialog.getColor() | ||
+ | </source> | ||
+ | |||
+ | This line will pop up the <i>QColorDialog</i>. | ||
+ | |||
+ | <source lang="python"> | ||
+ | self.widget.setStyleSheet("QWidget { background-color: %s }" | ||
+ | % color.name()) | ||
+ | </source> | ||
+ | |||
+ | We change the background color using stylesheets. | ||
+ | |||
+ | [[image: pyqt_faq_colordialog.jpg | center]] | ||
+ | |||
+ | == QFontDialog == | ||
+ | The <i>QFontDialog</i> is a dialog widget for selecting font. | ||
+ | <source lang="python"> | ||
+ | #!/usr/bin/python | ||
+ | |||
+ | # fontdialog.py | ||
+ | |||
+ | import sys | ||
+ | from PyQt4 import QtGui | ||
+ | from PyQt4 import QtCore | ||
+ | |||
+ | |||
+ | class FontDialog(QtGui.QWidget): | ||
+ | def __init__(self, parent=None): | ||
+ | QtGui.QWidget.__init__(self, parent) | ||
+ | |||
+ | hbox = QtGui.QHBoxLayout() | ||
+ | |||
+ | self.setGeometry(300, 300, 250, 110) | ||
+ | self.setWindowTitle('FontDialog') | ||
+ | |||
+ | button = QtGui.QPushButton('Dialog', self) | ||
+ | button.setFocusPolicy(QtCore.Qt.NoFocus) | ||
+ | button.move(20, 20) | ||
+ | |||
+ | hbox.addWidget(button) | ||
+ | |||
+ | self.connect(button, QtCore.SIGNAL('clicked()'), self.showDialog) | ||
+ | |||
+ | self.label = QtGui.QLabel('Knowledge only matters', self) | ||
+ | self.label.move(130, 20) | ||
+ | |||
+ | hbox.addWidget(self.label, 1) | ||
+ | self.setLayout(hbox) | ||
+ | |||
+ | |||
+ | def showDialog(self): | ||
+ | font, ok = QtGui.QFontDialog.getFont() | ||
+ | if ok: | ||
+ | self.label.setFont(font) | ||
+ | |||
+ | |||
+ | app = QtGui.QApplication(sys.argv) | ||
+ | cd = FontDialog() | ||
+ | cd.show() | ||
+ | app.exec_() | ||
+ | </source> | ||
+ | |||
+ | In our example, we have a button and a label. With <i>QFontDialog</i>, we change the font of the label. | ||
+ | |||
+ | <source lang="python"> | ||
+ | hbox.addWidget(self.label, 1) | ||
+ | </source> | ||
+ | |||
+ | 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. | ||
+ | |||
+ | <source lang="python"> | ||
+ | font, ok = QtGui.QFontDialog.getFont() | ||
+ | </source> | ||
+ | |||
+ | Here we pop up the font dialog. | ||
+ | |||
+ | <source lang="python"> | ||
+ | if ok: | ||
+ | self.label.setFont(font) | ||
+ | </source> | ||
+ | |||
+ | If we clicked ok, the font of the label was changed. | ||
+ | |||
+ | [[image: pyqt_faq_fontdialog.jpg | center]] | ||
+ | |||
+ | == QFileDialog == | ||
+ | The <i>QFileDialog</i> is a dialog that allows users to select files or directories. The files can be selected for both opening a saving. | ||
+ | <source lang="python"> | ||
+ | #!/usr/bin/python | ||
+ | |||
+ | # openfiledialog.py | ||
+ | |||
+ | import sys | ||
+ | from PyQt4 import QtGui | ||
+ | from PyQt4 import QtCore | ||
+ | |||
+ | |||
+ | class OpenFile(QtGui.QMainWindow): | ||
+ | def __init__(self, parent=None): | ||
+ | QtGui.QMainWindow.__init__(self, parent) | ||
+ | |||
+ | self.setGeometry(300, 300, 350, 300) | ||
+ | self.setWindowTitle('OpenFile') | ||
+ | |||
+ | self.textEdit = QtGui.QTextEdit() | ||
+ | self.setCentralWidget(self.textEdit) | ||
+ | self.statusBar() | ||
+ | self.setFocus() | ||
+ | |||
+ | exit = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self) | ||
+ | exit.setShortcut('Ctrl+O') | ||
+ | exit.setStatusTip('Open new File') | ||
+ | self.connect(exit, QtCore.SIGNAL('triggered()'), self.showDialog) | ||
+ | |||
+ | menubar = self.menuBar() | ||
+ | file = menubar.addMenu('&File') | ||
+ | file.addAction(exit) | ||
+ | |||
+ | def showDialog(self): | ||
+ | filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file', | ||
+ | '/home') | ||
+ | file=open(filename) | ||
+ | data = file.read() | ||
+ | self.textEdit.setText(data) | ||
+ | |||
+ | app = QtGui.QApplication(sys.argv) | ||
+ | cd = OpenFile() | ||
+ | cd.show() | ||
+ | app.exec_() | ||
+ | </source> | ||
+ | |||
+ | The example shows a menubar, centrally set text edit widget and a statusbar. The statusbar is shown only for desing purposes. | ||
+ | The the menu item shows the <i>QFileDialog</i> which is used to select a file. The contents of the file are loaded into the text edit widget. | ||
+ | |||
+ | <source lang="python"> | ||
+ | |||
+ | class OpenFile(QtGui.QMainWindow): | ||
+ | ... | ||
+ | self.textEdit = QtGui.QTextEdit() | ||
+ | self.setCentralWidget(self.textEdit) | ||
+ | </source> | ||
+ | |||
+ | The example is based on the <i>QMainWindow</i> widget, because we centrally set the text edit widget. | ||
+ | This is easily done with the <i>QMainWindow</i> widget, without resorting to layouts. | ||
+ | |||
+ | <source lang="python"> | ||
+ | filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file', | ||
+ | '/home') | ||
+ | </source> | ||
+ | |||
+ | We pop up the <i>QFileDialog</i>. 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 (*). | ||
+ | |||
+ | <source lang="python"> | ||
+ | file=open(filename) | ||
+ | data = file.read() | ||
+ | self.textEdit.setText(data) | ||
+ | </source> | ||
+ | |||
+ | The selected file name is read and the contents of the file are set to the text edit widget. | ||
+ | |||
+ | [[image: pyqt_faq_filedialog.jpg | center]] |