You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Shell中从网格选择并返回单个单元格内容的方法

在Debian 13中实现支持单个单元格选择的网格列表

zenity和yad的列表模式确实只能返回整行内容,没法单独选中单个单元格,这里给你两个轻量的实现方案,都能在Debian 13上直接使用:

方案1:Python + Tkinter(系统预装,无需额外安装依赖)

用Treeview组件实现网格布局,点击任意单元格即可返回对应内容:

#!/usr/bin/env python3
import tkinter as tk
from tkinter import ttk

def on_cell_click(event):
    # 获取点击的单元格信息
    item = tree.identify('item', event.x, event.y)
    col_idx = int(tree.identify('column', event.x, event.y)[1:]) - 1
    if item and col_idx >= 0:
        cell_value = tree.item(item, 'values')[col_idx]
        if cell_value:  # 跳过空单元格
            print(cell_value)
            root.quit()

# 读取目标文件内容(替换成你的文件路径)
with open('your_items.txt', 'r') as f:
    all_items = [line.strip() for line in f if line.strip()]

# 整理成两列结构
COLUMNS = 2
grid_rows = []
for i in range(0, len(all_items), COLUMNS):
    row = all_items[i:i+COLUMNS]
    while len(row) < COLUMNS:
        row.append('')
    grid_rows.append(row)

# 构建GUI窗口
root = tk.Tk()
root.title("Single Cell Selector")

# 创建Treeview网格
tree = ttk.Treeview(root, columns=[f'col{i+1}' for i in range(COLUMNS)], show='headings')
for i in range(COLUMNS):
    tree.heading(f'col{i+1}', text=f'Col {i+1}')
    tree.column(f'col{i+1}', width=120, anchor='center')

# 填充数据
for row in grid_rows:
    tree.insert('', tk.END, values=row)

# 绑定点击事件
tree.bind('<ButtonRelease-1>', on_cell_click)

tree.pack(padx=15, pady=15)
root.mainloop()

使用步骤:

  1. 把代码保存为cell_selector.py
  2. 赋予执行权限:chmod +x cell_selector.py
  3. 将A、B、C、D、E、F、G按每行一个的格式写入your_items.txt
  4. 运行./cell_selector.py,点击任意单元格,对应内容会输出到Shell,窗口自动关闭

方案2:Python + GTK3(需安装依赖)

用按钮模拟单元格,逻辑更简单,点击按钮直接返回对应内容:

先安装依赖:

sudo apt install python3-gi

代码:

#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class CellSelectorWindow(Gtk.Window):
    def __init__(self, items):
        Gtk.Window.__init__(self, title="Cell Picker")
        self.set_border_width(10)
        self.grid = Gtk.Grid(spacing=5)
        self.add(self.grid)

        # 排列成两列
        col, row = 0, 0
        for item in items:
            btn = Gtk.Button(label=item, width_request=100)
            btn.connect("clicked", self.on_btn_click, item)
            self.grid.attach(btn, col, row, 1, 1)
            col += 1
            if col >= 2:
                col = 0
                row += 1

    def on_btn_click(self, widget, item):
        print(item)
        Gtk.main_quit()

# 读取文件内容
with open('your_items.txt', 'r') as f:
    all_items = [line.strip() for line in f if line.strip()]

# 启动窗口
win = CellSelectorWindow(all_items)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

使用步骤和方案1一致,点击按钮后对应内容会输出到Shell。

内容的提问来源于stack exchange,提问作者HippoMan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 00:13:28