PyQt5簡單讀取以及顯示圖片的應(yīng)用實例
前言
本文為PyQt5入門教程,具體為以下四步驟
一、程序界面簡單設(shè)計
二、通過下拉列表框讀取讀取指定路徑的圖片
三、通過讀取到的圖片顯示在界面上
四、退出事件
最終效果如下:
一、程序界面簡單設(shè)計
程序初始構(gòu)成如下
#利用PyQt建立界面必須使用QApplication,代表整個應(yīng)用程序,是整個Qt的命脈,主要負責(zé)應(yīng)用程序的初始化和結(jié)束 import sys #引用sys庫 from PyQt5 import QtWidgets #引用PyQt5庫里QtWidgets類 from PyQt5.QtWidgets import * #導(dǎo)入PyQt5.QtWidgets里所有的方法 from PyQt5.QtGui import * #導(dǎo)入PyQt5.QtGui里所有的方法 import os #引用os庫 class Qt_Window(QWidget): #定義一個類,繼承于QWidget def __init__(self): #構(gòu)建方法 self._app = QtWidgets.QApplication([]) #創(chuàng)建QApplication示例 super(Qt_Window,self).__init__() #固定格式 def init_ui(self): #定義方法,在該方法里構(gòu)建界面組件 self.win = QMainWindow() self.win.showFullScreen() #窗口全屏顯示,不帶標題欄和邊框 sys.exit(self._app.exec_()) #sys.exit()退出程序機制 app.exec_()的作用是運行主循環(huán),開始進行事件處理,直到結(jié)束 s = Qt_Window() #調(diào)用對象和方法 s.init_ui() #調(diào)用對象和方法
在 init_ui內(nèi)開始界面布局(注:分辨率為1920 × 1080)
def init_ui(self): self.win = QMainWindow() #定義組件 self.comboBox = QComboBox(self.win) #下拉列表框 self.number_lable = QLabel(self.win) #標簽 self.open_Button = QPushButton(self.win) #退出按鈕 self.detect_image = QLabel(self.win) #圖片(目前為標簽控件,需要后續(xù)將其轉(zhuǎn)換為圖片控件) #設(shè)置控件 self.comboBox.resize(200,50) self.comboBox.move(600,800) self.open_Button.resize(200,50) self.open_Button.move(1000,800) self.open_Button.setText("退出") self.detect_image.resize(700,550) self.detect_image.move(550,100) self.number_lable.resize(200,50) self.number_lable.move(900,700) #resize為設(shè)置大小,move為設(shè)置x與y坐標,setText為設(shè)置控件文本內(nèi)容 self.win.showFullScreen() sys.exit(self._app.exec_())
結(jié)果應(yīng)為
注:此時的退出按鈕沒有效果,可自行退出界面
當前布局代碼:
import sys from PyQt5 import QtWidgets from PyQt5.QtWidgets import * from PyQt5.QtGui import * import os class Qt_Window(QWidget): def __init__(self): self._app = QtWidgets.QApplication([]) super(Qt_Window,self).__init__() def init_ui(self): self.win = QMainWindow() self.comboBox = QComboBox(self.win) self.number_lable = QLabel(self.win) self.open_Button = QPushButton(self.win) self.detect_image = QLabel(self.win) self.comboBox.resize(200,50) self.comboBox.move(600,800) self.open_Button.resize(200,50) self.open_Button.move(1000,800) self.open_Button.setText("退出") self.detect_image.resize(700,550) self.detect_image.move(550,100) self.number_lable.resize(200,50) self.number_lable.move(900,700) self.win.showFullScreen() sys.exit(self._app.exec_()) s = Qt_Window() s.init_ui()
二、通過下拉列表框讀取指定路徑上的圖片
1.設(shè)置路徑
一般情況下最好設(shè)置為絕對路徑,在init_ui內(nèi)輸入以下:
self.path = ("C:\\123\\picture\\")
設(shè)置路徑的時候一定要注意要輸入“\\”而非單個“\”,單個“\”將會對“\”后的第一個字母進行轉(zhuǎn)義!
2.將讀取到的圖片名稱添加到下拉列表框內(nèi)
使用os.listdir()指令,將文件夾內(nèi)的所有文件讀取出來
self.img_list = os.listdir(self.path)
此時可以嘗試輸出一下這個self.img_list
(文件夾內(nèi))
(輸出結(jié)果)
可以看出結(jié)果是符合的,并且返回出來的結(jié)果為列表,倘若要將此結(jié)果添加到下拉列表框內(nèi),那么應(yīng)該使用for ... in ... 循環(huán)將數(shù)據(jù)代入
self.comboBox.addItems([self.img_list[i] for i in range(len(self.img_list))])
以上代碼的意思為:將self.img_list這個列表內(nèi)的數(shù)據(jù)以此添加到下拉列表框內(nèi)。
self.comboBox.addItems()為添加多個列表
for i in range(len(self.img_list))先通過len算出self.img_list的長度為3,再利用range將其轉(zhuǎn)換為0-3的區(qū)間,然后將0-3的數(shù)字以此代入i后,再將self.img_list[i]里的數(shù)據(jù)以此添加到里面。
如:self.img_list[0]為天空.jpg;self.img_list[1]為海洋.jpg
注意:range()輸出的是一串連續(xù)的數(shù)據(jù),而len()輸出的是單個數(shù)據(jù),所以當放入循環(huán)操作時,range()會進行多次循環(huán),而len()只會進行一次循環(huán)!
此時打開串口可以看到下拉列表框內(nèi)已添加了路徑內(nèi)所有的圖片
3.將讀取到的圖片展示到界面上
我們需要實時監(jiān)測下拉列表框選擇了哪個圖片,此時我們可以自行定義一個選擇列表事件
def show_img(self): img = self.comboBox.currentText() pix = QPixmap(self.path + "\\" +img) #需額外添加"\\"否則輸出為C:\123\picture093056.jpg self.detect_image.setPixmap(pix) self.detect_image.setScaledContents(True) self.number_lable.setText(img)
首先通過currentText()函數(shù)獲取到當前下拉列表框內(nèi)的文字(圖片名稱),再將所獲取到的圖片名稱再加上路徑,就可以利用QPixmap來展示出來。將pix變成你選擇的圖片,再利用setPixmap函數(shù)將界面上的detect_image控件從標簽轉(zhuǎn)換成圖片。
由于圖片的分辨率不為統(tǒng)一,所以可以加上setScaledContents(True)這個函數(shù)將其設(shè)置為自適應(yīng)大小(之前設(shè)置界面時所設(shè)置的控件大?。?/p>
self.number_lable.setText(img)是將讀取到的圖片名稱輸出到界面上
此時還只是定義了這個事件,我們需要將這個事件和下拉列表框進行綁定,在init_ui下輸入以下代碼
self.comboBox.activated.connect(self.show_img)
此時打開界面并選擇圖片可以看到以下結(jié)果
如果想將海洋.jpg更改為海洋,可以在選擇列表事件內(nèi)將設(shè)置標簽文本的代碼修改為以下
lable = img.split(".")[0] self.number_lable.setText(lable)
通過split(“.”)指令將海洋.jpg中以“.”為分隔符輸出(海洋,jpg)列表,并通過在后方添加[0],表示僅輸出第一個也就是僅輸出為“海洋”
此時再打開界面可看見
四、退出事件
1.定義一個退出事件
自定義退出事件
def exit(self): #定義關(guān)閉事件 while True: sys.exit(0) #sys.exit(0)為正常退出 sys.exit(1)為異常退出
再將退出按鈕控件和退出事件進行綁定
self.open_Button.clicked.connect(self.exit)
此時打開界面點擊退出按鈕即可正常退出了
總結(jié)
完整代碼如下
import sys from PyQt5 import QtWidgets from PyQt5.QtWidgets import * from PyQt5.QtGui import * import os class Qt_Window(QWidget): def __init__(self): self._app = QtWidgets.QApplication([]) super(Qt_Window,self).__init__() def init_ui(self): self.win = QMainWindow() self.comboBox = QComboBox(self.win) self.number_lable = QLabel(self.win) self.open_Button = QPushButton(self.win) self.detect_image = QLabel(self.win) #設(shè)置路徑 self.path = ("C:\\123\\picture\\") self.comboBox.resize(200,50) self.comboBox.move(600,800) self.img_list = os.listdir(self.path) self.comboBox.addItems([self.img_list[i] for i in range(len(self.img_list))]) self.comboBox.activated.connect(self.show_img) self.open_Button.resize(200,50) self.open_Button.move(1000,800) self.open_Button.setText("退出") self.open_Button.clicked.connect(self.exit) self.detect_image.resize(700,550) self.detect_image.move(550,100) self.number_lable.resize(200,50) self.number_lable.move(900,700) self.win.showFullScreen() sys.exit(self._app.exec_()) def show_img(self): img = self.comboBox.currentText() pix = QPixmap(self.path + "\\" +img) self.detect_image.setPixmap(pix) self.detect_image.setScaledContents(True) lable = img.split(".")[0] self.number_lable.setText(lable) def exit(self): while True: sys.exit(0) s = Qt_Window() s.init_ui()
以上便是本人的制作過程,如遇問題或者文章有錯誤可私信或者評論
到此這篇關(guān)于PyQt5簡單讀取以及顯示圖片的文章就介紹到這了,更多相關(guān)PyQt5讀取及顯示圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解python opencv、scikit-image和PIL圖像處理庫比較
這篇文章主要介紹了詳解python opencv、scikit-image和PIL圖像處理庫比較,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12用Python監(jiān)控NASA TV直播畫面的實現(xiàn)步驟
本文分享一個名為"Spacestills"的開源程序,它可以用于查看 NASA TV 的直播畫面(靜止幀)2021-05-05