python 如何检查TextInput中的文本以便禁用其他TextInput

6yoyoihd  于 2023-02-28  发布在  Python
关注(0)|答案(1)|浏览(109)

我正在开发一个计算电气参数的应用程序。这是一个简单的项目,但这是我的第一个项目。
我使用Kivy库是因为我觉得它很容易上手。我想检查textInput窗口中的条目,所以如果我输入了2个值,程序会禁用可以基于输入值求值的字段。
不要介意变量、方法和ID。我不是英语母语者。
我的视图编码如下:
.kv文件:

#:kivy 2.1.0
<WarningPopup@Popup>:
    message: message
    auto_dismiss: True
    title: "Aviso"
    size_hint: None, None
    width: grid.width + dp(25)
    height: grid.height + root.title_size + dp(48)
    
    GridLayout:
        id: grid
        size_hint: None, None
        size: self.minimum_size
        padding: [10, 5]
        cols: 1
        AnchorLayout:
            anchor_x: "center"
            anchor_y: "bottom"
            size_hint: None, None
            height: message.height
            width: max(message.width, butt.width)
            Label: 
                id: message
                size_hint: None, None
                size: self.texture_size
                padding: [10, 5]
        AnchorLayout:
            anchor_x: "center"
            anchor_y: "bottom"
            size_hint: None, None
            height: butt.height
            width: max(message.width, butt.width)
            Button:
                id: butt
                text: 'Fehcar'
                size_hint: None, None
                size: self.texture_size
                padding: [10, 5]
                on_release: root.dismiss()


<ParamCalcWindow>:
    name: 'Calculadora'
    canvas:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        orientation: 'vertical'
        size_hint: 0.95, 0.95
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
    
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.09
            
            ToggleButton:
                id: trifasico
                text: 'Trifásico'
                group: 'fases'
                state: 'normal'
                on_press: root.check_toggles()

            ToggleButton:
                id: monofasico
                text: 'Monofásico'
                group: 'fases'
                state: 'down'
                on_press: root.check_toggles()

        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.09

                
            TextInput:
                id: PotenciaAparente
                hint_text: "Potência Aparente [VA]"
                text: ""
                halign: 'center'
                padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
                input_filter: 'float'
                multiline: False
            
            
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.09

            TextInput:
                id: PotenciaAtiva
                hint_text: "Potência Ativa [W]"
                text: ""
                halign: 'center'
                padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
                input_filter: 'float'
                multiline: False
                

        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.09

            TextInput:
                id: PotenciaReativa
                hint_text: "Potência Reativa [VAr]"
                text: ""
                halign: 'center'
                padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
                input_filter: 'float'
                multiline: False

        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.09

            TextInput:
                id: cosphi
                hint_text: "cos \u03C6"
                text: ""
                halign: 'center'
                padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
                input_filter: 'float'
                multiline: False
            
            TextInput:
                id: phi
                hint_text: "\u00B1\u03C6°"
                text: ""
                halign: 'center'
                padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
                input_filter: 'float'
                multiline: False

            TextInput:
                id: Reatancia
                hint_text: "Indutivo Capacitivo"
                text: ""
                halign: 'center'
                padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
                input_filter: 'float'
                
        
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.09

            TextInput:
                id: Tensao
                hint_text: "Tensão [V]"
                text: ""
                halign: 'center'
                padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]

        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.09

            TextInput:
                id: Corrente
                hint_text: "Corrente [A]"
                text: ""
                halign: 'center'
                padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]

        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.01
            Label:
                text: ""
                size_hint: 1, 0.01

        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, 0.09

            Button:
                id: Limpar
                text: "Limpar"
                size_hint: 0.4, 1
                on_release: root.clear_all()

            Label:
                text: ""
                size_hint: 0.2, 1

            Button:
                id: Calcular
                text: "Calcular"
                size_hint: 0.4, 1
                on_release: root.get_entries()

查看文件:

from kivy.uix.screenmanager import Screen
from kivy.graphics import Color, Rectangle
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup

class WarningPopup(Popup):
    def __init__(self, **kwargs):
        super(WarningPopup, self).__init__(**kwargs)
        with self.canvas.before:
            Color(0, 0, 0, 0)
            self.rect = Rectangle(size=self.size, pos=self.pos)
        self.bind(size=self._update_rect, pos=self._update_rect)

    def _update_rect(self, instance, value):
        self.rect.pos = instance.pos
        self.rect.size = instance.size

class ParamCalcWindow(Screen):

    toggles_previous_state = "normaldown"

    def __init__(self, **kw):
        super(ParamCalcWindow, self).__init__(**kw)

    def clear_entry(self, instance: TextInput):
        instance.text = ""
        pass

    def clear_all(self):
        self.ids.PotenciaAparente.text = ""
        self.ids.PotenciaAtiva.text = ""
        self.ids.PotenciaReativa.text = ""
        self.ids.cosphi.text = ""
        self.ids.phi.text = ""
        self.ids.Reatancia.text = ""
        self.ids.Tensao.text = ""
        self.ids.Corrente.text = ""
        pass

    def get_entries(self):
        return [
            float(self.ids.PotenciaAparente.text)
            if self.ids.PotenciaAparente.text
            else None,
            float(self.ids.PotenciaAtiva.text) if self.ids.PotenciaAtiva.text else None,
            float(self.ids.PotenciaReativa.text)
            if self.ids.PotenciaReativa.text
            else None,
            self.filter_TextInput(self.ids.cosphi, lower_limit=0, upper_limit=1),
            self.filter_TextInput(self.ids.phi, lower_limit=-90, upper_limit=90),
            self.ids.Reatancia.text if self.ids.Reatancia.text else None,
            float(self.ids.Tensao.text) if self.ids.Tensao.text else None,
            float(self.ids.Corrente.text) if self.ids.Corrente.text else None,
        ]

    def filter_TextInput(
        self, instance: TextInput, lower_limit: float, upper_limit: float
    ) -> float | None:
        text = instance.text

        if not text:
            return None
        value = float(text)
        if lower_limit <= value <= upper_limit:
            return instance
        warning = WarningPopup()
        warning.message.text = instance.hint_text + " fora do intervalo permitido"
        self.clear_entry(instance)
        warning.open()

    def get_mode(self):
        return [self.ids.trifasico.state, self.ids.monofasico.state]

    def check_toggles(self):

        if self.ids.trifasico.state == self.ids.monofasico.state:
            if self.toggles_previous_state == "normaldown":
                self.ids.trifasico.state = "down"
                self.ids.monofasico.satte = "normal"
            else:
                self.ids.trifasico.state = "normal"
                self.ids.monofasico.state = "down"

        self.toggles_previous_state = (
            self.ids.trifasico.state + self.ids.monofasico.state
        )

# tests
class ParamCalcApp(App):
    def build(self):
        return ParamCalcWindow()

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

如果有任何形式上的奇怪,这是自动形成的错误哈哈。
给定代码,输出为

我想让程序识别出我输入了一些参数,这样它就可以禁用将要进行如下求值的字段:

amrnrhlw

amrnrhlw1#

kv文件中,在感兴趣的TextInput下,可以添加:

on_text: app.text_changed(self, self.text)

并且,在app类中添加一个方法:

def text_changed(self, instance, text):

这将在TextInputtext发生更改时触发text_changed()的执行(实际上,在每次更改时)。
你可以用on_text_validate:代替on_text:,这样只会在用户点击Enter的时候触发这个方法。

相关问题