作事歷來不堅持的我又開始學習PyQt了。。。。。。

連接附上,再也不更新:PyQt5圖形界面編程

第一部分

第一個程序

 1 # -*- coding: utf-8 -*-
 2 
 3 import sys
 4 from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QPushButton
 5 from PyQt5.QtGui import QIcon, QFont
 6 
 7 class Example(QWidget):
 8 
 9     def __init__(self):
10         super(Example, self).__init__()
11         self.initUI()
12 
13     def initUI(self):
14         '''
15         在這裏咱們有兩個部件能夠顯示提示
16         一個是窗口自己,另外一個是按鈕
17         '''
18 
19         #設置提示字體和字號
20         QToolTip.setFont(QFont('微軟雅黑', 10))
21 
22         #給組件自己設置一個提示
23         self.setToolTip('this is a widget')
24 
25         #添加一個按鈕
26         btn = QPushButton('Button', self)
27         #並添加提示
28         btn.setToolTip('this is a pushbtn')
29         #設定按鈕大小和位置,使用推薦大小
30         btn.resize(btn.sizeHint())
31         btn.move(50, 50)
32 
33         #設置窗口的位置和寬高
34         self.setGeometry(300, 300, 300, 300)
35         #設置窗口標題
36         self.setWindowTitle('icon')
37         #設置窗口圖標
38         self.setWindowIcon(QIcon('dog.jpg'))
39 
40         self.show()
41 
42 
43 if __name__ == '__main__':
44 
45     app = QApplication(sys.argv)
46     example = Example()
47     sys.exit(app.exec_())

如何優雅地退出窗口

 1 # -*- coding: utf-8 -*-
 2 
 3 import sys
 4 from PyQt5.QtWidgets import QApplication, QWidget,QPushButton
 5 from PyQt5.QtCore import QCoreApplication
 6 
 7 class Example(QWidget):
 8 
 9     def __init__(self):
10         super(Example, self).__init__()
11         self.initUI()
12 
13     def initUI(self):
14 
15         #添加一個按鈕
16         btn = QPushButton('Quit', self)
17         btn.clicked.connect(QCoreApplication.instance().quit)
18         #設定按鈕大小和位置,使用推薦大小
19         btn.resize(btn.sizeHint())
20         btn.move(50, 50)
21 
22         #設置窗口的位置和寬高
23         self.setGeometry(300, 300, 300, 300)
24         #設置窗口標題
25         self.setWindowTitle('quit')
26 
27         self.show()
28 
29 
30 if __name__ == '__main__':
31 
32     app = QApplication(sys.argv)
33     example = Example()
34     sys.exit(app.exec_())

PyQt5中的事件處理系統是由信號槽機制(signals and slots)實現的。若是咱們點擊了這個按鈕,就會發出「clicked」這個信號。QtCore.QCoreApplication這個東西包含了程序的主循環,它處理和分派全部的事件,而instance()方法返回的是目前的實例(insatnce)。注意到QtCore.QCoreApplication隨着QtGui.QApplication的建立而建立,而因爲咱們這裏用connect()函數將「clicked」事件和能夠終止應用的quit()函數聯繫(connect)在了一塊兒,因此點擊按鈕,應用終止。這種交流在兩個對象之間完成:發送者和接受者,其中發送者是按鈕,接受者是應用自己。【這裏只要你們對connect方法有個感性的認識就能夠了】編程

消息框

 1 # -*- coding: utf-8 -*-
 2 
 3 import sys
 4 from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox
 5 
 6 class Example(QWidget):
 7 
 8     def __init__(self):
 9         super(Example, self).__init__()
10         self.initUI()
11 
12     def initUI(self):
13 
14         #設置窗口的位置和寬高
15         self.setGeometry(300, 300, 300, 300)
16         #設置窗口標題
17         self.setWindowTitle('Message Box')
18         self.show()
19 
20     def closeEvent(self, event):
21         '''
22         這裏咱們設定顯示一個有兩個選項(yes & no)的消息框(message box)
23         QtGui.QMessageBox.question()方法的第二個參數是出如今標題欄的標題
24         第三個參數是消息框顯示的對話內容,第四個參數是出如今消息框的按鈕的組合【用或( | )鏈接】
25         最後一個參數是默認按鈕,即消息框剛跳出來的時候按enter鍵就能夠執行的按鈕
26         這裏咱們將函數的返回值存儲在了reply這個變量中
27         '''
28         reply = QMessageBox.question(self, 'Message', 'Are you sure to quit?', QMessageBox.Yes|QMessageBox.No, QMessageBox.No)
29 
30         if reply == QMessageBox.Yes:
31             event.accept()
32         else:
33             event.ignore()
34 
35 
36 if __name__ == '__main__':
37 
38     app = QApplication(sys.argv)
39     example = Example()
40     sys.exit(app.exec_())

居中顯示

 1 # -*- coding: utf-8 -*-
 2 
 3 import sys
 4 from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget
 5 
 6 class Example(QWidget):
 7 
 8     def __init__(self):
 9         super(Example, self).__init__()
10         self.initUI()
11 
12     def initUI(self):
13 
14         #設置窗口的位置和寬高
15         self.resize(500, 500)
16         #設置窗口標題
17         self.setWindowTitle('center widget')
18         self.center()
19         self.show()
20 
21     def center(self):
22         # 獲得主窗口的矩形框架qr
23         qr = self.frameGeometry()
24         # 咱們調用這些方法來獲得屏幕分辨率,並最終獲得屏幕中間點的座標cp
25         cp = QDesktopWidget().availableGeometry().center()
26         # 這裏咱們將矩形框架移至屏幕正中央,大小不變
27         qr.moveCenter(cp)
28         # 最後咱們將應用窗口移至矩形框架的左上角點,這樣應用窗口就位於屏幕的中央了【注意部件的move都是左上角移動到某點】
29         self.move(qr.topLeft())
30 
31 
32 if __name__ == '__main__':
33 
34     app = QApplication(sys.argv)
35     example = Example()
36     sys.exit(app.exec_())

第二部分

狀態欄(statusbar)

 1 # -*- coding: utf-8 -*-
 2 
 3 import sys
 4 from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
 5 
 6 class Example(QMainWindow):
 7 
 8     def __init__(self):
 9         super(Example, self).__init__()
10         self.initUI()
11 
12     def initUI(self):
13         self.statusBar().showMessage('Ready')
14         #設置窗口的位置和寬高
15         self.setGeometry(300, 300, 250, 150)
16         #設置窗口標題
17         self.setWindowTitle('statusbar')
18         self.show()
19 
20 
21 if __name__ == '__main__':
22 
23     app = QApplication(sys.argv)
24     example = Example()
25     sys.exit(app.exec_())

菜單欄(menubar)

 1 # -*- coding: utf-8 -*-
 2 
 3 import sys
 4 from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp
 5 from PyQt5.QtGui import QIcon
 6 
 7 class Example(QMainWindow):
 8 
 9     def __init__(self):
10         super(Example, self).__init__()
11         self.initUI()
12 
13     def initUI(self):
14         # 建立一個選項有本身的圖標和名字
15         exitAct = QAction(QIcon('dog.jpg'), '&Exit', self)
16         # 設置對應的一個快捷鍵
17         exitAct.setShortcut('CTRL+Q')
18         # 當鼠標懸停在狀態欄的提示信息
19         exitAct.setStatusTip('Exit Application')
20         # 當咱們選擇了這個選項時,一個觸發信號(triggered signal)被髮出了
21         # 這個信號和qApp部件的quit()方法相聯繫(connect),因此信號發出後,程序終止
22         exitAct.triggered.connect(qApp.quit)
23 
24         statusbar = self.statusBar()
25 
26         menubar = self.menuBar()
27         fileMenu = menubar.addMenu('&File')
28         fileMenu.addAction(exitAct)
29 
30         self.setGeometry(300,300,300,300)
31         self.setWindowTitle('simple menu')
32         self.show()
33 
34 
35 if __name__ == '__main__':
36 
37     app = QApplication(sys.argv)
38     example = Example()
39     sys.exit(app.exec_())

工具欄

# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp
from PyQt5.QtGui import QIcon

class Example(QMainWindow):

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

    def initUI(self):
        # 建立一個選項有本身的圖標和名字
        exitAct = QAction(QIcon('dog.jpg'), '&Exit', self)
        # 設置對應的一個快捷鍵
        exitAct.setShortcut('CTRL+Q')
        # 當咱們選擇了這個選項時,一個觸發信號(triggered signal)被髮出了
        # 這個信號和qApp部件的quit()方法相聯繫(connect),因此信號發出後,程序終止
        exitAct.triggered.connect(qApp.quit)

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAct)

        self.setGeometry(300,300,300,300)
        self.setWindowTitle('simple menu')
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    example = Example()
    sys.exit(app.exec_())
相關文章
相關標籤/搜索