GUI选择fileA时自动提取netlist路径与cell名称的实现方法
实现方案
以下为基于Python Tkinter GUI框架的可运行实现,你也可以根据自己使用的GUI框架调整对应逻辑:
前置假设
- 目录A的路径我们提前定义为变量
dir_a = "./A",你可以修改为本地实际路径 - 目录A下提前存在fileA、netlist.tcl两个文件,fileA的内容和你给出的一致
实现步骤
- 初始化GUI组件,包含Combobox、netlist输入框、cell输入框三个核心控件
- 读取目录A下的所有文件名,加载到Combobox的选项列表中
- 给Combobox绑定
<<ComboboxSelected>>选中事件,触发解析填充逻辑 - 解析逻辑实现:
- 读取选中文件的完整路径,逐行读取内容
- 匹配以
Source开头的行,提取引号内的netlist文件名,和目录A路径拼接得到完整路径,填入netlist输入框 - 匹配以
cell开头的行,提取引号内的cell名称,填入cell输入框
可运行代码示例
import os import re import tkinter as tk from tkinter import ttk # 配置目录A的实际路径 DIR_A = "./A" def on_file_selected(event): # 获取选中的文件名 selected_file = combobox.get() file_path = os.path.join(DIR_A, selected_file) if not os.path.isfile(file_path): return netlist_val = "" cell_val = "" # 逐行解析文件内容 with open(file_path, "r", encoding="utf-8") as f: for line in f: line = line.strip() # 匹配Source行 if line.startswith("Source"): match = re.search(r'"(.*?)"', line) if match: netlist_filename = match.group(1) netlist_val = os.path.join(DIR_A, netlist_filename) # 匹配cell行 elif line.startswith("cell"): match = re.search(r'"(.*?)"', line) if match: cell_val = match.group(1) # 填充到对应输入框 netlist_entry.delete(0, tk.END) netlist_entry.insert(0, netlist_val) cell_entry.delete(0, tk.END) cell_entry.insert(0, cell_val) # 初始化GUI root = tk.Tk() root.title("文件解析填充") # 构造Combobox选项 file_options = [f for f in os.listdir(DIR_A) if os.path.isfile(os.path.join(DIR_A, f))] combobox = ttk.Combobox(root, values=file_options) combobox.pack(padx=10, pady=10) combobox.bind("<<ComboboxSelected>>", on_file_selected) # netlist字段 ttk.Label(root, text="netlist路径:").pack(padx=10, anchor="w") netlist_entry = ttk.Entry(root, width=50) netlist_entry.pack(padx=10, pady=5) # cell字段 ttk.Label(root, text="cell名称:").pack(padx=10, anchor="w") cell_entry = ttk.Entry(root, width=50) cell_entry.pack(padx=10, pady=5) root.mainloop()
验证效果
选中Combobox里的fileA选项后,netlist输入框会自动填充A/netlist.tcl,cell输入框会自动填充chklist,和预期输出完全一致。
内容的提问来源于stack exchange,提问作者Surabh S
相关产品推荐
相关产品推荐

