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+b
sum = 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 sys
def 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_dir
set sum [exec python add1.py 50 10]
puts $sum
通过捕获标准输出获取数据 exec直接运行Python脚本
set python_script {
import numpy as np
random_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, QTimer
from 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平台干哪些有趣的事情,也交由大家探索了。
看到这里了点点关注吧