1. Introduction

Hello, I’m Anguo!

In my last article, I wrote that I could use Aardio to develop desktop applications in combination with Python. Some friends left messages to me in the background, saying that there was too little information about Aardio, hoping that I could add some practical functions

Practical | use aardio Python rapid development of desktop applications

This article will talk about some of the more practical advanced uses of Aardio

2. Network request

In Aardio, you can use “inet.http” to send network requests

Let’s take the simplest GET request as an example

Here you add a button, add a click event to the button, send a network request within the event, and print out the result of the request

import win.ui; import console; import inet.http; . Mainform.get_btn. onCommand = function(id,event){var HTTP = inet.http(); Var result,err,errCode = http.get("http://ip address /get? username=xag"); http.close(); If (err) {the console. The log (" error code: "errCode, error message:" ", err); }else{ console.log(result); } console.pause() } mainForm.show(); return win.loopMessage();Copy the code

For POST requests, you can do the following:

It is important to note that when creating the request object HTTP, you can customize the request header and request body parameters

import win.ui; import console; import inet.http; . Mainform.post_btn. onCommand = function(id,event){var HTTP = inet.http(); HTTP. AddHeaders = {[" content-type "] = 'application/json'} {a = 1, b = 2} var result, err, errCode. = HTTP post (" http://ip address/books, "{} a = 1, b = 2). http.close(); If (err){console.log(err); }else{ console.log(result); } console.pause() }Copy the code

3. Customize libraries

Through the custom library, we can encapsulate some common logic to facilitate the hierarchical management of code

The operation mode is as follows:

First, open the project, right-click “User Libraries” in the project root directory, and select “New Library”

After you enter the name, a custom library will be automatically created under the folder

Then, write the business logic in the custom library

PS: For demonstration purposes, I just wrote a simple method that returns a string directly

// Customize a global method: custom_func custom_func = function(){return "xag"; }}Copy the code

Note that to make it easier to call a custom method, I have defined it as a global method, without referring to the keyword var

Finally, we import the library name into the source code of the form and call it in the format of “library name. custom methods ()”

Customlib import customLib... // Call the method defined in the library directly and print the result console.log(customlib.custom_func()); .Copy the code

4. Simple threads

Also in AARDIO, we can perform time-consuming operations in threads

The operation steps are as follows:

First, we define a function for the time-consuming operation, which is set to a member property of the form object

import fonts.fontAwesome; import win.ui; import console; . Mainform.test_func =function(){import console; Console. log("test_func ")}...Copy the code

You can then call the above function using the built-in “Thread. invoke” function

Note that the first argument to thread.invoke is an anonymous function, followed by the parameters specified for the anonymous function

We pass the form object and other parameters to the anonymous function, and then use the form object inside the function to call the function defined above

Need to be

. Mainform.calc. onCommand = function(id,event){//invoke parameters: 1, 2... thread.invoke( function(mainForm,url){ mainForm.test_func(); . / / set the controls can click mainForm. Calc. DisabledText = null; },mainForm,"http://www.baidu.com" ) } mainForm.show(); Return win.loopmessage ();Copy the code

5. Execute Python in a thread

In the previous section, we called functions defined in Python files directly from the main thread, but it was a bad experience to use functions for time-consuming operations

Here, I define a simple time-consuming function in a Python file

Import time def exec_OPERATION1 (): print(" start 1") time.sleep(5) print(" end 1") return "success1"Copy the code

Now we call this function by setting the click event for a button in the form’s source code

The specific steps are as follows:

First, place the Python file in the RES folder, then load the file and release the GIL lock

import win.ui; import console console.open() import py3; . // Load the Python file pyCode = string.load("\res\tp.py"); // Execute pycode py3.exec(pycode); // Release GIL Py3. releaseThread(); .Copy the code

Note that you use “console.open()” to open the debugger and look for exceptions in the thread

Then, create a thread function on a form object and call the function in a Python file using the following method

PyThread1 = function(mainForm) {import py3; Import console // The thread must import console, Function (){var result = tostring(py3.main.exec_operation1()) console.log(result)  console.log(type(result)) } ) } ...Copy the code

Finally, in the button click event, just call the method defined above

. MainForm. Button. The oncommand = function (id, event) {/ / call the method that defined above thread. Invoke (mainForm pyThread1, mainForm)}...Copy the code

6. The final

Many friends left messages to me in the background, saying that there was too little information about Aardio. In fact, the “Start page” in the Aardio editor software provided some official information and tips

The “example” in the lower right corner of the editor lists the common development skills of desktop development, and the toolbar provides interface processing, coding conversion, icon making, library function documents and other utility tools

You can use HTmLayout to customize the control. Aardio combines htMLayout to provide more freedom

In addition, you can beautify buttons, input boxes and selection boxes in the toolbar – Interface – Plus color matching tool

Finally, some excellent learning websites are listed for your reference to learn and improve

www.aardio.net/

bbs.aardio.com/

www.htmlayout.cn/

Github.com/search?q=aa…

If you think the article is good, please like, share, leave a message, because this will be my continuous output of more high-quality articles the strongest power!

Recommended reading

How does Postman debug an encrypted interface?

5 minutes, using Intranet penetration to quickly achieve remote desktop

How can Jmeter execute Python scripts concurrently

Talk about the best PC automation solution – Pywinauto

Talk about the best PC automation scheme – WinAppDriver

Practical | use aardio Python rapid development of desktop applications