• PyQt5--基础(2)


    #-*-coding:utf-8 -*-

    import sys
    from PyQt5.QtWidgets import QApplication,QWidget,QLabel
    from PyQt5.QtWidgets import QMainWindow,QAction,qApp
    from PyQt5.QtGui import QIcon,QColor
    from PyQt5.QtWidgets import QPushButton,QHBoxLayout,QVBoxLayout
    from PyQt5.QtWidgets import QLineEdit, QTextEdit,QGridLayout
    from PyQt5.QtWidgets import QInputDialog,QFrame,QColorDialog
    from PyQt5.QtWidgets import QSizePolicy,QFontDialog
    from PyQt5.QtWidgets import QFileDialog

    from PyQt5.QtCore import Qt,pyqtSignal,QObject
    from PyQt5.QtWidgets import QLCDNumber,QSlider

    from PyQt5.QtGui import QPainter,QColor,QFont


    class example(QWidget):
    def __init__(self):
    super().__init__()
    self.initUI()
    def initUI(self):
    lcd = QLCDNumber(self)
    #滑块
    sld = QSlider(Qt.Horizontal,self)

    vbox = QVBoxLayout()
    vbox.addWidget(lcd)
    vbox.addWidget(sld)

    self.setLayout(vbox)
    sld.valueChanged.connect(lcd.display)
    self.setGeometry(300,300,300,300)
    self.setWindowTitle('signal and slot')

    self.show()

    class example(QWidget):
    def __init__(self):
    super().__init__()
    self.initUI()
    def initUI(self):
    self.setGeometry(300,300,300,300)
    self.setWindowTitle("Event handler")
    self.show()
    def keyPressEvent(self, e):
    if e.key() == Qt.Key_Escape:
    self.close()


    class example(QWidget):
    def __init__(self):
    super().__init__()
    self.initUI()
    def initUI(self):
    grid = QGridLayout()
    grid.setSpacing(10)

    x = 0
    y = 0

    self.text = 'x: {0},y: {1}'.format(x,y)

    self.label = QLabel(self.text,self)
    grid.addWidget(self.label,0,0,Qt.AlignTop)

    self.setLayout(grid)

    self.setGeometry(300,300,200,200)
    self.setWindowTitle('Event object')
    self.show()
    def mouseMoveEvent(self, e):
    x = e.x()
    y = e.y()

    text = "x: {0},y:{1}".format(x,y)
    self.label.setText(text)

    class example(QMainWindow):
    def __init__(self):
    super().__init__()
    self.initUI()
    def initUI(self):

    btn1 = QPushButton('Button 1',self)
    btn1.move(30,50)

    btn2 = QPushButton('Button 2',self)
    btn2.move(150,50)

    btn1.clicked.connect(self.buttonClicked)
    btn2.clicked.connect(self.buttonClicked)

    self.statusBar()

    self.setGeometry(300,300,290,150)
    self.setWindowTitle('Event sender')
    self.show()

    def buttonClicked(self):
    sender = self.sender()
    # 获得是那个button 发出的信号
    self.statusBar().showMessage(sender.text()+'was pressed')



    class Communicate(QObject):
    #a custom signal
    closeApp = pyqtSignal()
    class example(QMainWindow):
    def __init__(self):
    super().__init__()
    self.initUI()
    def initUI(self):
    # custom signal object
    self.c = Communicate()
    self.c.closeApp.connect(self.close)

    self.setGeometry(300,300,290,150)
    self.setWindowTitle("Emit signal")
    self.show()

    def mousePressEvent(self, e):
    self.c.closeApp.emit()




    class example(QWidget):
    def __init__(self):
    super().__init__()
    self.initUI()
    def initUI(self):
    self.text = 'Лев Николаевич Толстой Анна Каренина'
    self.setGeometry(300,300,300,300)
    self.setWindowTitle('Drawing text')
    self.show()
    def paintEvent(self, event):
    qp = QPainter()
    qp.begin(self)
    self.drawText(event,qp)
    qp.end()
    def drawText(self,event,qp):
    qp.setPen(QColor(1,34,3))
    qp.setFont(QFont('Decorative',10))
    qp.drawText(event.rect(),Qt.AlignCenter,self.text)



    if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = example()
    sys.exit(app.exec_())
  • 相关阅读:
    C#中的异常处理
    How to: Create a Windows Communication Foundation Client
    王立平--result += "{";
    Gradle增量学习建筑
    android 当屏幕截图,你如何改变屏幕内容,他们俩bitmap将合并的图像被一起显示
    惊人go语言(image网站开发)
    树莓派的演奏音符3 -- LCD1602显示文章
    Linux解析内核源代码——传输控制块诞生
    Preemption Context Switches 和 Synchronization Context Switches
    folat i = 0.1; 警告
  • 原文地址:https://www.cnblogs.com/countryboy666/p/11186767.html
Copyright © 2020-2023  润新知