自制简单的图片查看器(python)

server/2025/2/22 4:54:52/

图片格式:支持常见的图片格式(JPG、PNG、BMP、GIF)。

python">import os
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, ImageTkclass ImageViewer:def __init__(self, root):self.root = rootself.root.title("图片查看器")self.root.geometry("800x600")self.root.configure(bg="#2E3440")  # 设置背景颜色# 当前图片路径self.current_image_path = Noneself.image = Noneself.photo = Noneself.scale_factor = 1.0self.is_auto_scaling = True  # 是否自动缩放# 创建界面self.create_widgets()def create_widgets(self):"""创建界面组件"""# 顶部工具栏toolbar = tk.Frame(self.root, bg="#3B4252")  # 工具栏背景颜色toolbar.pack(side=tk.TOP, fill=tk.X)# 打开按钮btn_open = tk.Button(toolbar, text="打开", command=self.open_image, bg="#81A1C1", fg="white", activebackground="#5E81AC", activeforeground="white", font=("微软雅黑", 12))btn_open.pack(side=tk.LEFT, padx=5, pady=5)# 放大按钮btn_zoom_in = tk.Button(toolbar, text="放大", command=self.zoom_in, bg="#81A1C1", fg="white", activebackground="#5E81AC", activeforeground="white", font=("微软雅黑", 12))btn_zoom_in.pack(side=tk.LEFT, padx=5, pady=5)# 缩小按钮btn_zoom_out = tk.Button(toolbar, text="缩小", command=self.zoom_out, bg="#81A1C1", fg="white", activebackground="#5E81AC", activeforeground="white", font=("微软雅黑", 12))btn_zoom_out.pack(side=tk.LEFT, padx=5, pady=5)# 图片显示区域self.canvas = tk.Canvas(self.root, bg="#2E3440", highlightthickness=0)self.canvas.pack(fill=tk.BOTH, expand=True)# 绑定窗口大小变化事件self.root.bind("<Configure>", self.on_window_resize)def open_image(self):"""打开图片"""file_path = filedialog.askopenfilename(title="选择图片",filetypes=[("图片文件", "*.jpg *.jpeg *.png *.bmp *.gif")])if file_path:self.current_image_path = file_pathself.load_image()def load_image(self):"""加载图片"""try:self.image = Image.open(self.current_image_path)self.scale_factor = 1.0self.is_auto_scaling = True  # 加载图片时启用自动缩放self.update_image()except Exception as e:messagebox.showerror("错误", f"无法加载图片: {str(e)}")def update_image(self):"""更新显示的图片"""if self.image:# 计算缩放后的尺寸canvas_width = self.canvas.winfo_width()canvas_height = self.canvas.winfo_height()image_width, image_height = self.image.sizeif self.is_auto_scaling:# 自动缩放时计算缩放比例width_ratio = canvas_width / image_widthheight_ratio = canvas_height / image_heightself.scale_factor = min(width_ratio, height_ratio)# 缩放图片width = int(image_width * self.scale_factor)height = int(image_height * self.scale_factor)resized_image = self.image.resize((width, height), Image.Resampling.LANCZOS)self.photo = ImageTk.PhotoImage(resized_image)# 清除画布并显示图片self.canvas.delete("all")self.canvas.create_image(canvas_width // 2,canvas_height // 2,anchor=tk.CENTER,image=self.photo)def zoom_in(self):"""放大图片"""if self.image:self.is_auto_scaling = False  # 手动缩放时禁用自动缩放self.scale_factor *= 1.2self.update_image()def zoom_out(self):"""缩小图片"""if self.image:self.is_auto_scaling = False  # 手动缩放时禁用自动缩放self.scale_factor /= 1.2self.update_image()def on_window_resize(self, event):"""窗口大小变化时自动调整图片大小"""if self.image and self.is_auto_scaling:self.update_image()if __name__ == "__main__":root = tk.Tk()app = ImageViewer(root)root.mainloop() 


http://www.ppmy.cn/server/169756.html

相关文章

vue2和vue3的按需引入的详细对比通俗易懂

以下是 Vue2 与 Vue3 按需引入的对比详解&#xff0c;用最简单的语言和场景说明差异&#xff1a; 一、按需引入的本质 目标&#xff1a;只打包项目中实际用到的代码&#xff08;组件、API&#xff09;&#xff0c;减少最终文件体积。类比&#xff1a;去餐厅点餐&#xff0c;只…

< OS 有关 > Ubuntu 24 SSH 服务器更换端口 in jp/us VPSs

原因&#xff1a; 两台 VPS 的 ssh 端口一直被密码重试&#xff0c; us 这台已经封了 632, jp 这台两周前清过一次 sqlite3 数据&#xff0c;现在赞到 1008 Fail2Ban 是使用 sqlite3 来记录&#xff0c;数据量大后&#xff0c;硬盘的 I/O 会飙升&#xff0c;我有写过一个 app…

10个Kettle(Pentaho Data Integration)基础概念面试题及答案

10个Kettle&#xff08;Pentaho Data Integration&#xff09;基础概念面试题及答案 1. 什么是Kettle&#xff1f; 答案&#xff1a; Kettle&#xff08;现称Pentaho Data Integration&#xff0c;PDI&#xff09;是一款开源的ETL&#xff08;Extract, Transform, Load&#…

springsecurity自定义认证

// jwt 方式 package com.kongjs.note.system.convert;import com.kongjs.note.admin.model.dto.TokenInfoDTO; import com.kongjs.note.admin.service.TokenService; import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletRequest; import lombok.ext…

js 打开新标签页和关闭标签页

window.open(url, _blank)&#xff0c;blank属性可以在新标签打开 window.open 直接调用可能会有拦截提示&#xff0c;在用户交互事件时调用不会出现拦截 由window.open 打开的新的标签页或弹窗可以由window.close关闭 window.open会返回一个 WindowProxy 对象。只要符合同源…

【云原生】最新版Kubernetes集群基于Containerd部署

文章目录 Kubernetes集群基于Containerd部署(单主多从模式)资源列表基础环境一、基础环境准备1.1、关闭Swap分区1.2、添加hosts解析1.3、桥接的IPv4流量传递给iptables的链 二、准备Containerd容器运行时2.1、安装Containerd2.2、配置Containerd2.3、启动Containerd 三、部署Ku…

【蓝桥杯集训·每日一题2025】 AcWing 6123. 哞叫时间 python

6123. 哞叫时间 Week 1 2月18日 农夫约翰正在试图向埃尔茜描述他最喜欢的 USACO 竞赛&#xff0c;但她很难理解为什么他这么喜欢它。 他说「竞赛中我最喜欢的部分是贝茜说 『现在是哞哞时间』并在整个竞赛中一直哞哞叫」。 埃尔茜仍然不理解&#xff0c;所以农夫约翰将竞赛以…

使用Java爬虫获取京东商品分类API接口(cat_get)的实现与解析

一、引言 在电商领域&#xff0c;商品分类数据是进行市场分析、商品管理以及用户体验优化的重要基础。京东作为国内领先的电商平台&#xff0c;其商品分类数据具有极高的商业价值。通过爬虫技术获取京东商品分类API接口&#xff08;假设接口名为cat_get&#xff09;的数据&…