首页/文章/ 详情

Tcl Call Python

3月前浏览1921



tcl是Tool Command Language的首字母缩写,发展至今,目前使用较多的,可能是以下几个领域,altair的HyperWork产品、IC行业,Python, perl ,haskell 等语言使用作为GUI库。用户少那么弊端就很明显,对应的交流就少,缺少维护缺少开源社区,反之对应的语言是什么呢,心中都有个答案,Python,是吧,又不是自己造轮子,git clone 一下方便快捷。


Python,既然发展得如此迅猛,tcl如何搭上这艘快船呢,他提供了一个关键的函数,exec,使用起来也是很简单。


最重要的是,将python.exe的目录置为当前。就是你调用的包,包含在当前解释器。内网的小伙伴,anaconda安排上,解决大部分模块安装问题


直接上代码,exec调用Python文件


# Python







def add(a,b):
   return a+bsum = add(10,20)
print(sum)

#tcl














set ALTAIR_HOME [hm_info -appinfo ALTAIR_HOME]
set python_s_dir "/hwsolvers/common/python/python3.4/win64"
set  python_dir [format $ALTAIR_HOME%s $python_s_dir]
cd $python_dir
set scriptsDir [file dir [info script]]
set sum [exec python [file join $scriptsDir add].py]
puts $sum

Python传递参数 基于sys模块

# Python文件内容






import sysdef add(a,b):  return int(a)+int(b)sum = add(sys.argv[1],sys.argv[2])print(sum)

#tcl文件内容







set ALTAIR_HOME [hm_info -appinfo ALTAIR_HOME]set python_s_dir "/hwsolvers/common/python/python3.4/win64"set  python_dir [format $ALTAIR_HOME%s $python_s_dir]cd $python_dirset sum [exec python add1.py 50 10]puts $sum


通过捕获标准输出获取数据 exec直接运行Python脚本 









set python_script { import numpy as nprandom_numbers = np.random.randn(100)print(random_numbers)}cd "D:/Users/jintian/anaconda3"set result [exec python << $python_script]puts "$result"


GUI界面运行























































































































































































































set python_script {
from PyQt6.QtCore import QDateTime, Qt, QTimerfrom PyQt6.QtWidgets import (QApplication, QCheckBox, QComboBox, QDateTimeEdit,        QDial, QDialog, QGridLayout, QGroupBox, QHBoxLayout, QLabel, QLineEdit,        QProgressBar, QPushButton, QRadioButton, QScrollBar, QSizePolicy,        QSlider, QSpinBox, QStyleFactory, QTableWidget, QTabWidget, QTextEdit,        QVBoxLayout, QWidget)

class WidgetGallery(QDialog):    def __init__(self, parent=None):        super(WidgetGallery, self).__init__(parent)
       self.originalPalette = QApplication.palette()
       styleComboBox = QComboBox()        styleComboBox.addItems(QStyleFactory.keys())
       styleLabel = QLabel("&Style:")        styleLabel.setBuddy(styleComboBox)
       self.useStylePaletteCheckBox = QCheckBox("&Use style's standard palette")        self.useStylePaletteCheckBox.setChecked(True)
       disableWidgetsCheckBox = QCheckBox("&Disable widgets")
       self.createTopLeftGroupBox()        QApplication.processEvents()        self.createTopRightGroupBox()        QApplication.processEvents()        self.createBottomLeftTabWidget()        QApplication.processEvents()        self.createBottomRightGroupBox()        QApplication.processEvents()        self.createProgressBar()        QApplication.processEvents()
       styleComboBox.textActivated.connect(self.changeStyle)        self.useStylePaletteCheckBox.toggled.connect(self.changePalette)        disableWidgetsCheckBox.toggled.connect(self.topLeftGroupBox.setDisabled)        disableWidgetsCheckBox.toggled.connect(self.topRightGroupBox.setDisabled)        disableWidgetsCheckBox.toggled.connect(self.bottomLeftTabWidget.setDisabled)        disableWidgetsCheckBox.toggled.connect(self.bottomRightGroupBox.setDisabled)
       topLayout = QHBoxLayout()        topLayout.addWidget(styleLabel)        topLayout.addWidget(styleComboBox)        topLayout.addStretch(1)        topLayout.addWidget(self.useStylePaletteCheckBox)        topLayout.addWidget(disableWidgetsCheckBox)
       mainLayout = QGridLayout()        mainLayout.addLayout(topLayout, 0, 0, 1, 2)        mainLayout.addWidget(self.topLeftGroupBox, 1, 0)        mainLayout.addWidget(self.topRightGroupBox, 1, 1)        mainLayout.addWidget(self.bottomLeftTabWidget, 2, 0)        mainLayout.addWidget(self.bottomRightGroupBox, 2, 1)        mainLayout.addWidget(self.progressBar, 3, 0, 1, 2)        mainLayout.setRowStretch(1, 1)        mainLayout.setRowStretch(2, 1)        mainLayout.setColumnStretch(0, 1)        mainLayout.setColumnStretch(1, 1)        self.setLayout(mainLayout)
       self.setWindowTitle("Styles")        self.changeStyle('Windows')
   def changeStyle(self, styleName):        QApplication.setStyle(QStyleFactory.create(styleName))        self.changePalette()
   def changePalette(self):        if (self.useStylePaletteCheckBox.isChecked()):            QApplication.setPalette(QApplication.style().standardPalette())        else:            QApplication.setPalette(self.originalPalette)
   def advanceProgressBar(self):        curVal = self.progressBar.value()        maxVal = self.progressBar.maximum()        self.progressBar.setValue(curVal + (maxVal - curVal) // 100)
   def createTopLeftGroupBox(self):        self.topLeftGroupBox = QGroupBox("Group 1")
       radioButton1 = QRadioButton("Radio button 1")        radioButton2 = QRadioButton("Radio button 2")        radioButton3 = QRadioButton("Radio button 3")        radioButton1.setChecked(True)
       checkBox = QCheckBox("Tri-state check box")        checkBox.setTristate(True)        checkBox.setCheckState(Qt.CheckState.PartiallyChecked)
       layout = QVBoxLayout()        layout.addWidget(radioButton1)        layout.addWidget(radioButton2)        layout.addWidget(radioButton3)        layout.addWidget(checkBox)        layout.addStretch(1)        self.topLeftGroupBox.setLayout(layout)
   def createTopRightGroupBox(self):        self.topRightGroupBox = QGroupBox("Group 2")
       defaultPushButton = QPushButton("Default Push Button")        defaultPushButton.setDefault(True)
       togglePushButton = QPushButton("Toggle Push Button")        togglePushButton.setCheckable(True)        togglePushButton.setChecked(True)
       flatPushButton = QPushButton("Flat Push Button")        flatPushButton.setFlat(True)
       layout = QVBoxLayout()        layout.addWidget(defaultPushButton)        layout.addWidget(togglePushButton)        layout.addWidget(flatPushButton)        layout.addStretch(1)        self.topRightGroupBox.setLayout(layout)
   def createBottomLeftTabWidget(self):        self.bottomLeftTabWidget = QTabWidget()        self.bottomLeftTabWidget.setSizePolicy(QSizePolicy.Policy.Preferred,                QSizePolicy.Policy.Ignored)
       tab1 = QWidget()        tableWidget = QTableWidget(10, 10)
       tab1hbox = QHBoxLayout()        tab1hbox.setContentsMargins(5, 5, 5, 5)        tab1hbox.addWidget(tableWidget)        tab1.setLayout(tab1hbox)
       tab2 = QWidget()        textEdit = QTextEdit()
       textEdit.setPlainText("Twinkle, twinkle, little star,\n"                              "How I wonder what you are.\n"                              "Up above the world so high,\n"                              "Like a diamond in the sky.\n"                              "Twinkle, twinkle, little star,\n"                              "How I wonder what you are!\n")
       tab2hbox = QHBoxLayout()        tab2hbox.setContentsMargins(5, 5, 5, 5)        tab2hbox.addWidget(textEdit)        tab2.setLayout(tab2hbox)
       self.bottomLeftTabWidget.addTab(tab1, "&Table")        self.bottomLeftTabWidget.addTab(tab2, "Text &Edit")
   def createBottomRightGroupBox(self):        self.bottomRightGroupBox = QGroupBox("Group 3")        self.bottomRightGroupBox.setCheckable(True)        self.bottomRightGroupBox.setChecked(True)
       lineEdit = QLineEdit('s3cRe7')        lineEdit.setEchoMode(QLineEdit.EchoMode.Password)
       spinBox = QSpinBox(self.bottomRightGroupBox)        spinBox.setValue(50)
       dateTimeEdit = QDateTimeEdit(self.bottomRightGroupBox)        dateTimeEdit.setDateTime(QDateTime.currentDateTime())
       slider = QSlider(Qt.Orientation.Horizontal, self.bottomRightGroupBox)        slider.setValue(40)
       scrollBar = QScrollBar(Qt.Orientation.Horizontal, self.bottomRightGroupBox)        scrollBar.setValue(60)
       dial = QDial(self.bottomRightGroupBox)        dial.setValue(30)        dial.setNotchesVisible(True)
       layout = QGridLayout()        layout.addWidget(lineEdit, 0, 0, 1, 2)        layout.addWidget(spinBox, 1, 0, 1, 2)        layout.addWidget(dateTimeEdit, 2, 0, 1, 2)        layout.addWidget(slider, 3, 0)        layout.addWidget(scrollBar, 4, 0)        layout.addWidget(dial, 3, 1, 2, 1)        layout.setRowStretch(5, 1)        self.bottomRightGroupBox.setLayout(layout)
   def createProgressBar(self):        self.progressBar = QProgressBar()        self.progressBar.setRange(0, 10000)        self.progressBar.setValue(0)
       timer = QTimer(self)        timer.timeout.connect(self.advanceProgressBar)        timer.start(1000)

if __name__ == '__main__':
   import sys
   app = QApplication(sys.argv)    gallery = WidgetGallery()    gallery.show()    sys.exit(app.exec())
}cd "D:/Users/jintian/anaconda3"set result [exec python << $python_script &]



拓展训练,如何使用tcl调取Python,对网格节点进行聚类分析,留给大家了,Python能够基于HyperWork平台干些有趣的事情,也交由大家探索了。

看到这里了点点关注吧



来源:TodayCAEer
ACTpythonUMOriginAltair
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2024-08-14
最近编辑:3月前
TodayCAEer
本科 签名征集中
获赞 18粉丝 28文章 249课程 0
点赞
收藏
作者推荐

关于矢量的基础函数

proccalculate{points}{setnum_points[llength$points]if{$num_points&lt;2||$num_points&gt;4}{return-codeerror&quot;Invalidnumberofpoints.Mustbe2,3,or4.&quot;}setlast_point[lindex$pointsend]switch$num_points{2{setpoint1[lindex$points0]setpoint2[lindex$points1]setvector[vecsub$point2$point1]setnormalized_vector[vecnormalize$vector]return[list$normalized_vector$point2]}3-4{setpoint1[lindex$points0]setpoint2[lindex$points1]setpoint3[lindex$points2]setvector1[vecsub$point2$point1]setvector2[vecsub$point3$point1]setnormal[veccross$vector1$vector2]setnormalized_normal[vecnormalize$normal]return[list$normalized_normal$last_point]}}}procvecsub{v1v2}{setresult[list]foreachi$v1j$v2{lappendresult[expr{$j-$i}]}return$result}procvecnormalize{v}{setlength0foreachi$v{setlength[expr{$length+$i*$i}]}setlength[expr{sqrt($length)}]setresult[list]foreachi$v{lappendresult[expr{$i/$length}]}return$result}procveccross{v1v2}{setx[expr{[lindex$v11]*[lindex$v22]-[lindex$v12]*[lindex$v21]}]sety[expr{[lindex$v12]*[lindex$v20]-[lindex$v10]*[lindex$v22]}]setz[expr{[lindex$v10]*[lindex$v21]-[lindex$v11]*[lindex$v20]}]return[list$x$y$z]}calculate{{-65.0463-298.55-61.6646}{38.73903-207.005-48.1097}}calculate{{-65.0463-298.55-61.6646}{38.73903-207.005-48.1097}{-51.713-230.822-49.622}}calculate{{-65.0463-298.55-61.6646}{38.73903-207.005-48.1097}{-51.713-230.822-49.622}{-44.304-262.711-49.416}}来源:TodayCAEer

未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈