📜 ⬆️ ⬇️

PyQt4 - Dialog Boxes

There are two kinds of dialogs in PyQt4: predefined and custom.

Predefined Dialogs


QInputDialog

QInputDialog is a simple dialog for retrieving a single value from a user. The value can be a string, a number, or an item from a list.

#!/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_ ( )


The form contains a button and an input line. Clicking on the button opens an input dialog to get a text value. The entered text will be displayed in the input line.

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

This line displays the input dialog. The first line is the title of the dialogue, the second is the message text. The dialog returns the entered text and a logical value. If we click the OK button, the Boolean value will be true, otherwise false.
Input dialog
')
QColorDialog

QColorDialog provides a dialog for choosing colors.
#!/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_ ( )


In this example, the form contains a button ( QPushButton ) and QWidget . The background of the widget is painted black. Using QColorDialog we can change its background to the color we specified.

color = QtGui. QColorDialog . getColor ( )

This line opens a QColorDialog .

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

We change the background color using the style sheet.
Color selection dialog

QFontDialog

QFontDialog provides a dialog to select a font.
#!/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_ ( )


In our example, we located on the form QPushButton and QLabel . With QFontDialog help , we changed the QLabel font.

hbox. addWidget ( self . label , 1 )

We made a QLabel resizable. This is necessary, because using different fonts the text may become larger and the inscription may not be fully visible.

font, ok = QtGui. QFontDialog . getFont ( )

Here we open the font selection dialog.

if ok:
self . label . setFont ( font )


If we click on the OK button, the QLabel font will change.
Font selection dialog

QFileDialog

QFileDialog is a dialog that allows users to select files or folders. Files can be specified both for saving and for opening.
#!/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_ ( )


In this example, the form contains a menu bar, QTextEdit, and a status bar. The menu item allows you to open the file selection dialog. The contents of the selected file are loaded into QTextEdit . The status bar is shown solely for beauty purposes :)

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


This example is based on QMainWindow , because we filled the QTextEdit form center. This is easy to do with QMainWindow without layouts .

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

We open the QFileDialog dialog. The second parameter in the getOpenFileName method is the window title. The third indicates the starting directory. The default file filter is set to All files (*).

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


The contents of the selected file are read and populate with QTextEdit .
File Selection Dialog
.

Source: https://habr.com/ru/post/52457/


All Articles