最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)GUI圖片瀏覽的小程序

 更新時(shí)間:2023年12月15日 11:01:08   作者:軟件技術(shù)愛(ài)好者  
這篇文章主要介紹了Python實(shí)現(xiàn)GUI圖片瀏覽程序,程序的實(shí)現(xiàn)需要pillow庫(kù),pillow是 Python 的第三方圖像處理庫(kù),需要安裝才能實(shí)用,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下

下面程序需要pillow庫(kù)。pillow是 Python 的第三方圖像處理庫(kù),需要安裝才能實(shí)用。pillow是PIL( Python Imaging Library)基礎(chǔ)上發(fā)展起來(lái)的,需要注意的是pillow庫(kù)安裝用pip install pillow,導(dǎo)包時(shí)要用PIL來(lái)導(dǎo)入。

一、簡(jiǎn)單的圖片查看程序

功能,使用了tkinter庫(kù)來(lái)創(chuàng)建一個(gè)窗口,用戶可以通過(guò)該窗口選擇一張圖片并在窗口中顯示。能調(diào)整窗口大小以適應(yīng)圖片。效果圖如下:

源碼如下:

import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
 
# 創(chuàng)建一個(gè)Tkinter窗口
root = tk.Tk()
root.geometry("400x300")  # 設(shè)置寬度為400像素,高度為300像素
root.title("Image Viewer")
 
# 添加一個(gè)按鈕來(lái)選擇圖片
def open_image():
    try:
        file_path = filedialog.askopenfilename()
        if file_path:
            image = Image.open(file_path)
            photo = ImageTk.PhotoImage(image)
 
            # 清除舊圖片
            for widget in root.winfo_children():
                if isinstance(widget, tk.Label):
                    widget.destroy()
            
            label = tk.Label(root, image=photo)
            label.image = photo
            label.pack()
 
            # 調(diào)整窗口大小以適應(yīng)圖片
            root.geometry("{}x{}".format(image.width, image.height))
    except AttributeError:
        print("No image selected.")
 
button = tk.Button(root, text="Open Image", command=open_image)
button.pack()
 
# 運(yùn)行窗口
root.mainloop()

此程序,創(chuàng)建一個(gè)tkinter窗口,設(shè)置窗口的大小為400x300像素,并設(shè)置窗口標(biāo)題為"Image Viewer"。

添加一個(gè)按鈕,當(dāng)用戶點(diǎn)擊該按鈕時(shí),會(huì)彈出文件選擇對(duì)話框,用戶可以選擇一張圖片文件。

選擇圖片后,程序會(huì)使用PIL庫(kù)中的Image.open方法打開(kāi)所選的圖片文件,并將其顯示在窗口中。

程序會(huì)在窗口中顯示所選的圖片,并在用戶選擇新圖片時(shí)清除舊圖片。

示例中,使用try-except塊來(lái)捕獲FileNotFoundError,該錯(cuò)誤會(huì)在用戶取消選擇圖片時(shí)觸發(fā)。當(dāng)用戶取消選擇圖片時(shí),會(huì)打印一條消息提示用戶沒(méi)有選擇圖片。這樣就可以避免因?yàn)槿∠x擇圖片而導(dǎo)致的報(bào)錯(cuò)。

二、圖片查看程序1

“Open Directory”按鈕用于指定一個(gè)目錄,窗體上再添加兩個(gè)按鈕:“Previous Image” 和“Next Image”,單擊這兩個(gè)按鈕實(shí)現(xiàn)切換顯示指定目錄中的圖片。這三個(gè)按鈕水平排列在頂部,在下方顯示圖片。如果所選圖片的尺寸超過(guò)了窗口的大小,程序會(huì)將圖片縮放到合適的尺寸以適應(yīng)窗口。效果圖如下:

源碼如下:

import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import os
 
class ImageViewer:
    def __init__(self, root):
        self.root = root
        self.root.geometry("400x350")
        self.root.title("Image Viewer")
 
        self.image_dir = ""
        self.image_files = []
        self.current_index = 0
 
        # 創(chuàng)建頂部按鈕框架
        self.button_frame = tk.Frame(self.root)
        self.button_frame.pack(side="top")
 
        # 創(chuàng)建打開(kāi)目錄按鈕
        self.open_button = tk.Button(self.button_frame, text="Open Directory", command=self.open_directory)
        self.open_button.pack(side="left")
 
        # 創(chuàng)建上一張圖片按鈕
        self.prev_button = tk.Button(self.button_frame, text="Previous Image", command=self.show_previous_image)
        self.prev_button.pack(side="left")
 
        # 創(chuàng)建下一張圖片按鈕
        self.next_button = tk.Button(self.button_frame, text="Next Image", command=self.show_next_image)
        self.next_button.pack(side="left")
 
        # 創(chuàng)建圖片顯示區(qū)域
        self.image_label = tk.Label(self.root)
        self.image_label.pack()
 
 
    def open_directory(self):
        try:
            self.image_dir = filedialog.askdirectory()
            if self.image_dir:
                self.image_files = [f for f in os.listdir(self.image_dir) if f.endswith(".jpg") or f.endswith(".png") or f.endswith(".jfif")]
                self.current_index = 0
                self.show_image()
        except tk.TclError:
            print("No directory selected.")
 
    def show_image(self):
        if self.image_files:
            image_path = os.path.join(self.image_dir, self.image_files[self.current_index])
            image = Image.open(image_path)
            image.thumbnail((400, 300), Image.ANTIALIAS)
            photo = ImageTk.PhotoImage(image)
            self.image_label.config(image=photo)
            self.image_label.image = photo
 
    def show_previous_image(self):
        if self.image_dir:
            if self.image_files:
                self.current_index = (self.current_index - 1) % len(self.image_files)
                self.show_image()
            else:
                print("Please open a directory first.")
        else:
            print("Please open a directory first.")
 
    def show_next_image(self):
        if self.image_dir:
            if self.image_files:
                self.current_index = (self.current_index + 1) % len(self.image_files)
                self.show_image()
            else:
                print("Please open a directory first.")
        else:
            print("Please open a directory first.")
 
root = tk.Tk()
app = ImageViewer(root)
root.mainloop()

三、圖片查看程序2

窗體上有3個(gè)控件,列表框和按鈕和在窗體上左側(cè)上下放置,右側(cè)區(qū)域顯示圖片, “Open Directory”按鈕用于指定目錄中,列表用于放置指定目錄中的所有圖片文件名,點(diǎn)擊列表中的圖片文件名,圖片在右側(cè)不變形縮放顯示到窗體上(圖片縮放到合適的尺寸以適應(yīng)窗口),效果圖如下:

源碼如下:

import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import os
 
# 創(chuàng)建主窗口
root = tk.Tk()
root.geometry("600x300")
root.title("Image Viewer")
 
# 創(chuàng)建一個(gè)Frame來(lái)包含按鈕和列表框
left_frame = tk.Frame(root)
left_frame.pack(side=tk.LEFT, fill=tk.BOTH, padx=5, pady=5)
 
# 創(chuàng)建一個(gè)Frame來(lái)包含圖片顯示區(qū)域
right_frame = tk.Frame(root)
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
 
# 創(chuàng)建一個(gè)列表框來(lái)顯示文件名
listbox = tk.Listbox(left_frame)
listbox.pack(fill=tk.BOTH, expand=True)
 
# 創(chuàng)建一個(gè)滾動(dòng)條并將其與列表框關(guān)聯(lián)
scrollbar = tk.Scrollbar(root, orient=tk.VERTICAL)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
scrollbar.config(command=listbox.yview)
listbox.config(yscrollcommand=scrollbar.set)
 
# 創(chuàng)建一個(gè)標(biāo)簽來(lái)顯示圖片
image_label = tk.Label(right_frame)
image_label.pack(fill=tk.BOTH, expand=True)
 
# 函數(shù):打開(kāi)目錄并列出圖片文件
def open_directory():
    directory = filedialog.askdirectory()
    if directory:
        # 清空列表框
        listbox.delete(0, tk.END)
        # 列出目錄中的所有圖片文件
        for file in os.listdir(directory):
            if file.lower().endswith(('.jpg', '.jpeg', '.png', '.gif','.jfif')):
                listbox.insert(tk.END, file)
        # 保存當(dāng)前目錄
        open_directory.current_directory = directory
 
# 函數(shù):在右側(cè)顯示選中的圖片
def show_selected_image(event):
    if not hasattr(open_directory, 'current_directory'):
        return
    # 獲取選中的文件名
    selected_file = listbox.get(listbox.curselection())
    # 構(gòu)建完整的文件路徑
    file_path = os.path.join(open_directory.current_directory, selected_file)
    # 打開(kāi)圖片并進(jìn)行縮放
    image = Image.open(file_path)
    image.thumbnail((right_frame.winfo_width(), right_frame.winfo_height()), Image.ANTIALIAS)
    # 用PIL的PhotoImage顯示圖片
    photo = ImageTk.PhotoImage(image)
    image_label.config(image=photo)
    image_label.image = photo  # 保存引用,防止被垃圾回收
 
# 創(chuàng)建“Open Directory”按鈕
open_button = tk.Button(left_frame, text="Open Directory", command=open_directory)
open_button.pack(fill=tk.X)
 
# 綁定列表框選擇事件
listbox.bind('<<ListboxSelect>>', show_selected_image)
 
# 運(yùn)行主循環(huán)
root.mainloop()

以上就是Python實(shí)現(xiàn)GUI圖片瀏覽的小程序的詳細(xì)內(nèi)容,更多關(guān)于Python GUI圖片瀏覽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 基于OpenCV實(shí)現(xiàn)視頻循環(huán)播放

    基于OpenCV實(shí)現(xiàn)視頻循環(huán)播放

    這篇文章主要為大家介紹了如何利用OpenCV實(shí)現(xiàn)視頻的循環(huán)播放,本文為大家提供了兩種方式,一個(gè)是利用Python語(yǔ)言實(shí)現(xiàn),一個(gè)是利用C++語(yǔ)言實(shí)現(xiàn),需要的可以參考一下
    2022-02-02
  • Python進(jìn)程和線程之多線程的使用及說(shuō)明

    Python進(jìn)程和線程之多線程的使用及說(shuō)明

    Python多線程通過(guò)threading模塊實(shí)現(xiàn),主線程默認(rèn)運(yùn)行,子線程由Thread類創(chuàng)建并啟動(dòng),線程共享變量易引發(fā)數(shù)據(jù)混亂,需用Lock同步,但鎖可能降低效率或?qū)е滤梨i,(79字)
    2025-09-09
  • Python3.5 Pandas模塊之Series用法實(shí)例分析

    Python3.5 Pandas模塊之Series用法實(shí)例分析

    這篇文章主要介紹了Python3.5 Pandas模塊之Series用法,結(jié)合實(shí)例形式分析了Python3.5中Pandas模塊的Series結(jié)構(gòu)原理、創(chuàng)建、獲取、運(yùn)算等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-04-04
  • Python數(shù)據(jù)集切分實(shí)例

    Python數(shù)據(jù)集切分實(shí)例

    今天小編就為大家分享一篇Python數(shù)據(jù)集切分實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • python實(shí)現(xiàn)將文本轉(zhuǎn)換成語(yǔ)音的方法

    python實(shí)現(xiàn)將文本轉(zhuǎn)換成語(yǔ)音的方法

    這篇文章主要介紹了python實(shí)現(xiàn)將文本轉(zhuǎn)換成語(yǔ)音的方法,涉及Python中pyTTS模塊的相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • 在Python中使用MySQL--PyMySQL的基本使用方法

    在Python中使用MySQL--PyMySQL的基本使用方法

    PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務(wù)器的一個(gè)庫(kù),Python2中則使用mysqldb。這篇文章主要介紹了在Python中使用MySQL--PyMySQL的基本使用,需要的朋友可以參考下
    2019-11-11
  • PyCharm 設(shè)置SciView工具窗口的方法

    PyCharm 設(shè)置SciView工具窗口的方法

    今天小編就為大家分享一篇PyCharm 設(shè)置SciView工具窗口的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • 最簡(jiǎn)單的matplotlib安裝教程(小白)

    最簡(jiǎn)單的matplotlib安裝教程(小白)

    這篇文章主要介紹了最簡(jiǎn)單的matplotlib安裝教程(小白),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Django2.1.7 查詢數(shù)據(jù)返回json格式的實(shí)現(xiàn)

    Django2.1.7 查詢數(shù)據(jù)返回json格式的實(shí)現(xiàn)

    這篇文章主要介紹了Django2.1.7 查詢數(shù)據(jù)返回json格式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Python ORM框架SQLAlchemy學(xué)習(xí)筆記之安裝和簡(jiǎn)單查詢實(shí)例

    Python ORM框架SQLAlchemy學(xué)習(xí)筆記之安裝和簡(jiǎn)單查詢實(shí)例

    這篇文章主要介紹了Python ORM框架SQLAlchemy學(xué)習(xí)筆記之安裝和簡(jiǎn)單查詢實(shí)例,簡(jiǎn)明入門教程,需要的朋友可以參考下
    2014-06-06

最新評(píng)論

三门县| 建瓯市| 香港| 房山区| 兰坪| 伊川县| 横山县| 诸暨市| 福泉市| 民乐县| 宁津县| 淮北市| 大邑县| 崇左市| 贵州省| 达州市| 北碚区| 纳雍县| 桑植县| 婺源县| 信阳市| 景德镇市| 镇坪县| 镇雄县| 商丘市| 靖安县| 当涂县| 运城市| 阿克苏市| 正定县| 高州市| 青田县| 民勤县| 高唐县| 运城市| 凌云县| 阿拉善盟| 天镇县| 青神县| 海门市| 民和|