进入后如何调用函数直到离开kivy?

问题描述 投票:0回答:3

进入离开kivy后如何调用函数? 我正在通过移动 kivy 应用程序编写鼠标控制应用程序。 我的代码: 进口猕猴桃 从 kivy.app 导入 App 从 kivy.uix.boxlayout 导入 BoxLayout 进口插座 导入时间

kivy.require("1.9.0")

客户端=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

MyRoot 类(BoxLayout): def init(自身): self.command = “” 自身体积 = 0 super(MyRoot, self).init()

def shutdown(self):
    self.command = "shutdown"
    self.send_message()

def tangAmLuong(self):
    self.command = "tang_am_luong"
    self.send_message()
    if self.vol <= 98:
        self.vol += 2
        self.volume_text.text = str(self.vol)
    time.sleep(0.1)

def giamAmLuong(self):
    self.command = "giam_am_luong"
    self.send_message()
    if self.vol >= 2:
        self.vol -= 2
        self.volume_text.text = str(self.vol)
    time.sleep(0.1)

def trai(self):
    self.command = "sang_trai"
    self.send_message()

def phai(self):
    self.command = "sang_phai"
    self.send_message()

def len(self):
    self.command = "len"
    self.send_message()

def xuong(self):
    self.command = "xuong"
    self.send_message()

def chuot_trai(self):
    self.command = "chuot_trai"
    self.send_message()

def chuot_phai(self):
    self.command = "chuot_phai"
    self.send_message()

def send_message(self):
    client.send(f"{self.nickname_text.text}: {self.command}".encode("utf-8"))

def connect_to_server(self):
    if self.nickname_text != "":
        client.connect((self.ip_text.text, 9999))
        self.send_message()

课程风格(应用程序):

def build(self):
    return MyRoot()

webChat = 样式() webChat.run()

Kivy文件:

ip_text: ip_text
nickname_text: nickname_text
connect_btn: connect_btn
connection_grid: connection_grid
shutdown_btn: shutdown_btn
tangal_btn: tangal_btn
giamal_btn: giamal_btn
volume_text: volume_text

orientation: 'vertical'

GridLayout:
    id: connection_grid
    rows: 1
    cols: 2
    padding: 10
    spacing: 10
    height: 125
    size_hint: (1, None)
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: "Server IP"
            font_size: 42
            color: 0.92, 0.45, 0, 1
        TextInput:
            id: ip_text
            size_hint: (1, None)
            height: 50
            font_size: 36
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: "Nickname"
            font_size: 42
            color: 0.92, 0.45, 0, 1
        TextInput:
            id: nickname_text
            size_hint: (1, None)
            height: 50
            font_size: 36
Button:
    id: connect_btn
    text: "Connect"
    font_size: 32
    size: 100, 50
    size_hint: (1, None)
    height: 50
    on_press: root.connect_to_server()
Button:
    id: shutdown_btn
    text: "Tắt máy"
    font_size: 32
    size: 100, 50
    size_hint: (1, None)
    height: 50
    on_press: root.shutdown()
Button:
    id: tangal_btn
    text: "Tăng âm lương"
    font_size: 32
    size: 100, 50
    size_hint: (1, None)
    height: 50
    on_press: root.tangAmLuong()
Button:
    id: giamal_btn
    text: "Giảm âm lượng"
    font_size: 32
    size: 100, 50
    size_hint: (1, None)
    height: 50
    on_press: root.giamAmLuong()
Label:
    id: volume_text
    text: "0"
    font_size: 36
GridLayout:
    rows: 3
    cols: 3
    Button:
        text: "Chuột Trái"
        font_size: 14
        on_press: root.chuot_trai()
    Button:
        text: "Lên"
        font_size: 14
        on_press: root.len()
    Button:
        text: "Chuột Phải"
        font_size: 14
        on_press: root.chuot_phai()
    Button:
        text: "Trái"
        font_size: 14
        on_press: root.trai()
    Label:
        text: ""
        font_size: 14
    Button:
        text: "Phải"
        font_size: 14
        on_press: root.phai()
    Label:
        text: ""
        font_size: 14
    Button:
        text: "Xuống"
        font_size: 14
        on_press: root.xuong()
    Label:
        text: ""
        font_size: 14
python android kivy kivy-language kivymd
3个回答
0
投票

我稍微改变了你的代码希望它有帮助。

请查看:https://kivy.org/doc/stable/api-kivy.properties.html

请原谅我的拼写(hung_nguyen)

hung_nguyen.py

    from kivy.app import App
    from kivy.lang.builder import Builder
    from kivy.properties import StringProperty, NumericProperty
    from kivy.uix.boxlayout import BoxLayout
    from socket import socket, AF_INET, SOCK_STREAM
    from time import sleep
    
    Builder.load_file('hung_nguyen.kv')
    client = socket(AF_INET, SOCK_STREAM)
    
    
    class MyRoot(BoxLayout):
        ip_text = StringProperty()
        nickname_text = StringProperty()
        connect_btn = StringProperty()
        connection_grid = StringProperty()
        shutdown_btn = StringProperty()
        tangal_btn = StringProperty()
        giamal_btn = StringProperty
        volume_text = StringProperty()
    
        vol = NumericProperty()
    
        def __init__(self):
            super(MyRoot, self).__init__()
            self.command = None
            pass
    
        def shutdown(self):
            self.command = "shutdown"
            self.send_message()
    
        def tangAmLuong(self):
            self.command = "tang_am_luong"
            self.send_message()
            if self.vol <= 98:
                self.vol += 2
                self.volume_text = str(self.vol)
            sleep(0.1)
    
        def giamAmLuong(self):
            self.command = "giam_am_luong"
            self.send_message()
            if self.vol >= 2:
                self.vol -= 2
                self.volume_text = str(self.vol)
            sleep(0.1)
    
        def trai(self):
            self.command = "sang_trai"
            self.send_message()
    
        def phai(self):
            self.command = "sang_phai"
            self.send_message()
    
        def len(self):
            self.command = "len"
            self.send_message()
    
        def xuong(self):
            self.command = "xuong"
            self.send_message()
    
        def chuot_trai(self):
            self.command = "chuot_trai"
            self.send_message()
    
        def chuot_phai(self):
            self.command = "chuot_phai"
            self.send_message()
    
        def on_ip_text(self, *args):
            print('on_ip_text', self.ip_text, args)
            pass
    
        def on_nickname_text(self, *args):
            print('on_nickname_text', self.nickname_text)
            pass
    
        def on_connect_btn(self, *args):
            print('on_connect_btn', self.connect_btn)
            pass
    
        def on_connection_grid(self, *args):
            print('on_connection_grid', self.connection_grid)
            pass
    
        def on_shutdown_btn(self, *args):
            print('on_shutdown_btn', self.shutdown_btn)
            pass
    
        def on_tangal_btn(self, *args):
            print('on_tangal_btn', self.tangal_btn)
            pass
    
        def on_giamal_btn(self, *args):
            print('on_giamal_btn', self.giamal_btn)
            pass
    
        def on_volume_text(self, *args):
            print('on_volume_text', self.volume_text)
            pass
    
        def on_vol(self, *args):
            print('on_vol', self.vol)
            pass
    
        def send_message(self):
            try:
                client.send(f"{self.nickname_text}: {self.command}".encode("utf-8"))
            except OSError as os_err:
                print(os_err)
    
        def connect_to_server(self):
            if self.nickname_text != "":
                client.connect((self.ip_text.text, 9999))
                self.send_message()
    
    
    class style(App):
        def build(self):
            return MyRoot()
    
    
    if __name__ == '__main__':
        style().run()

hung_nguyen.kv

<MyRoot>:
    ip_text: root.ip_text
    nickname_text: root.nickname_text
    connect_btn: root.connect_btn
    connection_grid: root.connection_grid
    shutdown_btn: root.shutdown_btn
    tangal_btn: root.tangal_btn
    giamal_btn: root.giamal_btn
    volume_text: root.volume_text
    BoxLayout:
        orientation: 'vertical'
        GridLayout:
            id: connection_grid
            rows: 1
            cols: 2
            padding: 10
            spacing: 10
            height: 125
            size_hint: (1, None)
            BoxLayout:
                orientation: 'vertical'
                Label:
                    text: "Server IP"
                    font_size: 42
                    color: 0.92, 0.45, 0, 1
                TextInput:
                    id: ip_text
                    size_hint: (1, None)
                    height: 50
                    font_size: 36
            BoxLayout:
                orientation: 'vertical'
                Label:
                    text: "Nickname"
                    font_size: 42
                    color: 0.92, 0.45, 0, 1
                TextInput:
                    id: nickname_text
                    size_hint: (1, None)
                    height: 50
                    font_size: 36
        Button:
            id: connect_btn
            text: "Connect"
            font_size: 32
            size: 100, 50
            size_hint: (1, None)
            height: 50
            on_press: root.connect_to_server()
        Button:
            id: shutdown_btn
            text: "Tắt máy"
            font_size: 32
            size: 100, 50
            size_hint: (1, None)
            height: 50
            on_press: root.shutdown()
        Button:
            id: tangal_btn
            text: "Tăng âm lương"
            font_size: 32
            size: 100, 50
            size_hint: (1, None)
            height: 50
            on_press: root.tangAmLuong()
        Button:
            id: giamal_btn
            text: "Giảm âm lượng"
            font_size: 32
            size: 100, 50
            size_hint: (1, None)
            height: 50
            on_press: root.giamAmLuong()
        Label:
            id: volume_text
            text: "0"
            font_size: 36
        GridLayout:
            rows: 3
            cols: 3
            Button:
                text: "Chuột Trái"
                font_size: 14
                on_press: root.chuot_trai()
            Button:
                text: "Lên"
                font_size: 14
                on_press: root.len()
            Button:
                text: "Chuột Phải"
                font_size: 14
                on_press: root.chuot_phai()
            Button:
                text: "Trái"
                font_size: 14
                on_press: root.trai()
            Label:
                text: ""
                font_size: 14
            Button:
                text: "Phải"
                font_size: 14
                on_press: root.phai()
            Label:
                text: ""
                font_size: 14
            Button:
                text: "Xuống"
                font_size: 14
                on_press: root.xuong()
            Label:
                text: ""
                font_size: 14

0
投票

您可以在

on_start
类中使用
App
方法在启动时调用任何函数,如下所示:

class style(App):
    def build(self):
        return MyRoot()
    
    def on_start(self):
        self.random_method1()
        self.random_method2()
        print("Start-up completed!")

0
投票

当您说进入和离开时,您是指鼠标进入/离开对象时吗?例如,当鼠标进入按钮时,执行某些操作,当鼠标离开时,执行其他操作?如果是这样,您需要导入悬停行为,并创建一个自定义按钮:

from kivy.app import App
from kivy.lang.builder import Builder
from kivy.properties import StringProperty, NumericProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivymd.uix.behaviors import HoverBehavior
from socket import socket, AF_INET, SOCK_STREAM
from time import sleep

Builder.load_file('hung_nguyen.kv')
client = socket(AF_INET, SOCK_STREAM)

class CustomButton(Button, HoverBehavior):
def on_enter(self, *args):
    print ("Would do something")

def on_leave(self, *args):
    print ("Would do something else")

class MyRoot(BoxLayout):
    ip_text = StringProperty()
    nickname_text = StringProperty()
    connect_btn = StringProperty()
    connection_grid = StringProperty()
    shutdown_btn = StringProperty()
    tangal_btn = StringProperty()
    giamal_btn = StringProperty
    volume_text = StringProperty()

vol = NumericProperty()

def __init__(self):
    super(MyRoot, self).__init__()
    self.command = None
    pass

def shutdown(self):
    self.command = "shutdown"
    self.send_message()

def tangAmLuong(self):
    self.command = "tang_am_luong"
    self.send_message()
    if self.vol <= 98:
        self.vol += 2
        self.volume_text = str(self.vol)
    sleep(0.1)

def giamAmLuong(self):
    self.command = "giam_am_luong"
    self.send_message()
    if self.vol >= 2:
        self.vol -= 2
        self.volume_text = str(self.vol)
    sleep(0.1)

def trai(self):
    self.command = "sang_trai"
    self.send_message()

def phai(self):
    self.command = "sang_phai"
    self.send_message()

def len(self):
    self.command = "len"
    self.send_message()

def xuong(self):
    self.command = "xuong"
    self.send_message()

def chuot_trai(self):
    self.command = "chuot_trai"
    self.send_message()

def chuot_phai(self):
    self.command = "chuot_phai"
    self.send_message()

def on_ip_text(self, *args):
    print('on_ip_text', self.ip_text, args)
    pass

def on_nickname_text(self, *args):
    print('on_nickname_text', self.nickname_text)
    pass

def on_connect_btn(self, *args):
    print('on_connect_btn', self.connect_btn)
    pass

def on_connection_grid(self, *args):
    print('on_connection_grid', self.connection_grid)
    pass

def on_shutdown_btn(self, *args):
    print('on_shutdown_btn', self.shutdown_btn)
    pass

def on_tangal_btn(self, *args):
    print('on_tangal_btn', self.tangal_btn)
    pass

def on_giamal_btn(self, *args):
    print('on_giamal_btn', self.giamal_btn)
    pass

def on_volume_text(self, *args):
    print('on_volume_text', self.volume_text)
    pass

def on_vol(self, *args):
    print('on_vol', self.vol)
    pass

def send_message(self):
    try:
        client.send(f"{self.nickname_text}: {self.command}".encode("utf-8"))
    except OSError as os_err:
        print(os_err)

def connect_to_server(self):
    if self.nickname_text != "":
        client.connect((self.ip_text.text, 9999))
        self.send_message()


class style(App):
    def build(self):
        return MyRoot()


if __name__ == '__main__':
    style().run()

然后在.kv文件中,将Button的所有实例更改为CustomButton:

<MyRoot>:
ip_text: root.ip_text
nickname_text: root.nickname_text
connect_btn: root.connect_btn
connection_grid: root.connection_grid
shutdown_btn: root.shutdown_btn
tangal_btn: root.tangal_btn
giamal_btn: root.giamal_btn
volume_text: root.volume_text
BoxLayout:
    orientation: 'vertical'
    GridLayout:
        id: connection_grid
        rows: 1
        cols: 2
        padding: 10
        spacing: 10
        height: 125
        size_hint: (1, None)
        BoxLayout:
            orientation: 'vertical'
            Label:
                text: "Server IP"
                font_size: 42
                color: 0.92, 0.45, 0, 1
            TextInput:
                id: ip_text
                size_hint: (1, None)
                height: 50
                font_size: 36
        BoxLayout:
            orientation: 'vertical'
            Label:
                text: "Nickname"
                font_size: 42
                color: 0.92, 0.45, 0, 1
            TextInput:
                id: nickname_text
                size_hint: (1, None)
                height: 50
                font_size: 36
    CustomButton:
        id: connect_btn
        text: "Connect"
        font_size: 32
        size: 100, 50
        size_hint: (1, None)
        height: 50
        on_press: root.connect_to_server()
    CustomButton:
        id: shutdown_btn
        text: "Tắt máy"
        font_size: 32
        size: 100, 50
        size_hint: (1, None)
        height: 50
        on_press: root.shutdown()
    CustomButton:
        id: tangal_btn
        text: "Tăng âm lương"
        font_size: 32
        size: 100, 50
        size_hint: (1, None)
        height: 50
        on_press: root.tangAmLuong()
    CustomButton:
        id: giamal_btn
        text: "Giảm âm lượng"
        font_size: 32
        size: 100, 50
        size_hint: (1, None)
        height: 50
        on_press: root.giamAmLuong()
    Label:
        id: volume_text
        text: "0"
        font_size: 36
    GridLayout:
        rows: 3
        cols: 3
        CustomButton:
            text: "Chuột Trái"
            font_size: 14
            on_press: root.chuot_trai()
        CustomButton:
            text: "Lên"
            font_size: 14
            on_press: root.len()
        CustomButton:
            text: "Chuột Phải"
            font_size: 14
            on_press: root.chuot_phai()
        CustomButton:
            text: "Trái"
            font_size: 14
            on_press: root.trai()
        Label:
            text: ""
            font_size: 14
        CustomButton:
            text: "Phải"
            font_size: 14
            on_press: root.phai()
        Label:
            text: ""
            font_size: 14
        CustomButton:
            text: "Xuống"
            font_size: 14
            on_press: root.xuong()
        Label:
            text: ""
            font_size: 14
© www.soinside.com 2019 - 2024. All rights reserved.