PyQt5的学习之路(二)
来源:互联网 发布:java无限循环代码 编辑:程序博客网 时间:2024/05/17 18:15
图标
#! /usr/bin/python# -*- coding: utf-8 -*-import sysfrom PyQt5.QtWidgets import QApplication, QWidgetfrom PyQt5.QtGui import QIconclass Myform(QWidget): def __init__(self): super(Myform, self).__init__() self.initUI() def initUI(self): # 前两个参数定位了窗口x轴,y轴的位置,效果等同于self.move(300, 300) # 后两个参数确定了它的尺寸,效果等同于self.resize(300, 200) self.setGeometry(300, 300, 300, 220) self.setWindowTitle('Icon') self.setWindowIcon(QIcon('/home/linux/1.jpg')) self.show()if __name__ == '__main__': app = QApplication(sys.argv) w = Myform() sys.exit(app.exec_())
tooltips
#! /usr/bin/python# -*- coding: utf-8 -*-import sysfrom PyQt5.QtWidgets import QWidget, QApplication, QToolTip, QPushButtonfrom PyQt5.QtGui import QFontclass Myform(QWidget): def __init__(self): super(Myform, self).__init__() self.initUI() def initUI(self): QToolTip.setFont(QFont('SansSerif', 20)) self.setToolTip('This is a <b>QWidget</b> widget') #设置窗口的提示内容 btn = QPushButton('Button', self) # 按钮组件 btn.setToolTip('This is a <b>QPushButton</b> widget') # 设置按钮的提示内容 btn.resize(btn.sizeHint()) # 设置组件为推荐大小 btn.move(0,0) # 从内容开始移位 self.setGeometry(300, 300, 300, 300) self.setWindowTitle('tips') self.show()if __name__ == '__main__': app = QApplication(sys.argv) w = Myform() sys.exit(app.exec_())
关闭按钮
#! /usr/bin/python# -*- coding: utf-8 -*-import sysfrom PyQt5.QtWidgets import QWidget, QApplication, QPushButtonfrom PyQt5.QtCore import QCoreApplicationclass Myform(QWidget): def __init__(self): super(Myform, self).__init__() self.initUI() def initUI(self): qbtn = QPushButton('Quit', self) # 信号&槽机制: clicked是信号,quit是槽(事件),QCoreApplication.instance()是事件对象 qbtn.clicked.connect(QCoreApplication.instance().quit) qbtn.resize(qbtn.sizeHint()) qbtn.move(50, 50) self.setGeometry(300, 300, 250, 150) self.setWindowTitle('Quit button') self.show()if __name__ == '__main__': app = QApplication(sys.argv) w = Myform() sys.exit(app.exec_())
提示框
#!/usr/bin/python# -*- coding: utf-8 -*-import sysfrom PyQt5.QtWidgets import QWidget, QApplication, QMessageBoxclass Myform(QWidget): def __init__(self): super(QWidget, self).__init__() self.initUI() def initUI(self): self.setGeometry(200, 200, 300, 300) self.setWindowTitle('message box') self.show() # closeEvent的重新实现 def closeEvent(self, e): # 参数从左到右: self, 提示窗口标题, 提示信息, 两个按钮, 默认选中 reply = QMessageBox.question(self, 'Message', 'Are you sure to quit?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: e.accept() else: e.ignore()if __name__ == '__main__': app = QApplication(sys.argv) w = Myform() sys.exit(app.exec_())
窗口居中
#!/usr/bin/python# -*- coding: utf-8 -*-import sysfrom PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidgetclass Myform(QWidget): def __init__(self): super(Myform, self).__init__() self.initUI() def initUI(self): self.resize(300, 300) self.center() self.setWindowTitle('居中对齐') self.show() def center(self): qr = self.frameGeometry() # 获得窗口框架 cp = QDesktopWidget().availableGeometry().center() # 得到屏幕尺寸的中心位置 qr.moveCenter(cp) #将框架的中心点移到之前获得的中心位置 self.move(qr.topLeft()) #将窗口移动到框架位置if __name__ == '__main__': app = QApplication(sys.argv) w = Myform() sys.exit(app.exec_())
1 0
- PyQt5的学习之路(二)
- PyQt5的学习之路(一)
- PyQt5的学习之路(三)
- PyQt5的学习之路(四)
- PyQt5的学习之路(五)
- PyQt5的学习之路(六)
- PyQt5的学习之路(七)
- PyQt5的学习之路(八)
- pycharm+pyqt5的配置(二)
- pyqt5 学习
- PyQt5学习
- PyQt5学习
- python学习笔记4:PyQt5的安装
- 我的学习之路(二)
- RAML的学习之路(二)
- Struts2的学习之路(二)
- Mybatis的学习之路(二)
- APICloud的学习成长之路(二)
- Qt for Mac 设置软件开机自启动
- Python 栈和队列
- SKStoreProductViewController中的坑
- android webView工具类
- C++易错:(四)&&与&
- PyQt5的学习之路(二)
- AOP动态代理实例
- JDK的安装与配置
- Android客户端与服务器端交互 如何保持session
- OpenCV 3 最新模块一次看个够
- Hibernate 之单表映射
- TensorFlow安装入门从Ubuntu到Windows
- Android通知栏监听之NotificationListenerService
- swift 基本运算符