将上面程序的上位机改得更美观,其他不变
时间: 2024-02-22 17:00:27 浏览: 27
可以使用 `ttk` 模块中提供的更美观的控件替换原有的控件,如 `ttk.Button` 替换 `tk.Button`,`ttk.Label` 替换 `tk.Label`。同时可以使用样式(style)来自定义控件的外观。下面是修改后的代码:
```
from PIL import Image
import tkinter as tk
from tkinter import ttk
def show_results(results):
# 创建子界面
win = tk.Toplevel()
win.geometry("400x400")
win.title("子界面")
# 创建表格
table = ttk.Treeview(win, columns=("X", "Y", "W", "H", "类别"), show='headings')
table.column("X", width=80)
table.column("Y", width=80)
table.column("W", width=80)
table.column("H", width=80)
table.column("类别", width=80)
table.heading("X", text="X")
table.heading("Y", text="Y")
table.heading("W", text="W")
table.heading("H", text="H")
table.heading("类别", text="类别")
table.pack()
# 创建表格内容
for i, s1 in enumerate(results):
table.insert("", "end", values=s1[:4]+[s1[4]])
# 创建选择按钮
select_button = ttk.Button(table, text="选择", command=lambda s=s1: select_result(s))
table.window_create(table.identify_region(i, 5), window=select_button)
# 定义选择结果函数
def select_result(result):
print("选择的是:", result)
def site(source, pred, names):
img = Image.open(source)
x1, x2 = img.size
print([x1, x2])
results = []
for i1 in pred:
s = []
for i2 in i1.data.cpu().numpy():
s1 = []
s = list(i2)
# 获取预测框中心点的坐标
x = s[0] = float(round((s[0] + s[2]) / 2 / x1, 4))
y = s[1] = float(round((s[1] + s[3]) / 2 / x2, 4))
# 预测框的宽和高
w = s[2] - s[0]
h = s[3] - s[1]
s1.append(str(x))
s1.append(str(y))
s1.append(str(w))
s1.append(str(h))
s1.append(names[int(s[5])])
if s[4] < 0.5:
break
results.append(s1)
# 创建GUI界面
window = tk.Tk()
window.geometry("400x400")
# 创建按钮
for name in set([r[4] for r in results]):
ttk.Label(window, text=name).pack()
button = ttk.Button(window, text="显示" + name + " 的结果", command=lambda name=name: show_results([r[:4]+[name] for r in results if r[4] == name]))
button.pack()
# 创建确定按钮
confirm_button = ttk.Button(window, text="退出", command=window.quit)
confirm_button.pack()
window.mainloop()
```
在这个版本中,我们使用了 `Treeview` 控件来显示检测结果,并用 `window_create` 方法将按钮插入到表格中。同时,我们也使用了 `ttk.Label` 和 `ttk.Button` 替换了原有的控件,并通过样式对其进行了美化。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)