pythonGUI programming --Qt Library (1)

1. A simple example implementation, a small window.
       PyQt5 is a high-level language, following only a few lines of code can be displayed in a small window. The bottom has the basic functions of the window.

! # / usr / bin / Python
#coding:. 8 UTF-
# simple example
Import SYS
# intake module, the basic controls located pyqt5.qtwidgets
from the QApplication PyQt5.QtWidgets Import, the QWidget

__name__ == IF '__main__':
# pyqt5 each application must create an application objects.
# sys.argv is a list of parameters, input parameters from the command line
App the QApplication = (the sys.argv)
#QWidget pyqt5 member is the base class for all user interfaces.
the QWidget = W ()
#resize method of adjusting the window size.
w.resize (250,150)
#move () method moves the window position on the screen, the window open initial position
w.move (300,300)
# Set the window title
w.setWindowTitle ( 'Hello')
# displayed on the screen
w.show ( )

System # exit () method to ensure the application of clean exits
the sys.exit (app.exec_ ())
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
       displays a small window:
                                                 

2. Display a small icon in the upper left corner of the window.
! # / usr / bin / Python
#coding: UTF-8
SYS Import
from PyQt5.QtWidgets Import QApplication, QWidget
from PyQt5.QtGui Import QIcon
# Example create a subclass, and inheritance QWidegt parent
class Example (QWidget):
DEF __init __ ( Self):
Super () .__ the init __ ()
self.initUI ()

def initUI (self):
location and size settings window #
self.setGeometry (300,300,300,300)
# Set the window title
self.setWindowTitle ( 'the Hello')
# Set the window icon, a reference picture in the current directory
self.setWindowIcon (QIcon ( ' 1.jpg '))
# display window
self.show ()

the __name__ == IF '__main__':
App = the QApplication (the sys.argv)
EX = Example ()
the sys.exit (app.exec_ ())
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
       Super () is a special function, its role is to associate the parent class and subclass. It will display a small window with a Pikachu icon:
                                                 

3. Display button for a prompt.
! # / usr / bin / Python
#coding: UTF-8
Import SYS
from PyQt5.QtWidgets * Import
from PyQt5.QtGui Import QFont, QIcon

Example class (QWidget):
DEF __init __ (Self):
Super () .__ the init __ ()
self.initUI ()

DEF initUI (Self):
# this static method for tooltips font. The second parameter is the font size
QToolTip.setFont (QFont ( 'ScanSerif', 12))
# create a prompt.
self.setToolTip ( 'This IS A <B> QWidegt </ B> the widget')
# Create a button and set it to a prompt
BTN = the QPushButton ( 'the Button', Self)
btn.setToolTip ( 'This IS A <B> QWidegt </ b> widget ')

# btn.sizeHint () displays the default size
btn.resize (btn.sizeHint ())
# button moves the window position
btn.move (50,50)

# Set icon
self.setWindowIcon (QIcon ( '1.jpg'))
self.setGeometry (300,300,300,300)
self.setWindowTitle ( 'Tooltips')
self.show ()

the __name__ == IF '__main__':
App = the QApplication (the sys.argv)
EX = Example ()
the sys.exit (app.exec_ ())
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
       run renderings:
                                          

4. Click the Settings button to launch.
! # / usr / bin / Python
#coding: UTF-8
Import SYS
from PyQt5.QtWidgets Import QWidget, QPushButton, QApplication
from PyQt5.QtCore Import QCoreApplication
from PyQt5.QtGui Import QIcon
class Example (QWidget):

__init __ DEF (Self):
Super () .__ the init __ ()
self.initUI ()
DEF initUI (Self):
# Settings button, named for the Quit
qbtn = QPushButton ( 'the Quit', Self)
# Exit clicks binding
qbtn. clicked.connect (the lambda:. QCoreApplication.instance () quit ())
qbtn.resize (qbtn.sizeHint ())
qbtn.move (50, 50)

self.setGeometry (300,300,300,300)
self.setWindowTitle ( 'the Quit Button')
self.setWindowIcon (QIcon ( '1.jpg'))
self.show ()
IF the __name__ == '__main__':
App = the QApplication (the sys.argv)
EX Example = ()
the sys.exit (app.exec_ ())
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
       code lambda anonymous function, do not know why bind click event to exit the method will not only set after being given an anonymous function, chiefs who want answers.

       Run renderings click Quit to close the window:
                                          

5. Set the closed prompt, click the X that is there will be prompt.
! # / usr / bin / Python
#coding: UTF-8
Import SYS
from PyQt5.QtWidgets Import QWidget, QMessageBox, QApplication
from PyQt5.QtGui Import QIcon

class Example(QWidget):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setGeometry(300,300,300,300)
self.setWindowIcon(QIcon('1.jpg'))
self.setWindowTitle('Message box')
self.show()

the closeEvent DEF (Self, Event):
'' '
displays a message box, Yes or No two buttons,
the first parameter is the name of the message box appears, the
second parameter is a text box displays the message,
the third parameter is option Yes or No,
the fourth parameter is the default option No
'' '
Reply = QMessageBox.question (Self,' the Message ',
"Are you the Sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
IF == QMessageBox.Yes Reply:
event.accept ()
the else:
event.ignore ()

the __name__ == IF '__main__':
App = the QApplication (the sys.argv)
EX = Example ()
the sys.exit (app.exec_ ())
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
       code statement determines if a click event If yes exit, if no ignored.
       Run effect diagram:
                                          

6. The center of the screen window.
! # / usr / bin / Python
#coding: UTF-8
SYS Import
from PyQt5.QtWidgets Import QWidget, QDesktopWidget, QApplication

class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.resize(250,150)
self.center()
self.setWindowTitle('center')
self.show()

# Control window to the center of the screen method
DEF Center (Self):
# window obtained
QR = self.frameGeometry ()
# center of the screen to obtain
CP = QDesktopWidget () availableGeometry () Center ()..
# To the screen center
qr.moveCenter (cp)
self.move (qr.topLeft ())

the __name__ == IF '__main__':
App = the QApplication (the sys.argv)
EX = Example ()
the sys.exit (app.exec_ ())
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
       QtGui, QDesktopWidget class provides information about the user's desktop, including screen size.
       Run renderings:
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/11014135.html