本文共529字,预计阅读需要2分钟

今日阅读:

今日软件:

哪吒探针

简单高效有用的VPS探针,我现在手头三个VPS挨个看确实看不过来了,还是有必要安装一个面板的。

今日代码:

Flutter & Dart - The Complete Guide [2023Edition]

继续学习Flutter中,发现这个类的概念有点难懂和复杂,小组件是怎么调用又怎么生成的,一直没转过来。

还有super的概念也听的云里雾里,得先暂停下来专项攻克了。

附带一个使用最近逆向出来的动漫之家图床api,随手搓的GUI图床小软件。

主要是为了测试这个代码块的展示效果。

主程序部分:

image_uploader_app.py
import tkinter as tk
from tkinter import filedialog, messagebox, ttk, Label
import requests
import threading
import json
from PIL import Image, ImageTk
import io
from history_manager import HistoryManager
 
class ImageUploaderApp:
    def __init__(self, root):
        self.root = root
        root.title("图床软件")
        self.create_widgets()
        self.history_manager = HistoryManager()
        self.load_history()
 
    def create_widgets(self):
        tk.Button(self.root, text="选择图片并上传", command=self.upload_images).pack(pady=10)
 
        self.history_frame = ttk.Frame(self.root)
        self.history_frame.pack(pady=10, fill='both', expand=True)
        self.history_list = ttk.Treeview(self.history_frame, columns=('文件路径', '网址'), show='headings')
        self.history_list.column('文件路径', width=250, anchor='center')
        self.history_list.column('网址', width=250, anchor='center')
        self.history_list.heading('文件路径', text='文件路径')
        self.history_list.heading('网址', text='网址')
        self.history_list.pack(fill='both', expand=True)
 
        self.history_list.bind("<Double-1>", self.on_history_click)
 
        self.menu = tk.Menu(self.root, tearoff=0)
        self.menu.add_command(label="复制链接", command=self.copy_link)
        self.history_list.bind("<Button-3>", self.show_context_menu)
 
    def upload_images(self):
        file_paths = filedialog.askopenfilenames(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg")])
        if file_paths:
            for file_path in file_paths:
                self.upload_image(file_path)
 
    def upload_image(self, file_path):
        threading.Thread(target=self.upload_image_thread, args=(file_path,)).start()
 
    def upload_image_thread(self, file_path):
        url = 'https://upload-forum.idmzj.com/php/controller?action=uploadimage'
        files = {'upfile': open(file_path, 'rb')}
        try:
            response = requests.post(url, files=files)
            response.raise_for_status()
            response_data = json.loads(response.text)
            image_url = response_data.get('url')
            self.history_manager.add_record(file_path, image_url)
            self.root.after(0, self.update_history_view)
        except requests.RequestException as e:
            messagebox.showerror("上传失败", f"无法上传图片:{file_path}\n错误信息:{e}")
        finally:
            files['upfile'].close()
 
    def update_history_view(self):
        self.history_list.delete(*self.history_list.get_children())
        for record in self.history_manager.get_records():
            self.history_list.insert('', 'end', values=(record['file_path'], record['url']))
 
    def on_history_click(self, event):
        item = self.history_list.selection()[0]
        url = self.history_list.item(item, 'values')[1]
        self.show_image_preview(url)
 
    def show_image_preview(self, url):
        preview_window = tk.Toplevel(self.root)
        preview_window.title("图片预览")
 
        try:
            response = requests.get(url, stream=True)
            response.raise_for_status()
            img_data = response.content
            img = Image.open(io.BytesIO(img_data))
            img.thumbnail((800, 800), Image.ANTIALIAS)
            photo = ImageTk.PhotoImage(img)
            label = Label(preview_window, image=photo)
            label.image = photo
            label.pack()
        except requests.RequestException as e:
            messagebox.showerror("预览失败", f"无法加载图片:{url}\n错误信息:{e}")
 
    def show_context_menu(self, event):
        item = self.history_list.identify_row(event.y)
        if item:
            self.history_list.selection_set(item)
            self.menu.post(event.x_root, event.y_root)
 
    def copy_link(self):
        selected_item = self.history_list.selection()[0]
        url = self.history_list.item(selected_item, 'values')[1]
        self.root.clipboard_clear()
        self.root.clipboard_append(url)
        messagebox.showinfo("复制", "链接已复制到剪贴板")
 
    def load_history(self):
        self.history_manager.load_history()
        self.update_history_view()
 
if __name__ == "__main__":
    root = tk.Tk()
    app = ImageUploaderApp(root)
    root.mainloop()

保存历史记录的模块:

history_manager.py
import json
 
class HistoryManager:
    def __init__(self):
        self.history_file = "upload_history.json"
        self.history = []
 
    def add_record(self, file_path, url):
        self.history.append({"file_path": file_path, "url": url})
        self.save_history()
 
    def get_records(self):
        return self.history
 
    def save_history(self):
        with open(self.history_file, 'w') as file:
            json.dump(self.history, file)
 
    def load_history(self):
        try:
            with open(self.history_file, 'r') as file:
                self.history = json.load(file)
        except FileNotFoundError:
            self.history = []

今日见闻:

美国将上线首个全Ai主播的新闻频道Channel 1。

字节反驳自己没有盗用Openai的数据训练模型。

今日废话:

我昨天都干嘛来着?印象最深的是看了一集黑镜,就是第一集首相日猪。

互联网风暴下的政治家也只是一个普通人,最后一幕对妻子的那句话和角度拍的太好了。

让我想起电影《搜索》,看了下黑镜第一集播放于2011年,那比搜索还要早的。