输入直到离开kivy时如何调用函数?

Posted

技术标签:

【中文标题】输入直到离开kivy时如何调用函数?【英文标题】:How to call a function when entered until leaved kivy? 【发布时间】:2021-12-11 23:53:49 【问题描述】:

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

kivy.require("1.9.0")

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

类 MyRoot(BoxLayout): def 初始化(自我): 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()

类风格(App):

def build(self):
    return MyRoot()

webChat = 风格() webChat.run()

基维文件:

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

【问题讨论】:

【参考方案1】:

我稍微修改了您的代码,希望对您有所帮助。

请看: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

【讨论】:

【参考方案2】:

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

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

【讨论】:

以上是关于输入直到离开kivy时如何调用函数?的主要内容,如果未能解决你的问题,请参考以下文章

python - kivy:从另一个类调用函数

使用 Kivy 的 Python 跨类函数调用

将文本从文本输入传递到 Kivy 中的标签

(Kivy) 从 App 类调用函数 - 函数对象没有属性

为啥我的 kivy 程序没有从另一个类调用函数?

Kivy 在另一个类中调用函数