PyQt4
基本知识
1 入门程序
import sys
from PyQt4 import QtGui, QtCore
def test_main():
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.resize(250, 150)
widget.setWindowTitle("simple")
widget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
test_main()
2 程序图标
import sys
from PyQt4 import QtGui, QtCore
class Icon(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Icon')
self.setWindowIcon(QtGui.QIcon(u"C:\\Users\wb.duanxingcai\Desktop\学习笔记\pyqtimg\\01.png"))
def main():
app = QtGui.QApplication(sys.argv)
icon = Icon()
icon.show()
sys.exit(app.exec_())
if __name__ == '__main__':
test_main()
3 显示提示信息
import sys
from PyQt4 import QtGui, QtCore
class Tooltip(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Tooltip')
self.setToolTip('this is a <b> QWidget </b> widget')
QtGui.QToolTip.setFont(QtGui.QFont('OldEnglish', 10))
def main():
app = QtGui.QApplication(sys.argv)
tool = Tooltip()
tool.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
4 关闭窗口
import sys
from PyQt4 import QtGui, QtCore
class QuitButton(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle("quitbutton")
quit = QtGui.QPushButton('Close', self)
quit.setGeometry(10, 10, 60, 35)
self.connect(quit, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT('quit()'))
def main():
app = QtGui.QApplication(sys.argv)
quit = QuitButton()
quit.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
5 消息窗口
import sys
from PyQt4 import QtGui, QtCore
class MessageBox(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('message box')
def closeEvent(self, event):
reply = QtGui.QMessageBox.question(self, 'Message', 'Are you sure to quit',
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def main():
app = QtGui.QApplication(sys.argv)
qb = MessageBox()
qb.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
6 窗口放屏幕中间
import sys
from PyQt4 import QtGui, QtCore
class Center(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setWindowTitle("center")
self.resize(250, 150)
self.center()
def center(self):
screen = QtGui.QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) / 2,
(screen.height() - size.height()) / 2)
def main():
app = QtGui.QApplication(sys.argv)
center = Center()
center.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
7 状态栏
import sys
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(250, 150)
self.setWindowTitle('statusbar')
self.statusBar().showMessage('Ready')
def main():
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()