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 = 'Лев Николаевич Толстой\nАнна Каренина'
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_())
posted @ 2019-07-15 07:04  countryboy666  阅读(180)  评论(0编辑  收藏  举报