Python如何在Abaqus中实现文件选择和保存

目录

  1. 文件选择(askopenfilename)

引言

  在Abaqus中python的版本使用的是python 2.x系列,要脱离Abaqus GUI出现选择界面,推荐使用 tkFileDialog 模块的函数实现。

文件选择(askopenfilename

  在实现弹窗选择文件前,需要运行Tkinter窗口,python代码如下:

# Dialog from abaqus python 2.x
from odbAccess import *
#python 2.x need to import Tkinter
import Tkinter as tk
import tkFileDialog
import os
root = tk.Tk()
# Hide the windows
root.withdraw()

  文件选择的方式有两种,一种是单文件选择,另一种是多文件选择。

  单文件选择,python代码如下:

'''
‌参数说明:‌ 这些函数支持多种参数,用于自定义对话框行为,例如:
filetypes=[(label1, pattern1), (label2, pattern2), ...]:设置文件类型过滤器,如 filetypes=[("文本文件", "*.txt"), ("Python文件", "*.py")]。
initialdir="path":指定对话框默认显示的初始目录。
title="Dialog Title":设置对话框标题。
defaultextension=".ext":指定默认文件扩展名。
initialfile="filename":预填充文件名输入框。
parent=root_window:指定父窗口,对话框将依附于该窗口
'''
def searchSingleFilePath():
    fileName = tkFileDialog.askopenfilename(title="Select a file",filetypes=[("Txt file","*.txt"),("All of files","*.*")])
    if fileName:
        print("Select a file",fileName)
    else:
        print("No file is selected")

  多文件选择,python代码如下:

def searchMultipleFilePath():
    # Return string for the askopenfilenames method
    fileNames = tkFileDialog.askopenfilenames(initialdir="./",title="Select inp file",filetypes=[("INP file","*.inp")])
    # Convert data type from unicode to string
    fileNames = fileNames.encode("utf-8")
    # Get path list
    fileNewPaths = multiplePathConvert(fileNames)
    # fileNameTuples = tk.splitlist(fileNames)
    print(fileNewPaths)
    for fileName in fileNewPaths:
        print(fileName)

  多文件选择时,需要注意安装的Abaqus中python的版本是在2.6以上时,多文件选择后返回的路径数据类型为(unicode字符),需要处理一下。由于路径中可能存在空格,文件选择后返回的数据存在不同,具体信息如下:  

'''
    Nonspace path data:
        E:/learning_materials/python_pros/WxPython/Testpath02/Testinp01.inp E:/learning_materials/python_pros/WxPython/Testpath02/Testinp02.inp E:/learning_materials/python_pros/WxPython/Testpath02/Testinp03.inp E:/learning_materials/python_pros/WxPython/Testpath02/Testinp04.inp
    Space path data:
        {E:/learning_materials/python_pros/WxPython/Test path/Testinp01.inp} {E:/learning_materials/python_pros/WxPython/Test path/Testinp02.inp} {E:/learning_materials/python_pros/WxPython/Test path/Testinp03.inp} {E:/learning_materials/python_pros/WxPython/Test path/Testinp04.inp}
'''
def multiplePathConvert(fileNames):
    fileNewPaths = ""
    # Select the path with spaces
    if fileNames.startswith("{"):
       fileNames = fileNames.lstrip("{").rstrip("}")
       fileNewPaths = fileNames.split("} {")
    # Select the path without spaces
    else:
        fileNewPaths = fileNames.split()
    return fileNewPaths

  完整代码

Select files(askopenfilename)

 #!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@File    :   tkinter2ptx.py
@Time    :  
@Author  :   
@Version :   1.0
@Desc    :   Dialog from abaqus python 2.x
'''
from odbAccess import *
import Tkinter as tk
import tkFileDialog
root = tk.Tk()
import os

# Hide the windows
root.withdraw()
'''
‌参数说明:‌ 这些函数支持多种参数,用于自定义对话框行为,例如:
filetypes=[(label1, pattern1), (label2, pattern2), ...]:设置文件类型过滤器,如 filetypes=[("文本文件", "*.txt"), ("Python文件", "*.py")]。
initialdir="path":指定对话框默认显示的初始目录。
title="Dialog Title":设置对话框标题。
defaultextension=".ext":指定默认文件扩展名。
initialfile="filename":预填充文件名输入框。
parent=root_window:指定父窗口,对话框将依附于该窗口
'''
def multiplePathConvert(fileNames):
    fileNewPaths = ""
    # Select the path with spaces
    if fileNames.startswith("{"):
       fileNames = fileNames.lstrip("{").rstrip("}")
       fileNewPaths = fileNames.split("} {")
    # Select the path without spaces
    else:
        fileNewPaths = fileNames.split()
    return fileNewPaths
def searchSingleFilePath():
    fileName = tkFileDialog.askopenfilename(title="Select a file",filetypes=[("Txt file","*.txt"),("All of files","*.*")])
    if fileName:
        print("Select a file",fileName)
    else:
        print("No file is selected")
def searchMultipleFilePath():
    # Return string for the askopenfilenames method
    fileNames = tkFileDialog.askopenfilenames(initialdir="./",title="Select inp file",filetypes=[("INP file","*.inp")])
    # Convert data type from unicode to string
    fileNames = fileNames.encode("utf-8")
    # Get path list
    print(fileNames)
    fileNewPaths = multiplePathConvert(fileNames)
    # fileNameTuples = tk.splitlist(fileNames)
    for fileName in fileNewPaths:
        print(fileName)

if __name__=="__main__":
    searchMultipleFilePath()

参考

 

posted @ 2026-01-12 22:27  SwingGodLin  阅读(0)  评论(0)    收藏  举报