做事从来不坚持的我又开始学习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_())

 

posted @ 2017-10-15 16:45  swallowblank  阅读(1107)  评论(0编辑  收藏  举报