[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()