TinEngine usage - embedded code

Posted by wafflestomper on Thu, 30 Dec 2021 09:12:39 +0100

introduction

In HTML, javascript occupies a very important part. Both front-end and back-end, javascript has brought a lot of operability and interactivity to web pages, making web pages richer and more consistent with people's operation.

In Tin, the writer can use the < code > tag to write embedded code and inline a script of a certain format into the Tin file. This is similar to the < script > element of HTML. This tag allows writers to use specific scripts to enhance the operability and interactivity of files.

General use

The < code > tag allows four parameters.

<code>Code name;Code type[;parameter]
code
</code>

The code name is the flag of the code segment in this TinText. When the name is repeated, it means that the code segment is overwritten or updated; When the name is "now", the code segment is executed immediately without triggering.

Code types are the script types allowed to run in TinText, including python, tcl and vbs. Among them, Python and tcl have restrictions. For specific restrictions, see the instructions for TinEngine, or download TinReader to obtain the instruction file. vbs files can be run using wscript of Windows system by generating temporary files.

Code parameters are a series of parameters, which are divided by "|". This is not required. These parameters exist in the form of "% parameter%" in the code segment. When the code segment is triggered and the code segment parameters are given, these parameters will be replaced. Otherwise, the parameter content is the default value of the parameters. This option is not useful to ordinary users, but if you want your Tin file to impress readers, you can also try this function.

Code trigger

At present, you can trigger inline code using three methods: immediate start, < do > tag and internal function.

Let's take the following Python code as an example:

self.insert('end','main')

Immediate start

As mentioned above, when the code name is "now", TinText will directly run this code and block rendering.

<code>now;python
self.insert('end','main')
</code>

do tag start

The < do > tag is a specific tag for starting the specified script in Tin. Generally, a parameter is used: < do > code_ name[;args]. See Tin documentation for more usage.

This method is a preceding code segment

<code>test;python
self.insert('end','main')
</code>

;Code snippets need to be loaded in advance
<do>test

Internal function start

In TinText, use the function = = self get_ codes_ to_ Run (name, args: List = []) = = trigger the specified code segment.

Preceding code segment

<code>test;python
self.insert('end','main')
</code>

<tkinter>
get_codes_to_run('test')
</tkinter>

Post snippet

<tkinter>
window_create(self.end,window=Button(self,text='code button',command=lambda:self.get_codes_to_run('test')))
</tkinter>
;stay TinReader In, use:
;<tkinter>
;window_create(self.end,window=Button(self,text='code button',command=lambda:MyText.get_codes_to_run('test')))
;</tkinter>

<code>test;python
self.insert('end','main')
</code>

Effects of post snippets:

Although this code is very simple, it is enough to show the importance of < code > in Tin.

Use parameters

For ordinary users, the above use is enough, but for writers who want to play tricks, there is no parameter setting, but there is less fun.

Next, let's still take the above simple Python code as an example to see how to use parameters in < code >.

Execute directly, ignoring parameters

In Tin, the code can have a parameter list, but when a piece of code is triggered, the parameters can not be passed, or the parameters can be incomplete (it is not recommended to pass the parameters or not to use the default value). When the parameters are passed, the words "% parameter%" in the code segment will be replaced with the parameter value, otherwise it will be directly replaced with "parameter"

Then the above code segment needs to be rewritten:

<code>now;python;self.end|main
self.insert('end','TIN')
self.insert(%self.end%,' %main%')
</code>

At this time, TIN main is displayed in TinText.

Import parameters

Trigger tag

If the < do > tag is used for triggering, the method of not transmitting parameters is as follows:

<code>test;python;self.end|main
self.insert('end','TIN')
self.insert(%self.end%,' %main%')
</code>

<do>test

At this time, TIN main is displayed in TinText.

How to transfer parameters:

<code>test;python;self.end|main
self.insert('end','TIN')
self.insert(%self.end%,' %main%')
</code>

<do>test;1.1|code

At this time, T codeIN is displayed in TinText.

Internal function

Using internal functions, you can still pass no parameters:

<code>test;python;self.end|main
self.insert('end','TIN')
self.insert(%self.end%,' %main%')
</code>

<code>now;python
self.get_codes_to_run('test')
</code>

At this time, TIN main is displayed in TinText.

Transfer parameters:

<code>test;python;self.end|main
self.insert('end','TIN')
self.insert(%self.end%,' %main%')
</code>

<code>now;python
self.get_codes_to_run('test',['1.1','code'])
</code>

At this time, T codeIN is displayed in TinText.

epilogue

This is the basic method of using embedded code segments in Tin language. Using script control can enrich the interactive functions of TinText.

Topics: Python Tkinter