代码:
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()
结果:
输入数据