Getting Started with PyQt5 (16) Scroll Bar & Dynamic Display of Current Time & Window Timing Closing & Thread Class Writing Counter

table of Contents

1. There are people outside, and there are heaven outside the sky

2. Scroll bar control QScrollBar

Three. Dynamically display the current time

Four. Let the window close regularly

Five. Use the thread class (QThread) to write the counter


1. There are people outside, and there are heaven outside the sky

Tonight I realized a big shortcoming of myself. I am self-righteous. I always feel that I have something, but now I know that I am nothing. To be a human being, you must be humble, and you must learn to judge your time. You are not superior to others. where!

When you think you are attractive enough, the other party may not pay attention to you.

 

2. Scroll bar control QScrollBar

Here is a problem to be solved : how to mix and match the layout , this problem should be discussed below, and then we will further improve the code.

Code:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

class ScrollBar(QWidget):
    def __init__(self):
        super(ScrollBar, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('滚动条控件演示')
        self.setGeometry(400,300,300,500)
        #水平布局
        hbox=QHBoxLayout()

        #定义控件
        self.label=QLabel('拖动滚动条去改变文字颜色')

        self.scrollbar1=QScrollBar() #滚动条
        self.scrollbar1.setMaximum(255) #滚动条最大值
        #信号与槽
        self.scrollbar1.sliderMoved.connect(self.sliderMoved)

        self.scrollbar2 = QScrollBar()
        self.scrollbar2.setMaximum(255)
        self.scrollbar2.sliderMoved.connect(self.sliderMoved)

        self.scrollbar3 = QScrollBar()
        self.scrollbar3.setMaximum(255)
        self.scrollbar3.sliderMoved.connect(self.sliderMoved)

        self.scrollbar4 = QScrollBar()
        self.scrollbar4.setMaximum(255)
        self.scrollbar4.sliderMoved.connect(self.sliderMoved1)

        #向布局中添加控件
        hbox.addWidget(self.label)
        hbox.addWidget(self.scrollbar1)
        hbox.addWidget(self.scrollbar2)
        hbox.addWidget(self.scrollbar3)
        hbox.addWidget(self.scrollbar4)

        self.setLayout(hbox)
        #获得标签的纵坐标
        self.y=self.label.pos().y()

    def sliderMoved(self):
        print(self.scrollbar1.value(),self.scrollbar2.value(),self.scrollbar3.value())
        #调色版
        palette=QPalette()
        #最后一个参数是透明度
        c=QColor(self.scrollbar1.value(),self.scrollbar2.value(),self.scrollbar3.value(),255)
        #参数一:QPalette.Foreground设置前景色,即标签的颜色  参数2:颜色
        palette.setColor(QPalette.Foreground,c)
        self.label.setPalette(palette)

    def sliderMoved1(self):
        #向下移动标签
        self.label.move(self.label.x(),self.y+self.scrollbar4.value())


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=ScrollBar()
    main.show()
    sys.exit(app.exec_())

operation result:

Try dragging it yourself.

 

Three. Dynamically display the current time

QTimer timer, suitable for multi-tasking, that is, it will be called once every certain time

QThread: You can use this to complete a single task

Multithreading: used to complete multiple tasks at the same time

 

Code:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class showTime(QWidget):
    def __init__(self,parent=None):
        super(showTime, self).__init__(parent)
        self.setWindowTitle('动态显示当前时间')

        self.label=QLabel('显示当前时间')
        self.startBtn=QPushButton('开始')
        self.endBtn=QPushButton('结束')
        #栅格布局
        layout=QGridLayout()

        #计时器对象
        self.timer=QTimer()
        self.timer.timeout.connect(self.showTime)

        layout.addWidget(self.label,0,0,1,2)#占一行两列
        layout.addWidget(self.startBtn,1,0)
        layout.addWidget(self.endBtn, 1, 1)

        self.startBtn.clicked.connect(self.startTimer)
        self.endBtn.clicked.connect(self.endTimer)

        self.setLayout(layout)

    def showTime(self):
        time=QDateTime.currentDateTime()
        #dddd是星期几
        timeDispaly=time.toString('yyyy-MM-dd hh:mm:ss dddd')
        #将标签设置成当前时间
        self.label.setText(timeDispaly)

    def startTimer(self):
        # 参数是时间间隔,1000毫秒
        self.timer.start(1000)
        self.startBtn.setEnabled(False) #不能按
        self.endBtn.setEnabled(True) #可以按

    def endTimer(self):
        #停止计时
        self.timer.stop()
        self.startBtn.setEnabled(True)
        self.endBtn.setEnabled(False)


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=showTime()
    main.show()
    sys.exit(app.exec_())

operation result:

 

Four. Let the window close regularly

Code:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


if __name__=='__main__':
    app=QApplication(sys.argv)
    label=QLabel('<font color=red size=140><b>Hello World,窗口在5秒后自动关闭!</b></font>')
    #setWindowFlag设置窗口属性:启动画面,无框架
    label.setWindowFlags(Qt.SplashScreen | Qt.FramelessWindowHint)
    label.show()
    #5秒之后退出整个程序
    QTimer.singleShot(5000,app.quit)
    sys.exit(app.exec_())


operation result:

It turns off automatically after 5 seconds.

 

Five. Use the thread class (QThread) to write the counter

Use the thread class (QThread) to write the interpreter
QThread


def run(self):
    while True:
       self. sleep(1)
       if sec == 5:
            break;


QLCDNumber
WorkThread (QThread)
uses a custom signal. The signals used before are all defined by the system. Here you need to define another one yourself.
 

Code:

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

#全局变量,当前计数从0开始
sec=0

class WorkThread(QThread): #继承QThread类
    # 每隔一秒发送一次信号,pyqtSignal()用来定义信号的
    timer=pyqtSignal()
    # 计数完成后发送一次信号
    end=pyqtSignal()
    def run(self):
        while True:
            self.sleep(1)#休眠一秒
            if sec==5:
                self.end.emit()#发送end信号,emit()用来发送信号
                break
            self.timer.emit() #发送timer信号


class Counter(QWidget):
    def __init__(self,parent=None):
        super(Counter, self).__init__(parent)

        self.setWindowTitle('使用线程类(QThread)编写计数器')
        self.resize(300,120)

        layout=QVBoxLayout() #垂直布局
        #数码管
        self.lcdNumber=QLCDNumber()
        layout.addWidget(self.lcdNumber)

        btn=QPushButton('开始计数')
        layout.addWidget(btn)

        self.workThread=WorkThread()

        self.workThread.timer.connect(self.countTime)
        self.workThread.end.connect(self.end)
        btn.clicked.connect(self.work)

        self.setLayout(layout)

    def countTime(self):
        global sec #声明一下是全局变量
        sec+=1
        self.lcdNumber.display(sec)

    def end(self):
        QMessageBox.information(self,'消息','计数结束',QMessageBox.Ok)

    def work(self):
        self.workThread.start()


if __name__=='__main__':
    app=QApplication(sys.argv)
    main=Counter()
    main.show()
    sys.exit(app.exec_())

operation result:

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/113726513