python—gui-计算图像像素两点间距离

代码:

import tkinter as tk
from tkinter import ttk


def create_gui():
    # 创建Tkinter窗口
    root = tk.Tk()
    # 设置窗口标题
    root.title("显示图片")

    # 图片文件路径
    image_path = 'path_to_your_image.jpg'
    # 加载图片
    img = load_image(image_path)

    # 创建标签,并将图片作为背景
    label = tk.Label(root, image=img)
    # 放置标签
    label.pack()

    # 开始Tkinter事件循环
    root.mainloop()

def calculate_distance(x1, y1, x2, y2):
    distance = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
    label_distance.config(text=f"The distance is: {distance}")


def on_entry_change(event):
    x1 = float(entry_x1.get())
    y1 = float(entry_y1.get())
    x2 = float(entry_x2.get())
    y2 = float(entry_y2.get())
    calculate_distance(x1, y1, x2, y2)


root = tk.Tk()
root.title("Distance Calculator")

# 创建两组坐标输入框
frame_coords = tk.LabelFrame(root, text="Coordinates")
frame_coords.pack(padx=20, pady=20)

entry_x1 = ttk.Entry(frame_coords)
entry_x1.pack(side=tk.LEFT, padx=5, pady=5)
entry_y1 = ttk.Entry(frame_coords)
entry_y1.pack(side=tk.LEFT, padx=5, pady=5)

label_distance_separator = tk.Label(frame_coords, text=" vs. ")
label_distance_separator.pack(side=tk.LEFT, padx=5, pady=5)

entry_x2 = ttk.Entry(frame_coords)
entry_x2.pack(side=tk.LEFT, padx=5, pady=5)
entry_y2 = ttk.Entry(frame_coords)
entry_y2.pack(side=tk.LEFT, padx=5, pady=5)

# 绑定事件,当输入框内容改变时计算距离
entry_x1.bind('<KeyRelease>', on_entry_change)
entry_y1.bind('<KeyRelease>', on_entry_change)
entry_x2.bind('<KeyRelease>', on_entry_change)
entry_y2.bind('<KeyRelease>', on_entry_change)

# 标签用于显示最终距离
label_distance = ttk.Label(root, text="The distance will be displayed here")
label_distance.pack(padx=20, pady=20)

root.mainloop()

结果:

输入数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值