实例1:滑动条+显示
import sysfrom PyQt5.QtCore import Qtfrom PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider, QVBoxLayout, QApplication)class SigSlot(QWidget): def __init__(self, parent=None): QWidget.__init__(self) self.setWindowTitle('XXOO') lcd = QLCDNumber(self) slider = QSlider(Qt.Horizontal, self) vbox = QVBoxLayout() vbox.addWidget(lcd) vbox.addWidget(slider) self.setLayout(vbox) slider.valueChanged.connect(lcd.display) self.resize(350, 250)app = QApplication(sys.argv)qb = SigSlot()qb.show()sys.exit(app.exec_())!/usr/bin/python3 coding = utf-8
实例2:新建窗口
import sysfrom PyQt5.QtWidgets import QApplication, QWidgetif __name__ == '__main__': app = QApplication(sys.argv) w = QWidget() w.resize(600, 600) w.move(1068, 612) w.setWindowTitle('天晟出品') w.show() sys.exit(app.exec_())
实例3:try...except...的语句,通过这个语句我们实现了:命令行中如果带入参数,窗口的标题就用这个参数代替,否则用默认的“学点编程吧出品”作为窗口的标题。当输入多个参数时,则合并这些参数为一个。
import sysfrom PyQt5.QtWidgets import QApplication, QWidgetif __name__ == '__main__': app = QApplication(sys.argv) try: if len(sys.argv) < 2: raise ValueError else: title = " ".join(sys.argv[1:]) except ValueError: title = "天晟出品" w = QWidget() w.resize(600, 600) w.move(1068, 612) w.setWindowTitle(title) w.show() sys.exit(app.exec_())
实例4:插入图标
import sysfrom PyQt5.QtWidgets import QApplication, QWidgetfrom PyQt5.QtGui import QIconclass Ico(QWidget): def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setGeometry(1068, 612, 600, 600) self.setWindowTitle('天晟出品') self.setWindowIcon(QIcon('ComNet.ico')) self.show()if __name__ == '__main__': app = QApplication(sys.argv) ex = Ico() sys.exit(app.exec_())
实例5:点击按钮退出
import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QPushButtonfrom PyQt5.QtGui import QIconfrom PyQt5.QtCore import QCoreApplicationclass Ico(QWidget): def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setGeometry(1068, 612, 600, 600) self.setWindowTitle('天晟出品') self.setWindowIcon(QIcon('ComNet.ico')) qbtn = QPushButton('退出',self) #新建按钮对象 qbtn.clicked.connect(QCoreApplication.instance().quit) #按钮点击事件 qbtn.resize(100, 100) qbtn.move(250, 250) self.show()if __name__ == '__main__': app = QApplication(sys.argv) ex = Ico() sys.exit(app.exec_())