#!/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_ ( )
text, ok = QtGui. QInputDialog . getText ( self , 'Input Dialog' , 'Enter your name:' )
#!/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_ ( )
color = QtGui. QColorDialog . getColor ( )
self . widget . setStyleSheet ( "QWidget { background-color: %s }" % color. name ( ) )
#!/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_ ( )
hbox. addWidget ( self . label , 1 )
font, ok = QtGui. QFontDialog . getFont ( )
if ok:
self . label . setFont ( font )
#!/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_ ( )
class OpenFile ( QtGui. QMainWindow ) :
...
self . textEdit = QtGui. QTextEdit ( )
self . setCentralWidget ( self . textEdit )
filename = QtGui. QFileDialog . getOpenFileName ( self , 'Open file' , '/home' )
file = open ( filename )
data = file . read ( )
self . textEdit . setText ( data )
Source: https://habr.com/ru/post/52457/
All Articles