94 lines
1.6 KiB
Python
94 lines
1.6 KiB
Python
"""设备选择组件
|
||
|
||
包含设备选择、刷新设备等功能的GUI组件。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any, Callable
|
||
import tkinter as tk
|
||
from tkinter import ttk
|
||
|
||
from gui.gui_core import find_adb_executable, list_adb_devices
|
||
|
||
|
||
def create_device_selector(
|
||
parent: Any,
|
||
on_line: Callable[[str], None],
|
||
device_var: tk.StringVar,
|
||
) -> tuple[ttk.Combobox, Callable[[], None]]:
|
||
"""创建设备选择组件。
|
||
|
||
参数:
|
||
parent: 父容器
|
||
on_line: 日志输出回调
|
||
device_var: 设备ID变量
|
||
|
||
返回:
|
||
(device_combo, refresh_devices_func) 设备下拉框和刷新函数
|
||
"""
|
||
adb_exe = find_adb_executable()
|
||
devices = list_adb_devices(adb_exe)
|
||
device_var.set(devices[0] if devices else "")
|
||
|
||
ttk.Label(parent, text="设备:").pack(side="left")
|
||
device_combo = ttk.Combobox(parent, textvariable=device_var, values=devices, width=28)
|
||
device_combo.pack(side="left", padx=(0, 10))
|
||
|
||
def refresh_devices():
|
||
nonlocal adb_exe
|
||
adb_exe = find_adb_executable()
|
||
devs = list_adb_devices(adb_exe)
|
||
device_combo["values"] = devs
|
||
if devs and not device_var.get():
|
||
device_var.set(devs[0])
|
||
if not adb_exe:
|
||
on_line("[设备] 未找到 adb.exe(请确认已安装 platform-tools)")
|
||
elif not devs:
|
||
on_line("[设备] adb 可用,但未发现 device 状态设备(可能未授权/未连接)")
|
||
|
||
ttk.Button(parent, text="刷新设备", command=refresh_devices).pack(
|
||
side="left", padx=(0, 20)
|
||
)
|
||
|
||
return device_combo, refresh_devices
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|