吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1736|回复: 26
收起左侧

[Python 原创] 分辨率计算器 V1.2

[复制链接]
sexcat 发表于 2024-2-25 18:59
本帖最后由 sexcat 于 2024-2-27 11:42 编辑

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

更新了一版,加上了清除结果的按钮。但是运行起来还是有报错
请朋友们帮忙看看什么原因:
image.png

又更新了一版。换了字体,不会糊了。调整了下框体组件的间隔,略微美观了点
image.png

[Python] 纯文本查看 复制代码
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[0]), float(ratio[1])
    
    # 获取新宽度和新高度的输入
    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)

def clear_results():
    # 当按下清除按钮时,清空原始分辨率输入区的内容
    new_width_entry.delete(0, tk.END)
    new_height_entry.delete(0, tk.END)

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

# 设置样式
style = ttk.Style()
style.configure('TLabel', font=('宋体', 10))
style.configure('TButton', font=('宋体', 10))
style.configure('TRadiobutton', font=('宋体', 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.2", font=('宋体', 12, 'bold'))
title_label.grid(column=0, row=0, columnspan=4, pady=(0, 5))

# 快捷选择区域
selected_option = tk.StringVar(value='custom')
options_frame = ttk.Frame(content_frame)
options_frame.grid(column=0, row=1, columnspan=4, pady=(5, 5), 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)

# 在content_frame中创建一个新的Frame用于放置按钮
buttons_frame = ttk.Frame(content_frame)
buttons_frame.grid(column=0, row=7, columnspan=4, pady=(10, 5), sticky='EW')

# 在buttons_frame中添加“清除结果”按钮,并通过pack使其居中
clear_button = ttk.Button(buttons_frame, text="清除结果", command=clear_results)
clear_button.pack(side='left', padx=5, expand=True)

# 在buttons_frame中添加“开始生成”按钮,并通过pack使其居中
calculate_button = ttk.Button(buttons_frame, text="开始生成", command=calculate_new_dimension)
calculate_button.pack(side='left', padx=5, expand=True)

# 设置buttons_frame内部的水平拉伸,以便按钮可以居中显示
buttons_frame.columnconfigure(0, weight=1)

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

root.mainloop()


https://wwf.lanzoul.com/iXs9Q1piao7g
密码:7i20

免费评分

参与人数 9吾爱币 +13 热心值 +7 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
shengruqing + 1 我很赞同!
xingdh + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
ysy2001 + 1 + 1 谢谢@Thanks!
蓝天伯爵 + 1 用心讨论,共获提升!
blindcat + 1 + 1 谢谢@Thanks!
wuaiwxh + 1 我很赞同!
yanglinman + 1 谢谢@Thanks!
collinchen1218 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

sai609 发表于 2024-2-25 22:06
咱们用的是pycharm
hao6988456 发表于 2024-2-25 22:32
Corgibro 发表于 2024-2-25 23:13
loovo 发表于 2024-2-25 23:15
好东西,很方便
youseeseeyou 发表于 2024-2-26 00:01
简单实用,已收藏
meiguihudieyu 发表于 2024-2-26 07:22
不错不错,谢谢分享
13932140048 发表于 2024-2-26 07:26
谢谢分享!!!!!
xiaomianao 发表于 2024-2-26 07:38
看着就不明觉厉的感觉
blindcat 发表于 2024-2-26 07:47
有点意思
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 14:39

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表