Python QTimer實(shí)現(xiàn)多線程及QSS應(yīng)用過程解析
多線程類似于同時執(zhí)行多個不同程序,多線程運(yùn)行有如下優(yōu)點(diǎn):
- 使用線程可以把占據(jù)長時間的程序中的任務(wù)放到后臺去處理。
- 用戶界面可以更加吸引人,比如用戶點(diǎn)擊了一個按鈕去觸發(fā)某些事件的處理,可以彈出一個進(jìn)度條來顯示處理的進(jìn)度。
- 程序的運(yùn)行速度可能加快。
- 在一些等待的任務(wù)實(shí)現(xiàn)上如用戶輸入、文件讀寫和網(wǎng)絡(luò)收發(fā)數(shù)據(jù)等,線程就比較有用了。在這種情況下我們可以釋放一些珍貴的資源如內(nèi)存占用等等。
- 每個獨(dú)立的線程有一個程序運(yùn)行的入口、順序執(zhí)行序列和程序的出口。但是線程不能夠獨(dú)立執(zhí)行,必須依存在應(yīng)用程序中,由應(yīng)用程序提供多個線程執(zhí)行控制。
每個線程都有他自己的一組CPU寄存器,稱為線程的上下文,該上下文反映了線程上次運(yùn)行該線程的CPU寄存器的狀態(tài)。
指令指針和堆棧指針寄存器是線程上下文中兩個最重要的寄存器,線程總是在進(jìn)程得到上下文中運(yùn)行的,這些地址都用于標(biāo)志擁有線程的進(jìn)程地址空間中的內(nèi)存。
線程可以被搶占(中斷)。
在其他線程正在運(yùn)行時,線程可以暫時擱置(也稱為睡眠) -- 這就是線程的退讓。
線程可以分為:
- 內(nèi)核線程:由操作系統(tǒng)內(nèi)核創(chuàng)建和撤銷。
- 用戶線程:不需要內(nèi)核支持而在用戶程序中實(shí)現(xiàn)的線程。
Python3 線程中常用的兩個模塊為:
- _thread
- threading(推薦使用)
thread 模塊已被廢棄。用戶可以使用 threading 模塊代替。所以,在 Python3 中不能再使用"thread" 模塊。為了兼容性,Python3 將 thread 重命名為 "_thread"。
Python中使用線程有兩種方式:函數(shù)或者用類來包裝線程對象。
函數(shù)式:調(diào)用 _thread 模塊中的start_new_thread()函數(shù)來產(chǎn)生新線程。語法如下:
_thread.start_new_thread ( function, args[, kwargs] )
參數(shù)說明:
- function - 線程函數(shù)。
- args - 傳遞給線程函數(shù)的參數(shù),他必須是個tuple類型。
- kwargs - 可選參數(shù)。
#!/usr/bin/python3
import _thread
import time
# 為線程定義一個函數(shù)
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
# 創(chuàng)建兩個線程
try:
_thread.start_new_thread( print_time, ("Thread-1", 2, ) )
_thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
print ("Error: 無法啟動線程")
while 1:
pass
線程模塊
Python3 通過兩個標(biāo)準(zhǔn)庫 _thread 和 threading 提供對線程的支持。
_thread 提供了低級別的、原始的線程以及一個簡單的鎖,它相比于 threading 模塊的功能還是比較有限的。
threading 模塊除了包含 _thread 模塊中的所有方法外,還提供的其他方法:
threading.currentThread(): 返回當(dāng)前的線程變量。
threading.enumerate(): 返回一個包含正在運(yùn)行的線程的list。正在運(yùn)行指線程啟動后、結(jié)束前,不包括啟動前和終止后的線程。
threading.activeCount(): 返回正在運(yùn)行的線程數(shù)量,與len(threading.enumerate())有相同的結(jié)果。
除了使用方法外,線程模塊同樣提供了Thread類來處理線程,Thread類提供了以下方法:
- run(): 用以表示線程活動的方法。
- start():啟動線程活動。
- join([time]): 等待至線程中止。這阻塞調(diào)用線程直至線程的join() 方法被調(diào)用中止-正常退出或者拋出未處理的異常-或者是可選的超時發(fā)生。
- isAlive(): 返回線程是否活動的。
- getName(): 返回線程名。
- setName(): 設(shè)置線程名。
使用 threading 模塊創(chuàng)建線程
我們可以通過直接從 threading.Thread 繼承創(chuàng)建一個新的子類,并實(shí)例化后調(diào)用 start() 方法啟動新線程,即它調(diào)用了線程的 run() 方法:
#!/usr/bin/python3
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("開始線程:" + self.name)
print_time(self.name, self.counter, 5)
print ("退出線程:" + self.name)
def print_time(threadName, delay, counter):
while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
# 創(chuàng)建新線程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# 開啟新線程
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print ("退出主線程")
線程同步
如果多個線程共同對某個數(shù)據(jù)修改,則可能出現(xiàn)不可預(yù)料的結(jié)果,為了保證數(shù)據(jù)的正確性,需要對多個線程進(jìn)行同步。
使用 Thread 對象的 Lock 和 Rlock 可以實(shí)現(xiàn)簡單的線程同步,這兩個對象都有 acquire 方法和 release 方法,對于那些需要每次只允許一個線程操作的數(shù)據(jù),可以將其操作放到 acquire 和 release 方法之間。如下:
多線程的優(yōu)勢在于可以同時運(yùn)行多個任務(wù)(至少感覺起來是這樣)。但是當(dāng)線程需要共享數(shù)據(jù)時,可能存在數(shù)據(jù)不同步的問題。
考慮這樣一種情況:一個列表里所有元素都是0,線程"set"從后向前把所有元素改成1,而線程"print"負(fù)責(zé)從前往后讀取列表并打印。
那么,可能線程"set"開始改的時候,線程"print"便來打印列表了,輸出就成了一半0一半1,這就是數(shù)據(jù)的不同步。為了避免這種情況,引入了鎖的概念。
鎖有兩種狀態(tài)——鎖定和未鎖定。每當(dāng)一個線程比如"set"要訪問共享數(shù)據(jù)時,必須先獲得鎖定;如果已經(jīng)有別的線程比如"print"獲得鎖定了,那么就讓線程"set"暫停,也就是同步阻塞;等到線程"print"訪問完畢,釋放鎖以后,再讓線程"set"繼續(xù)。
經(jīng)過這樣的處理,打印列表時要么全部輸出0,要么全部輸出1,不會再出現(xiàn)一半0一半1的尷尬場面。
#!/usr/bin/python3
import threading
import time
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("開啟線程: " + self.name)
# 獲取鎖,用于線程同步
threadLock.acquire()
print_time(self.name, self.counter, 3)
# 釋放鎖,開啟下一個線程
threadLock.release()
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
print ("%s: %s" % (threadName, time.ctime(time.time())))
counter -= 1
threadLock = threading.Lock()
threads = []
# 創(chuàng)建新線程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# 開啟新線程
thread1.start()
thread2.start()
# 添加線程到線程列表
threads.append(thread1)
threads.append(thread2)
# 等待所有線程完成
for t in threads:
t.join()
print ("退出主線程")
線程優(yōu)先級隊列( Queue)
Python 的 Queue 模塊中提供了同步的、線程安全的隊列類,包括FIFO(先入先出)隊列Queue,LIFO(后入先出)隊列LifoQueue,和優(yōu)先級隊列 PriorityQueue。
這些隊列都實(shí)現(xiàn)了鎖原語,能夠在多線程中直接使用,可以使用隊列來實(shí)現(xiàn)線程間的同步。
Queue 模塊中的常用方法:
- Queue.qsize() 返回隊列的大小
- Queue.empty() 如果隊列為空,返回True,反之False
- Queue.full() 如果隊列滿了,返回True,反之False
- Queue.full 與 maxsize 大小對應(yīng)
- Queue.get([block[, timeout]])獲取隊列,timeout等待時間
- Queue.get_nowait() 相當(dāng)Queue.get(False)
- Queue.put(item) 寫入隊列,timeout等待時間
- Queue.put_nowait(item) 相當(dāng)Queue.put(item, False)
- Queue.task_done() 在完成一項工作之后,Queue.task_done()函數(shù)向任務(wù)已經(jīng)完成的隊列發(fā)送一個信號
- Queue.join() 實(shí)際上意味著等到隊列為空,再執(zhí)行別的操作
#!/usr/bin/python3
import queue
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("開啟線程:" + self.name)
process_data(self.name, self.q)
print ("退出線程:" + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print ("%s processing %s" % (threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = queue.Queue(10)
threads = []
threadID = 1
# 創(chuàng)建新線程
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# 填充隊列
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# 等待隊列清空
while not workQueue.empty():
pass
# 通知線程是時候退出
exitFlag = 1
# 等待所有線程完成
for t in threads:
t.join()
print ("退出主線程")
*******************************************************************************************************
定義QTimer 類
self.timer = QTimer(self)
self.timer.start(1000) #單位為毫秒
self.stop()
QTimer 類的信號
self.timer.timeout.connect(self.function) #到達(dá)設(shè)定的時間后,執(zhí)行function函數(shù)
self.timer.singleShot.connect(1000, app.quit) #設(shè)置 1 秒后界面自動關(guān)閉
這種也是多線程
************************************************************************************
整個這個實(shí)際上是遵循CSS 的對應(yīng)的寫法的,這個是CSS的手冊,所有的東西都可以參考這里: https://css.doyoe.com/
Style Sheets是文字性的設(shè)定,對于整個應(yīng)用程序可以使用QApplication::setStyleSheet() 或者對應(yīng)一個窗口可以使用QWidget::setStyleSheet
舉例:
ui->pushButton->setStyleSheet("QPushButton{border-image: url(:/new/prefix1/image/Main_preset_normal.bmp);}"
"QPushButton:hover{border-image: url(:/new/prefix1/image/Main_preset_hold.bmp);}"
"QPushButton:pressed{border-image: url(:/new/prefix1/image/Main_preset_down.bmp);}");按鈕實(shí)現(xiàn)進(jìn)入、離開、點(diǎn)擊三種效果
self.right_widget.setStyleSheet('''
QWidget#right_widget{
color:#232C51;
background:white;
border-top:1px solid darkGray;
border-bottom:1px solid darkGray;
border-right:1px solid darkGray;
border-top-right-radius:10px;
border-bottom-right-radius:10px;
}
QLabel#right_lable{
border:none;
font-size:16px;
font-weight:700;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
''')加#號,則是widget里的具體那個控件,不加#,沒有#后面的控件名,則是widget里全部該類控件
self.my_ui.graphicsView.setStyleSheet("border:none;background-color:white;")
單個控件應(yīng)用
源代碼:
窗口:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'gui.ui'
#
# Created by: PyQt5 UI code generator 5.14.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_mainWindow(object):
def setupUi(self, mainWindow):
mainWindow.setObjectName("mainWindow")
mainWindow.resize(1211, 865)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(mainWindow.sizePolicy().hasHeightForWidth())
mainWindow.setSizePolicy(sizePolicy)
self.centralwidget = QtWidgets.QWidget(mainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setObjectName("widget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.graphicsView = QtWidgets.QGraphicsView(self.widget)
self.graphicsView.setObjectName("graphicsView")
self.horizontalLayout_2.addWidget(self.graphicsView)
self.verticalLayout.addWidget(self.widget)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setText("")
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setText("")
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.verticalLayout.addLayout(self.horizontalLayout)
mainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(mainWindow)
QtCore.QMetaObject.connectSlotsByName(mainWindow)
def retranslateUi(self, mainWindow):
_translate = QtCore.QCoreApplication.translate
mainWindow.setWindowTitle(_translate("mainWindow", "菜芽"))
主代碼:
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsScene,QGraphicsItem
from PyQt5.QtCore import Qt, QRectF
from PyQt5.QtGui import QColor, QPainter
import qtawesome
from math import pi,sin
from numpy import arange
import _thread
from sys import argv,exit
from PyQt5.QtCore import QTimer
import gui
class my_mainwindow():
def __init__(self):
# PyQt5中,每個應(yīng)用程序都必須實(shí)例化一個QApplication():
app = QApplication(argv)
self.my_MainWindow = QMainWindow()
self.my_ui = gui.Ui_mainWindow()
self.my_ui.setupUi(self.my_MainWindow)
self.my_MainWindow.setWindowOpacity(0.9) # 設(shè)置窗口透明度
self.my_MainWindow.setAttribute(Qt.WA_TranslucentBackground) # 設(shè)置窗口背景透明
self.my_MainWindow.setWindowFlag(Qt.FramelessWindowHint) # 隱藏邊框
# self.my_ui.graphicsView.setStyleSheet("border:none;")
self.my_ui.graphicsView.setStyleSheet("border:none;background-color:white;")
##############################################################################
self.my_ui.pushButton.setIcon(qtawesome.icon('fa.check-square',color='black'))
self.my_ui.pushButton_2.setIcon(qtawesome.icon('fa.legal', color='black'))
self.my_ui.pushButton.setStyleSheet(
'''QPushButton{background:#F7D674;border-radius:5px;}QPushButton:hover{background:yellow;}''')
self.my_ui.pushButton_2.setStyleSheet(
'''QPushButton{background:#6DDF6D;border-radius:5px;}QPushButton:hover{background:green;}''')
self.dd=0
####################################################################手動
self.scene = QGraphicsScene() # 創(chuàng)建場景
self.my_ui.graphicsView.setScene(self.scene) # 將場景加入到視圖中顯示出來
self.my_ui.graphicsView.setRenderHint(QPainter.Antialiasing) ##設(shè)置視圖的抗鋸齒渲染模式。
####################################################################
self.timer = QTimer()
self.timer.timeout.connect(self.showTime)
self.click_pushbutton()
#####################################################################
self.my_MainWindow.show()
exit(app.exec_())
def click_pushbutton(self):
self.my_ui.pushButton.clicked.connect(self.begin)
self.my_ui.pushButton_2.clicked.connect(self.close)
def close(self):
self.my_MainWindow.close()
def showTime(self):
self.dd = self.dd + 1
self.scene.clear()
xfloat = arange(-3.3 ** 0.5, 3.3 ** 0.5, 0.0001)
yfloat = [abs(xx) ** (2 / 3) + 0.9 * (3.3 - xx ** 2) ** 0.5 * sin(self.dd * pi * xx) for xx in xfloat]
xint = []
yint = []
for i in range(0, len(xfloat)):
xint.append(750- int((xfloat[i] + 2) * 750 / 4))
yint.append(int(750 - (yfloat[i] + 3) * 750 / 6))
self.item = KEYTypeItem(xint, yint) # 創(chuàng)建像素圖元
self.item.setPos(0, 0)
self.scene.addItem(self.item) # 將圖元添加到場景中
def begin(self):
self.dd=0
self.timer.start(100)
self.my_ui.pushButton.setEnabled(False)
_thread.start_new_thread(self.print_time, ("Thread-2",))
def print_time(self,threadName):
while 1:
if self.dd==80:
self.timer.stop()
self.my_ui.pushButton.setEnabled(True)
break
class KEYTypeItem(QGraphicsItem):
def __init__(self,x,y):
super(KEYTypeItem, self).__init__()
self.myx=x
self.myy=y
def boundingRect(self):
return QRectF(0, 0, 750, 750)
def paint(self, painter, option, widget):
painter.setPen(QColor(245, 12, 231))
for i in range(0,len(self.myx)):
painter.drawPoint(self.myx[i],self.myy[i])
'''
.drawPie(0,0,95,95,0*16,120*16)繪制扇形
.drawArc(0,0,95,95,30*16,120*16)繪制圓弧
.drawText(50,50,"文字")繪制文本
.drawRect(0,0,95,95)繪制矩形
.drawLine(0,0,0,95) 繪制直線
.drawEllipse(0, 0, 95, 95)繪制橢圓'''
if __name__ == "__main__":
my_mainwindow()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
和孩子一起學(xué)習(xí)python之變量命名規(guī)則
這篇文章我們給大家總結(jié)了關(guān)于兒童學(xué)習(xí)python中的變量命名規(guī)則相關(guān)知識點(diǎn)內(nèi)容,有興趣的朋友跟著參考學(xué)習(xí)下。2018-05-05
python實(shí)現(xiàn)將html表格轉(zhuǎn)換成CSV文件的方法
這篇文章主要介紹了python實(shí)現(xiàn)將html表格轉(zhuǎn)換成CSV文件的方法,涉及Python操作csv文件的相關(guān)技巧,需要的朋友可以參考下2015-06-06
windows上安裝Anaconda和python的教程詳解
本文主要給大家介紹windows上安裝Anaconda和python的教程詳解,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-03-03
利用Django提供的ModelForm增刪改數(shù)據(jù)的方法
這篇文章主要介紹了利用Django提供的ModelForm增刪改數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
python可視化篇之流式數(shù)據(jù)監(jiān)控的實(shí)現(xiàn)
這篇文章主要介紹了python可視化篇之流式數(shù)據(jù)監(jiān)控的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
python實(shí)現(xiàn)flappy bird小游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)flappy bird小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12

