python-23-mobile phone APP application example

The general idea of ​​Kivy is: kv code manages interface, python code manages logic.

These two words args and kwargs are often seen in code in Python, usually with one or two asterisks in front of them. In fact, this is just the variable name agreed upon by programmers. args is the abbreviation of arguments, which means positional parameters; kwargs is the abbreviation of keyword arguments, which means keyword parameters. This is actually the two forms of variable parameters in Python, and *args must be placed before **kwargs, because positional parameters are before keyword arguments.

1 hello world

1.1 Pure python file

File main.py

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

class IndexPage(BoxLayout):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.btn = Button(text = "helloworld")
        self.add_widget(self.btn)

class TestApp(App):
    def build(self):
        return IndexPage()

if __name__ == "__main__":
    TestApp().run()

1.2 python file and .kv file

Replace the following two lines of code
self.btn = Button(text = "helloworld")
self.add_widget(self.btn)

File test.kv

<IndexPage>:
    Button:
        text: "helloworld"

File main.py

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

class IndexPage(BoxLayout):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)

class TestApp(App):
    def build(self):
        return IndexPage()

if __name__ == "__main__":
    TestApp().run()

2 button button event

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

class IndexPage(BoxLayout):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.btn = Button(text = "press me")
        self.btn.bind(on_press = self.press_button)
        self.add_widget(self.btn)

    def press_button(self, arg):
        print("press button is running")
        
class TestApp(App):
    def build(self):
        return IndexPage()

if __name__ == "__main__":
    TestApp().run()

3 Label

Download fonts support Chinese

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout

class IndexPage(BoxLayout):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        # Button按钮
        self.btn = Button(text = "press me")
        self.btn.bind(on_press = self.press_button)
        self.add_widget(self.btn)
        # Label标签
        self.la = Label(font_name = "./arialuni.ttf")
        self.add_widget(self.la)
    def press_button(self, arg):
        self.la.text = "show you看"

class TestApp(App):
    def build(self):
        return IndexPage()

if __name__ == "__main__":
    TestApp().run()

Guess you like

Origin blog.csdn.net/qq_20466211/article/details/113794117