zoukankan      html  css  js  c++  java
  • 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_())
  • 相关阅读:
    PC端圣诞树下载
    win7开机一直在正在启动windows界面怎么办?
    EFI、UEFI、MBR、GPT的区别
    进入BIOS中,设置U盘启动
    CSS3摆动动画效果
    比特币钱包搭建与使用
    自动校时工具
    windows7蓝屏0x000000c4
    如何使用webpack打包你的项目
    开源货币/比特币Multiminer、bitrade、bitcoinjs-lib、python-bitcoinrpc介绍
  • 原文地址:https://www.cnblogs.com/countryboy666/p/11186767.html
Copyright © 2011-2022 走看看