Basic usage: write a Linux desktop script

Source: Internet
Author: User
This series of articles explores how to use Python to create scripts for GNOME Desktops, screenlet frameworks, and Nautilus to provide a highly productive environment. Script enabled on the desktop

This series of articles explores how to use Python to create scripts for GNOME Desktops, screenlet frameworks, and Nautilus to provide a highly productive environment. The drag-and-drop feature is enabled for scripts on the desktop to quickly access frequently used information and services. This section describes how to use the screenlet toolkit to build desktop applications.

Developing applications for Linux desktops usually requires some Graphical User interfaces (GUI) framework as the basis for building. Options include GTK + for GNOME Desktop and Qt for K Desktop environments (KDE. These two platforms provide everything developers need to build GUI applications, including libraries and layout tools to create windows that users can see. This article shows you how to build a desktop productivity application based on the screenlet widget kit.

Some existing applications will be categorized into desktop productivity categories, including GNOME Do and Tomboy. These applications usually allow users to interact directly with them from the desktop by dragging and dropping through a specific key combination or from another application, such as Mozilla Firefox. Tomboy is used as a desktop note tool and supports text drag and drop from other windows.

Getting started with Screenlet

You need to install some programs to start developing screenlet. First, install the screenlets package using the Ubuntu Software Center or command line. In the Ubuntu Software Center, type screenlets in the Search box. You should see two options: the main package and the independent installation package in the document.

You can use Python to program screenlet in Python and Ubuntu. The basic installation of Ubuntu 10.04 includes Python 2.6, because many utilities depend on it. You may need other libraries, depending on your application requirements. For this article, I installed and tested everything on Ubuntu 10.04. Next, download the screenlet source code from the screenlets.org site. The Test screenlet is located in the src/share/screenlets/Test folder and uses Cairo and GTK, which also need to be installed. The entire source code of the test program is located in the TestScreenlet. py file. Open this file in your favorite editor to view the screenlet infrastructure.

Python is highly object-oriented, so class keywords are used to define objects. In this example, the class is named TestScreenlet and has some defined methods. In TestScreenlet. py, pay attention to the 42nd lines in the following code:

Def _ init _ (self, ** keyword_args ):

Python uses the underscore (_) to identify system functions through predefined behaviors. In this example, the __init _ function has all the intent and purpose for the class constructor, and contains any number of initialization steps to be executed when creating a new instance of the object. By convention, the first parameter of each class method is a reference to the current instance of the class and is named as self. With this behavior, you can easily use self to reference the methods and attributes of its instance:

Self. theme_name = "default"

The Screenlet framework defines naming conventions and standards, which are outlined on the developer page at screenlets.org. There is also a link to the source code of the screenlet package and the Application Programming Interface (API) documentation. The code also gives you an insight into what each function can do by calling parameters and what is returned.

Write simple screenlet

The basic components of Screenlet include the icon file, source code file, and topic folder. The topic folder contains additional folders for different themes. You will find the sample template and the files and folders required for getting started on screenlets.org.

For the first example, use the provided template to create a basic "Hello World" application. the code of this basic application is shown in listing 1.

Listing 1. Python code for Hello World screenlet

  1. #! /Usr/bin/env pythonimport screenletsclass HelloWorldScreenlet (screenlets. screenlet) :__ name _ = 'helloworld' _ version _ = '0. 1' _ author _ = 'John Doe '_ desc _ = 'simple Hello World screenlet' def _ init _ (self, ** kwargs ): # Customize the width and height. screenlets. screenlet. _ init _ (self, width = 180, height = 50, ** kwargs) def on_draw (self, ctx): # Change the color to white and fill the screenlet. ctx. set _ Source_rgb (255,255,255) self. draw_rectangle (ctx, 0, 0, self. width, self. height) # Change the color to black and write the message. ctx. set_source_rgb (0, 0, 0) text = 'Hello World! 'Self. draw_text (ctx, text, 10, 10, "Sans 9", 20, self. width) if _ name _ = "_ main _": import screenlets. sessionscreenlets. session. create_session (HelloWorldScreenlet)

Each application must import the screenlet framework and create a new session. There are also some other minimum requirements, including any initialization steps and basic drawing functions, to present widgets, TestScreenlet, on the screen. the py example has the _ init _ method used to initialize the object. In this example, you will see a line containing the call to the _ init _ method of screenlet, which sets the initial width and height of the window to be created for this application.

The only other function required by this application is the on_draw method. this routine sets the background color of the box to white and draws a rectangle using the previously defined dimension, which sets the text color to black, set the source text to "Hello World! ", Then draw the text. Figure 1 shows what you should see when running this screenlet. This basic structure persists when you create more useful applications on these simple blocks.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.