我的回答并不适用于标签

时间:2017-08-24 01:00:21

标签: python kivy

每个组件都运行良好但遗憾的是存在问题。当第一个TextInput和第二个TextInput不为空且同时按下(编码 / 解码)按钮时,按钮不会显示回答标签。我做错了什么?

这是我的 main.py 文件:

from kivy.app import App
from kivy.lang import Builder

from kivy.uix.boxlayout import BoxLayout 

from cipher import Cod as c

class mother_layout(BoxLayout):
      def encode(self,*args):
            try:
                  self._lbl=self.ids['ans']
                  self._txt=self.ids['tt']
                  self._key=self.ids['tn']
                  self._lbl.bind(text=c().Encode_en(KeyNumber=int(self._key.text),Text=str(self._txt.text)))
            except:
                  self._lbl.text='bye bye'

      def decode(self,*args):
            try:
                  self._lbl=self.ids['ans']
                  self._txt=self.ids['tt']
                  self._key=self.ids['tn']
                  self._lbl.bind(text=c().Decode_en(KeyNumber=int(self._key.text),Text=str(self._txt.text)))
            except:
                  self._lbl.text='bye bye'


Builder.load_file('main.kv')

class myApp(App):
      title='cesar cipher'
      def build(self):
            self.use_default_settings=False
            m=mother_layout()
            return m

myApp().run()

这是我的 main.kv 文件:

<mother_layout>:
    orientation:'vertical'
    BoxLayout:
        orientation:'vertical'
        id:os
        size_hint_y:None
        height:root.height*0.4
        BoxLayout:
            orientation:'horizontal'
            size_hint_y:None
            height:os.height*.5
            spacing:10
            Label:
                size_hint_x:.25
                text:'Key of Encode/Decode ==>'
                font_size:min(self.height,self.width)*0.09
            TextInput:
                size_hint_x:.25
                number:True
                id:tn
                font_size:max(self.height,self.width)*0.1
            Label:
                size_hint_x:.25
                text:'your text'
                font_size:max(self.height,self.width)*0.1
            TextInput:
                size_hint_x:.25
                id:tt
                font_size:max(self.height,self.width)*0.1
        BoxLayout:
            size_hint_y:None
            height:os.height*.5
            orientation:'horizontal'
            spacing:10
            Button:
                size_hint_x:.5
                text:'encode'
                font_size:max(self.height,self.width)*0.05
                on_press:root.encode()
            Button:
                size_hint_x:.5
                text:'decode'
                font_size:max(self.height,self.width)*0.05
                on_press:root.decode()
    ScrollView:
        size_hint_y:None
        height:root.height*0.6
        Label:
            id:ans
            text_size:self.width,None
            size_hint_y:None
            height:self.texture_size[1]
            text:'here will place your encoded/decoded text\n4\n5\n'*50
            color:0,0,0,1
            canvas.before:
                Color:
                    rgba:1,1,1,0.7
                Rectangle:
                    size:ans.size
                    pos:ans.pos

1 个答案:

答案 0 :(得分:0)

您使用的是bind而不只是设置标签文本

#in encode
 self._lbl.bind(text=c().Encode_en(KeyNumber=int(self._key.text),Text=str(self._txt.text)))
#should be
self._lbl.text = c().Encode_en(KeyNumber=int(self._key.text),Text=str(self._txt.text)))

#do the same for decode...