Honeys 发表于 2024-2-26 02:28:34

分辨率计算器

最近经常要切照片,写了个分辨率计算器,可以按原图比例计算需要的比例,但是打包运行后出现了错误提示,我代码里没用用过png图片啊?
我的环境是windows10,PY3.12

请朋友们帮忙看看什么原因:




import tkinter as tk
from tkinter import ttk

def calculate_new_dimension():
    # 根据选择或输入获取原始分辨率
    if selected_option.get() == 'custom':
      original_width = float(original_width_entry.get())
      original_height = float(original_height_entry.get())
    else:
      ratio = selected_option.get().split(':')
      original_width, original_height = float(ratio), float(ratio)
   
    # 获取新宽度和新高度的输入
    new_width = new_width_entry.get()
    new_height = new_height_entry.get()

    # 如果新宽度有输入,则计算新高度
    if new_width:
      new_width = float(new_width)
      calculated_height = (new_width / original_width) * original_height
      new_height_entry.delete(0, tk.END)
      new_height_entry.insert(0, str(round(calculated_height)))
    # 如果新高度有输入,则计算新宽度
    elif new_height:
      new_height = float(new_height)
      calculated_width = (new_height / original_height) * original_width
      new_width_entry.delete(0, tk.END)
      new_width_entry.insert(0, str(round(calculated_width)))

def update_entry_state():
    # 当选择快捷选项时,清空原始分辨率输入区的内容
    if selected_option.get() != 'custom':
      original_width_entry.delete(0, tk.END)
      original_height_entry.delete(0, tk.END)
    # 根据选择启用或禁用原始分辨率输入框
    state = 'normal' if selected_option.get() == 'custom' else 'disabled'
    original_width_entry.config(state=state)
    original_height_entry.config(state=state)

# 创建主窗口
root = tk.Tk()
root.title("分辨率计算器 V1.0")

# 设置样式
style = ttk.Style()
style.configure('TLabel', font=('Arial', 10))
style.configure('TButton', font=('Arial', 10))
style.configure('TRadiobutton', font=('Arial', 10))

# 设置整体布局的内边距
content_frame = ttk.Frame(root, padding="10 10 10 10")# 上下左右的内边距
content_frame.grid(column=0, row=0, sticky=('N', 'W', 'E', 'S'))
content_frame.columnconfigure(0, weight=1)
content_frame.rowconfigure(0, weight=1)

# 软件名字标签
title_label = ttk.Label(content_frame, text="分辨率计算器V1.0", font=('Arial', 12, 'bold'))
title_label.grid(column=0, row=0, columnspan=4, pady=(10, 20))

# 快捷选择区域
selected_option = tk.StringVar(value='custom')
options_frame = ttk.Frame(content_frame)
options_frame.grid(column=0, row=1, columnspan=4, sticky='ew')
options = {'custom': '手动', '4:3': '4:3', '16:9': '16:9', '9:16': '9:16'}
for value, text in options.items():
    radio_button = ttk.Radiobutton(options_frame, text=text, value=value, variable=selected_option, command=update_entry_state)
    radio_button.pack(side='left', expand=True)

# 原始分辨率输入区
ttk.Label(content_frame, text="原始宽度").grid(column=0, row=2, sticky='w')
original_width_entry = ttk.Entry(content_frame)
original_width_entry.grid(column=1, row=2, sticky='ew', columnspan=3)
ttk.Label(content_frame, text="原始高度").grid(column=0, row=3, sticky='w')
original_height_entry = ttk.Entry(content_frame)
original_height_entry.grid(column=1, row=3, sticky='ew', columnspan=3)

# 分隔线
separator = ttk.Separator(content_frame, orient='horizontal')
separator.grid(column=0, row=4, columnspan=4, sticky='ew', pady=10)

# 新分辨率输入区
ttk.Label(content_frame, text="新宽度").grid(column=0, row=5, sticky='w')
new_width_entry = ttk.Entry(content_frame)
new_width_entry.grid(column=1, row=5, sticky='ew', columnspan=3)
ttk.Label(content_frame, text="新高度").grid(column=0, row=6, sticky='w')
new_height_entry = ttk.Entry(content_frame)
new_height_entry.grid(column=1, row=6, sticky='ew', columnspan=3)

# 计算按钮
calculate_button = ttk.Button(content_frame, text="开始生成", command=calculate_new_dimension)
calculate_button.grid(column=0, row=7, columnspan=4, pady=(10, 5))

# 作者名字标签
author_label = ttk.Label(content_frame, text="By: icescat 2024.2.25", foreground="grey", font=('Arial', 7))
author_label.grid(column=0, row=8, columnspan=4, pady=(0, 5))

root.mainloop()

keyishiyong 发表于 2024-2-29 07:28:21

这个很有用,可以下载收藏
页: [1]
查看完整版本: 分辨率计算器