PyQt5--基础

Posted countryboy666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyQt5--基础相关的知识,希望对你有一定的参考价值。

#-*-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_())

以上是关于PyQt5--基础的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5快速上手基础篇2-按钮控制LCD屏显示

《PyQT5软件开发 - 基础篇》第1章 PyQt5简介

《PyQT5软件开发 - 基础篇》第4章 PyQt5菜单和工具栏

《PyQT5软件开发 - 基础篇》第4章 PyQt5菜单和工具栏

《PyQT5软件开发 - 基础篇》第3章 PyQt5布局管理

《PyQT5软件开发 - 基础篇》第5章 PyQt5事件和信号