打工仔小小阳的个人空间 https://blog.eetop.cn/1791140 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

GUI Python

已有 392 次阅读| 2024-3-2 14:58 |系统分类:其他

import tkinter as tk  
from tkinter import filedialog, messagebox  
import os  
 
# 配置文件名  
CONFIG_FILE = 'configdemo'  
 
# 读取和修改配置文件  
def read_and_modify_config(tt_checked, ss_checked, ff_checked, all_checked, sim_checked, project_entry, cellnamex_entry):  
    with open(CONFIG_FILE, 'r') as file:  
        lines = file.readlines()  
 
    # 寻找TT_CORNER和SS_CORNER并修改  
    modified = False  
    for i, line in enumerate(lines):  
        if 'TT_CORNER' in line:  
            if tt_checked or all_checked:  
                lines[i] = line.replace('[0]', '[1]')  
            else:  
                lines[i] = line.replace('[1]', '[0]')  
                modified = True  
        elif 'SS_CORNER' in line:  
            if ss_checked or all_checked:  
                lines[i] = line.replace('[0]', '[1]')  
            else:  
                lines[i] = line.replace('[1]', '[0]')  
                modified = True
        elif 'FF_CORNER' in line:  
            if ff_checked or all_checked:  
                lines[i] = line.replace('[0]', '[1]')  
            else:  
                lines[i] = line.replace('[1]', '[0]')  
                modified = True  
        elif 'PRE_OR_POST ' in line:  
            if sim_checked :
                lines[i] = line.replace('[0]', '[1]')  
            else:  
                lines[i] = line.replace('[1]', '[0]')  
                modified = True  
       
 
    # 添加CDL_PATH  
    cdl_path = cdl_entry.get()  
    if cdl_path and 'CDL_PATH' not in lines:  
        # Ensure CDL_PATH is added as a new line  
        lines.append('\n')  # Add a new line before CDL_PATH  
        lines.append(f'CDL_PATH=[{cdl_path}]\n')  
        modified = True  
    cdl_path = cdl_entry.get()  
    # 添加model_PATH  
    model_path = model_entry.get()  
    if model_path and 'MODEL_PATH' not in lines:  
        # Ensure MODEL_PATH is added as a new line  
        lines.append('\n')  # Add a new line before MODEL_PATH  
        lines.append(f'MODEL_PATH=[{model_path}]\n')  
        modified = True  
    model_path = model_entry.get()

    # 添加或修改project  
    project_value = project_entry.get()  
    if project_value and 'project' not in lines:  
        lines.append(f'project={project_value}\n')  
        modified = True
   
    # 添加或修改cell_namex
    cellnamex_value = cellnamex_entry.get()  
    if cellnamex_value and 'cellnamex' not in lines:  
        lines.append(f'cellnamex={cellnamex_value}\n')  
        modified = True

    # 如果文件被修改,则另存为新文件  
    if modified:  
        new_file_name = f"config"  # 新文件名,可以是任何您选择的名称  
        with open(new_file_name, 'w') as new_file:  # 使用新文件名打开文件以写入  
            new_file.writelines(lines)  
        messagebox.showinfo('Success', f'Modified config saved to {new_file_name}')
       
# 文件选择按钮回调函数  
def select_file():  
    filepath = filedialog.askopenfilename()  
    if filepath:  
        cdl_entry.delete(0, tk.END)  
        cdl_entry.insert(0, filepath)
       
def select_model_file():  
    file_path = filedialog.askopenfilename()  
    if file_path:  
       model_entry.delete(0, tk.END)  
       model_entry.insert(0, file_path)  

 
 
# 保存按钮回调函数  
# def save_changes():  
#    read_and_modify_config(tt_check.get(), ss_check.get(), all_check.get(), project_entry, cellnamex_entry)
def save_changes():  
    # 假设这些是从GUI中获取的checkbox和输入框的值  
    tt_checked = tt_check.get()  
    ss_checked = ss_check.get()  
    ff_checked = ff_check.get()  
    all_checked = all_check.get()  
    sim_checked = sim_check.get()  
     
    read_and_modify_config(tt_checked, ss_checked, ff_checked, all_checked, sim_checked, project_entry, cellnamex_entry)  
 

# 创建GUI窗口  
root = tk.Tk()  
root.title('Config Modifier')  
root.geometry("300x200")  # 设置窗口大小  
root.configure(background='lightgray')  # 设置窗口背景色  
 
# 添加 "Corner" Label  
corner_label = tk.Label(root, text="Corner:", font=('Arial', 12))  
corner_label.pack(pady=(10, 0), anchor=tk.W)  # 使用anchor=tk.W来确保它靠左对齐  
 
# 添加包含勾选框的Frame  
frame = tk.Frame(root)  
frame.pack(pady=(0, 10), anchor=tk.N)  # 使用anchor=tk.N来确保它靠北对齐,并设置下方的间距  
 
tt_check = tk.BooleanVar(value=False)  
tt_check_entry = tk.Checkbutton(frame, text='TT', variable=tt_check)  
tt_check_entry.pack(side=tk.LEFT, padx=5)  
 
ss_check = tk.BooleanVar(value=False)  
ss_check_entry = tk.Checkbutton(frame, text='SS', variable=ss_check)  
ss_check_entry.pack(side=tk.LEFT, padx=5)  
 
ff_check = tk.BooleanVar(value=False)  
ff_check_entry = tk.Checkbutton(frame, text='FF', variable=ff_check)  
ff_check_entry.pack(side=tk.LEFT, padx=5)  
 
all_check = tk.BooleanVar(value=False)  
all_check_entry = tk.Checkbutton(frame, text='ALL', variable=all_check)  
all_check_entry.pack(side=tk.LEFT, padx=5)  
 
# 添加 "sim" Label 和 "POST" 勾选框到同一个Frame中  
sim_frame = tk.Frame(root)  
sim_frame.pack(pady=(0, 10), anchor=tk.N)  # 使用anchor=tk.N来确保它靠北对齐,并设置下方的间距  
 
sim_label = tk.Label(sim_frame, text="POST :", font=('Arial', 12))  
sim_label.pack(side=tk.LEFT)  
 
sim_check = tk.BooleanVar(value=False)  
sim_check_entry = tk.Checkbutton(sim_frame, text='POST', variable=sim_check)  
sim_check_entry.pack(side=tk.LEFT, padx=5)  
# 由于sim_frame和frame都在root中,并且都使用anchor=tk.N,sim_frame会出现在frame上方  

# 文件选择按钮和输入框  
cdl_path_button = tk.Button(root, text='Select CDL Path', command=select_file)  
cdl_path_button.pack(pady=5)  
 
cdl_entry = tk.Entry(root)  
cdl_entry.pack(pady=5)  

model_path_button = tk.Button(root, text='Select MODEL Path', command=select_model_file)  
model_path_button.pack(pady=5)  
 
model_entry = tk.Entry(root)  
model_entry.pack(pady=5)  


# 建立project对应按钮及输入框
project_entry = tk.StringVar()
project_label = tk.Label(root, text="Project:", font=('Arial', 12))  
project_label.pack()  

project_entry_field = tk.Entry(root, textvariable=project_entry)  
project_entry_field.pack()  

# 添加提示信息的标签  
project_hint_label = tk.Label(root, text="Rup, Rdown, tran...")  
project_hint_label.pack()  

# 建立cellname对应按钮及输入框
cellnamex_entry = tk.StringVar()
cellnamex_label = tk.Label(root, text="Cellname:", font=('Arial', 12))  
cellnamex_label.pack()  

cellnamex_entry_field = tk.Entry(root, textvariable=cellnamex_entry)  
cellnamex_entry_field.pack()  

# 添加提示信息的标签  
cellnamex_hint_label = tk.Label(root, text="retion X or Y")  
cellnamex_hint_label.pack()
 
# 保存按钮  
save_button = tk.Button(root, text='Save', command=save_changes)  
save_button.pack(pady=5)  
 
# 运行GUI  
root.mainloop()



点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 0

    粉丝
  • 0

    好友
  • 0

    获赞
  • 0

    评论
  • 13

    访问数
关闭

站长推荐 上一条 /2 下一条


小黑屋| 手机版| 关于我们| 联系我们| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-12-26 15:41 , Processed in 0.026598 second(s), 14 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部