2017-07-21 168 views
0

我有一段代碼。 (1)應該顯示TextInput值,但首先它不應該是可編輯的,點擊相應的CheckBox後,TextInput將是可編輯的。
(2)使用迭代,Label和TextInput應該得到該值。 Label和TextInput的值不應該被硬編碼(儘管它在我的代碼中,@FJSevilla幫助我完成這個)。
(3)但是,Label和TextInput的值以json格式存儲在變量中。 (你可以考慮像地圖中的鍵,值對)[變量='{「a」:「Goc」,「b」:「Coc」,「c」:「Dow」}'] (你可以請參閱圖表以獲得更多通關)。 我很感激幫助。如何在Python中使用kivy在TextInput中啓用/禁用編輯

from kivy.app import App 
from kivy.uix.tabbedpanel import TabbedPanel 
from kivy.lang import Builder 

Builder.load_string(""" 

<Test>: 
    do_default_tab: False 

    TabbedPanelItem: 
     text: 'page1' 

     BoxLayout: 
      padding: 50, 50, 50, 50 
      orientation: 'horizontal' 

      BoxLayout: 
       spacing: 50 
       orientation: 'vertical' 
       size_hint_x: 1 
       Label: 
        text: 'a' 
       Label: 
        text: 'b' 
       Label: 
        text: 'c' 

      BoxLayout: 
       spacing: 50 
       orientation: 'vertical' 
       TextInput: 
        text: 'Goc' 
       TextInput: 
        text: 'Coc' 
       TextInput: 
        text: 'Dow' 

      BoxLayout: 
       spacing: 50 
       orientation: 'vertical' 
       size_hint_x: 0.40 
       CheckBox: 
        text: 'CheckBox' 
       CheckBox: 
        text: 'CheckBox' 
       CheckBox: 
        text: 'CheckBox' 

      BoxLayout: 
       spacing: 50 
       orientation: 'vertical' 
       size_hint_x: 0.60 
       Button: 
        text: 'save' 
       Button: 
        text: 'save' 
       Button: 
        text: 'save' 


""") 

class Test(TabbedPanel): 
    pass 

class MyApp(App): 

    def build(self): 
     test = Test() 
     return test 


if __name__ == '__main__': 
    MyApp().run() 
所有的

kivy userInterface

回答

2

首先,感謝你提供的應用程序,它是很容易的工作。

我試圖實現你正在尋找的東西,除了JSON。我正在使用一個簡單的列表,它應該是直接擴展我的代碼爲JSON。

而不是使用colums,我使用行,這使得更容易將標籤textinput和複選框的屬性鏈接在一起。

enter image description here

from kivy.app import App 
from kivy.uix.tabbedpanel import TabbedPanel 
from kivy.uix.boxlayout import BoxLayout 
from kivy.properties import StringProperty 
from kivy.uix.textinput import TextInput 
from kivy.uix.checkbox import CheckBox 
from kivy.lang import Builder 

ROWS = ['Goc', 'COC', 'EEE'] 

Builder.load_string(""" 

<Test>: 
    do_default_tab: False 

    TabbedPanelItem: 
     text: 'page1' 

     Table: 
      padding: 50, 50, 50, 50 
      orientation: 'vertical' 

<Row>: 
    spacing: 50 
    #orientation: 'vertical' 
    size_hint_x: 1 
    txt: txtinpt.text 
    Label: 
     text: root.txt 
    TextInput: 
     id: txtinpt 
     text: root.txt 
     disabled: not CheckBox.active 
    CheckBox: 
     id:CheckBox 
     text: 'CheckBox' 
     active: False 
    Button: 
     text: 'save' 

""") 
class Table(BoxLayout): 
    def __init__(self, **kwargs): 
     super(Table, self).__init__(**kwargs) 
     for row in ROWS: 
      self.add_widget(Row(row)) 



class Row(BoxLayout): 
    txt = StringProperty() 
    def __init__(self, row, **kwargs): 
     super(Row, self).__init__(**kwargs) 
     self.txt = row 



class Test(TabbedPanel): 
    pass 

class MyApp(App): 

    def build(self): 
     test = Test() 
     return test 


if __name__ == '__main__': 
    MyApp().run() 
+2

您也可以在KV做到這一點沒有在複選框功能:on_active。像這樣:TextInput:disabled:not CheckBox.active – favcau

+0

@favcau不知道,謝謝。我相應地編輯了我的答案。 – PalimPalim

+0

親愛的@PalimPalim感謝您的時間。其實有些問題仍然存在。 (1)標籤和TextInput都正在編輯,但只需要編輯相應標籤的TextInput。 (2)編輯完成後,當我再次點擊一個CheckButton時,對應的TextInput將再次爲Non_Editable。和(3)應該在終端中顯示更改(包括更改的標籤和TextInput)。 – crazyDelight