图标
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()
self.initUI()
def initUI(self):
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_())
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QToolTip, QPushButton
from PyQt5.QtGui import QFont
class 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_())
关闭按钮
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton
from PyQt5.QtCore import QCoreApplication
class Myform(QWidget):
def __init__(self):
super(Myform, self).__init__()
self.initUI()
def initUI(self):
qbtn = QPushButton('Quit', self)
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_())
提示框
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox
class 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()
def closeEvent(self, e):
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_())
窗口居中
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidget
class 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_())