Posted to tcl by hardkorebob at Wed Aug 05 20:09:24 GMT 2026view raw

  1. source ytcl.tcl
  2.  
  3. set BG {bg="#bbb"}
  4. set TEXT_PACK {expand=1,side="left",fill="both",padx=0,pady=0}
  5. set CLI_PACK {expand=0,fill="x",padx=2,pady=2}
  6. set TEXT_PROP {self,wrap="none",undo=True,highlightbackground="#bbb",bg="#bbb",fg="#000",font=("VictorMono",14),relief="flat",padx=5,pady=5,blockcursor=True,insertbackground="red",highlightcolor="#bbb",cursor="cross",spacing2=5,spacing3=5,selectbackground="#444",selectforeground="white"}
  7. set CLI_PROP {self,textvariable=self.theCmd,highlightbackground="#bbb",bg="#aaa",fg="#000",font=("VictorMono",16),relief="flat",highlightcolor="#bbb",cursor="heart",selectbackground="#444",selectforeground="white"}
  8. set STATS_PROP {self,textvariable=self.insPos,relief="flat",bg="#bbb",fg="#000",font=("VictorMono",14)}
  9. set STATS_PACK {expand=0,fill="none",padx=5,pady=2,anchor="nw"}
  10. set PYTXT_PROP {self,height="2",textvariable=self.pyTxt,relief="flat",bg="#bbb",fg="#000",font=("VictorMono",14)}
  11. set PYTXT_PACK {expand=0,fill="none",padx=5,pady=2,anchor="nw"}
  12. set PBP {self,mode="determinate",orient="horizontal",length="1000",style="TProgressbar"}
  13.  
  14. bang
  15. imp subprocess
  16. frm math *
  17. frm tkinter Tk,Entry,Text,Frame,Label,StringVar
  18. frm tkinter.ttk Progressbar,Style
  19.  
  20. cl App Tk
  21. +
  22. df s __init__
  23. +
  24. fc l {super().__init__}
  25. fc s configure $BG
  26. vf s main MainWin self
  27. fc s main.pack $TEXT_PACK
  28. fc s main.bind_all {"<Control-t>", self.newFrame}
  29. fc s main.bind_all {"<Control-T>", self.delFrame}
  30. -
  31.  
  32. de s newFrame
  33. +
  34. vf s sec MainWin self
  35. fc s sec.pack $TEXT_PACK
  36. fc s main.goCli
  37. -
  38.  
  39. de s delFrame
  40. +
  41. fc s sec.destroy
  42. -
  43. -
  44.  
  45. cl MainWin Frame
  46. +
  47. df s __init__ parent
  48. +
  49. fc l {super().__init__} parent
  50. va s master parent
  51. fc s configure $BG
  52. vf s theCmd StringVar
  53. vf s insPos StringVar
  54. vf s pyTxt StringVar
  55. vf s py_label Label $PYTXT_PROP
  56. fc s py_label.pack $PYTXT_PACK
  57. vf s pos_label Label $STATS_PROP
  58. fc s pos_label.pack $STATS_PACK
  59. vf s cli_entry Entry $CLI_PROP
  60. fc s cli_entry.pack $CLI_PACK
  61. vf s total_pb Progressbar $PBP
  62. fc s total_pb.pack $CLI_PACK
  63. vf s main_text Text $TEXT_PROP
  64. fc s main_text.pack $TEXT_PACK
  65. vf s style Style self
  66.  
  67. bd s main_text {
  68. <Escape> goCli
  69. <Tab> indent
  70. <Key> autoComplete
  71. }
  72.  
  73. bd s cli_entry {
  74. <Up> lineUp
  75. <Down> lineDown
  76. <Escape> clearCmd
  77. <F5> runPy
  78. <Return> mainSend
  79. <Shift-Return> newLine
  80. <Control-Return> showPy
  81. <Control-j> mainfontSize
  82. <Control-o> openFile
  83. <Control-O> openSelf
  84. <Control-s> saveFile
  85. <Control-l> clearText
  86. <Control-f> findWord
  87. <Control-n> newFile
  88. }
  89.  
  90. fc s style.configure {"TProgressbar",thickness=5}
  91. fc s main_text.config {yscrollcommand=self.trackScroll}
  92. fc s main_text.tag_configure {"currline", background="#aaa"}
  93. fc s main_text.tag_configure {"found", background="#ddd"}
  94.  
  95. fc s newFile
  96. fc s cli_entry.focus
  97. -
  98.  
  99. de s newFile
  100. +
  101. va s file_name {"empty"}
  102. fc s clearCmd
  103. fc s clearText
  104. -
  105.  
  106. df s subProc w,cmd
  107. +
  108. ytr
  109. +
  110. vf l output subprocess.run {cmd,shell=True,executable="/bin/bash",stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True}
  111. ff l {w == "main"}
  112. +
  113. ff l output.returncode {== 0}
  114. +
  115. fc s main_text.insert {"insert",f"\n{output.stdout}\n"}
  116. -
  117. el
  118. +
  119. fc s clearText
  120. fc s main_text.insert {"insert",output.stderr}
  121. -
  122. -
  123. ef l {w == "sec"}
  124. +
  125. fc s master.sec.main_text.insert {"1.0",output.stdout}
  126. -
  127. el
  128. +
  129. fc s pyTxt.set {output.stdout}
  130. -
  131. -
  132. exc
  133. +
  134. fc s main_text.insert {"1.0",str(e)}
  135. -
  136. -
  137.  
  138. de s showPy
  139. +
  140. fc s subProc {"sec", f"tclsh {self.file_name}"}
  141. -
  142.  
  143. de s runPy
  144. +
  145. fc s main_text.mark_set {"insert","end"}
  146. fc s subProc {"main",f"tclsh {self.file_name} | python3"}
  147. fc s main_text.mark_set {"insert","1.0"}
  148. -
  149.  
  150. de s goCli
  151. +
  152. fc s cli_entry.focus
  153. fc s seeLine
  154. -
  155.  
  156. de s clearText
  157. +
  158. fc s main_text.delete {"1.0", "end-1c"}
  159. fc s countWords
  160. -
  161.  
  162. de s clearCmd
  163. +
  164. fc s theCmd.set "''"
  165. fc s main_text.tag_remove {"found","1.0","end"}
  166. -
  167.  
  168. de s newLine
  169. +
  170. fc s main_text.insert {"insert", f"\n"}
  171. fc s clearCmd
  172. fc s countWords
  173. -
  174.  
  175. de s getLine
  176. +
  177. vf k line main_text.get {"insert linestart","insert lineend"}
  178. rt line
  179. -
  180.  
  181. de s delLine
  182. +
  183. vf k line main_text.delete {"insert linestart","insert lineend"}
  184. -
  185.  
  186. de s highlightLine
  187. +
  188. fc s main_text.tag_remove {"currline","1.0","end"}
  189. fc s main_text.tag_add {"currline","insert linestart","insert lineend"}
  190. -
  191.  
  192. de s checkLine
  193. +
  194. vf k curline getLine
  195. fc s theCmd.set {f"{curline}"}
  196. fc s highlightLine
  197. -
  198.  
  199. de s seeLine
  200. +
  201. fc s main_text.see {"insert"}
  202. fc s countWords
  203. -
  204.  
  205. de s lineUp
  206. +
  207. vf k currline main_text.mark_set {"insert", "insert - 1 line"}
  208. fc s checkLine
  209. fc s seeLine
  210. -
  211.  
  212. de s lineDown
  213. +
  214. vf k line main_text.mark_set {"insert", "insert + 1 line"}
  215. fc s checkLine
  216. fc s seeLine
  217. -
  218.  
  219. de s mainSend
  220. +
  221. vf k cmd theCmd.get
  222. fc s delLine
  223. ff l {cmd[:2] == "s "}
  224. +
  225. vf l c {cmd[2:].strip}
  226. fc s subProc {"main",f"{c}"}
  227. fc s main_text.mark_set {"insert","1.0"}
  228. fc s seeLine
  229. -
  230. ef l {cmd[:2] == "e "}
  231. +
  232. vf l c {cmd[2:].strip}
  233. ytr
  234. +
  235. vf l expr eval c
  236. fc s main_text.insert {"insert",f"\n{expr}\n"}
  237. fc s main_text.mark_set {"insert","1.0"}
  238. fc s seeLine
  239. -
  240. exc
  241. +
  242. fc s main_text.insert {"insert",f"\n{e}\n"}
  243. fc s main_text.mark_set {"insert","1.0"}
  244. fc s seeLine
  245. -
  246. -
  247. el
  248. +
  249. fc s main_text.insert {"insert", cmd}
  250. fc s seeLine
  251. -
  252. rt {"break"}
  253. -
  254.  
  255. de s openIt
  256. +
  257. wi a open {file_name,"r"} file
  258. +
  259. vf l data file.read
  260. fc l file.close
  261. -
  262. fc s clearText
  263. fc s main_text.insert {"1.0",data}
  264. fc s main_text.mark_set {"insert","1.0"}
  265. fc s countWords
  266. -
  267.  
  268. de s openFile
  269. +
  270. vf t file_name theCmd.get
  271. ff s {file_name == ""}
  272. +
  273. fc s pyTxt.set {"can't open empty!"}
  274. ps
  275. -
  276. fc s clearCmd
  277. fc s openIt
  278. -
  279.  
  280. de s openSelf
  281. +
  282. va s file_name {"newed"}
  283. fc s openIt
  284. -
  285.  
  286. de s saveFile
  287. +
  288. ff s file_name {== "empty"}
  289. +
  290. vf t file_name theCmd.get
  291. fc s clearCmd
  292. -
  293. vf k data main_text.get {"1.0","end-1c"}
  294. wi a open {file_name,"w"} file
  295. +
  296. fc l file.write data
  297. fc l file.close
  298. -
  299. fc s countWords
  300. fc s pyTxt.set {"saved"}
  301. -
  302.  
  303. de s seePy
  304. +
  305. vf k data getLine
  306. va l f {".tmp"}
  307. wi l open {f,"w"} file
  308. +
  309. fc l file.write {f"source ytcl.tcl\n{data}\n"}
  310. fc l file.close
  311. -
  312. fc s subProc {"l",f"tclsh {f}"}
  313. -
  314.  
  315. df s trackScroll {top,bottom}
  316. +
  317. fc s total_pb.configure {maximum=100}
  318. vf l bot_perc float {bottom} {* 100}
  319. va s {total_pb['value']} bot_perc
  320. -
  321.  
  322. de s mainfontSize
  323. +
  324. vf k sz theCmd.get
  325. vf l fsz int sz
  326. fc s main_text.configure {font=("VictorMono",fsz)}
  327. fc s clearCmd
  328. -
  329.  
  330. de s findWord
  331. +
  332. vf k word theCmd.get
  333. ff l word {== ""}
  334. +
  335. fc s pyTxt.set {"empty"}
  336. -
  337. el
  338. +
  339. va l si {"1.0"}
  340. va l tot 0
  341. fc s main_text.tag_remove {"found","1.0","end"}
  342. wh True
  343. +
  344. vf k si main_text.search {word, si, "end"}
  345. ff l not si
  346. +
  347. br
  348. -
  349. va l ei {f"{si}+{len(word)}c"}
  350. fc s main_text.tag_add {"found",si,ei}
  351. va l si ei
  352. ac l tot 1
  353. -
  354. fc s pyTxt.set {f"{tot} found: {word}"}
  355. -
  356.  
  357. -
  358.  
  359. de s countWords
  360. +
  361. vf t ins_pos main_text.index {"insert"}
  362. vf k data main_text.get {"1.0","end"}
  363. vf l words_counted data.split
  364. vf l lines_counted data.split {"\n"}
  365. vf l lns len lines_counted
  366. vf l wrds len words_counted
  367. fc s insPos.set {f"{self.ins_pos} {self.file_name} : {wrds} | {lns-1}"}
  368. fc s seePy
  369. fc s total_pb.configure {maximum=lns}
  370. vf k spltpos ins_pos.split {'.'}
  371. vf l inspos int {spltpos[0]}
  372. va s {total_pb['value']} inspos
  373. -
  374.  
  375. de s indent
  376. +
  377. vf k cursor_pos main_text.index {"insert"}
  378. fc s main_text.insert {cursor_pos, f"+\n\n-"}
  379. fc s main_text.mark_set {"insert", f"{cursor_pos} + 1 line"}
  380. rt {"break"}
  381. -
  382.  
  383. de s autoComplete
  384. +
  385. vf k cursor_pos main_text.index {"insert"}
  386. ff - {event.char == "'"}
  387. +
  388. fc s main_text.insert {cursor_pos, "'"}
  389. -
  390. ef - {event.char == "<"}
  391. +
  392. fc s main_text.insert {cursor_pos, ">"}
  393. fc s main_text.mark_set {"insert", cursor_pos}
  394. -
  395. ef - {event.char == "("}
  396. +
  397. fc s main_text.insert {cursor_pos, ")"}
  398. fc s main_text.mark_set {"insert", cursor_pos}
  399. -
  400. ef - "event.char == \"\{\""
  401. +
  402. fc s main_text.insert "cursor_pos, \"}\""
  403. fc s main_text.mark_set {"insert", cursor_pos}
  404. -
  405. ef - {event.char == "["}
  406. +
  407. fc s main_text.insert {cursor_pos, "]"}
  408. fc s main_text.mark_set {"insert", cursor_pos}
  409. -
  410. ef - {event.char == '\"'}
  411. +
  412. fc s main_text.insert {cursor_pos, '\"'}
  413. fc s main_text.mark_set {"insert", cursor_pos}
  414. -
  415. -
  416. mn App tclyed
  417.  
  418.  
  419.  
  420.  
  421.  

Add a comment

Please note that this site uses the meta tags nofollow,noindex for all pages that contain comments.
Items are closed for new comments after 1 week