Kivy Chinese Tutorial Example Primer (Simple Paint App): 1. Custom Widgets (Widgets)

Source: Internet
Author: User

1. Framework Code

Create a new project named Simplepaintapp with Pycharm, and then create a new Python source file named simple_paint_app.py.

In the Code Editor, enter the following framework code

1  fromKivy.appImportApp2  fromKivy.uix.widgetImportWidgets3  4  5 classMypaintwidget (widgets):6     Pass7  8  9 classMypaintapp (App):Ten     defBuild (self): One         returnMypaintwidget () A   -   - if __name__=='__main__': theMypaintapp (). Run ()

Run the above code to display a window with a black background

Look boring, but don't underestimate the lines of code. These are the simple frame of the framework code, it is like the skeleton of the program, the latter will lead you to add a variety of new features on this skeleton, gradually enrich and improve the application.

Line 5th   class  mypaintwidget (widget :  inherit from the class Widget and construct our custom widget Mypaintwidget. The main logic of the artboard will be implemented in the Mypaintwidget class. Now we've only written a   pass  (line 6th), equivalent to a placeholder, first let the whole code run, specific functions, We'll add that in a later tutorial.

Line 12th return mypaintwidget() creates and returns a custom Widget object Mypaintwidget when the initialization is applied (called the Build method)

2. Add interaction

Now that our custom widgets can't do anything, we'll try to make it responsive to the user's actions.

The code is as follows:

1  fromKivy.appImportApp2  fromKivy.uix.widgetImportWidgets3  4  5 classMypaintwidget (widgets):6     defOn_touch_down (self, Touch):7         Print(Touch)8  9  Ten classMypaintapp (App): One     defBuild (self): A         returnMypaintwidget () -   -   the if __name__=='__main__': -Mypaintapp (). Run ()

Run the modified code and still show a black window that doesn't seem to change much. However, when you click in the window with the mouse, it is found that there is output in the console of the Pycharm, and the output number will change as the click position is different.

When the user clicks on the window with the mouse, it will trigger the Mypaintwidget On_touch_down method (line 6th). The touch parameter of the On_touch_down method contains the position information when the mouse is clicked. Here, we haven't implemented any useful interactions, just output the location information of the mouse click to the console, the 7th line of code Print(touch)

Thinking

    • Using the application of this section to do the experiment, think of the coordinate system used by kivy what kind of? (where is the origin?) What's the direction of X and Y? )

Original link: http://www.ipaomi.com/2017/11/15/kivy-Chinese tutorial-Getting started with an example-simple artboard-simple-paint-app:1-custom window/

Kivy Chinese Tutorial Example Primer (Simple Paint App): 1. Custom Widgets (Widgets)

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.