2015-02-10 161 views
0

我無法找到有關每個元素的事件處理程序的信息(如標籤,按鈕,TabbedPanel等)事件處理程序TabbedPanel

我有一個默認顯示TabbedPanel第一個標籤的問題。

程序啓動時,它顯示第一個選項卡「tab_def」,它是空的。 但我想看到通過SHOW_DEF()獲得的文本。 如果我點擊另一個選項卡,然後點擊第一個選項卡 - 我得到我想要的。

我該如何解決? 在此先感謝。

.kv代碼

<GeneralForm>: 
    do_default_tab: False 
    txt1:txt1 
    txt2:txt2 
    txt3:txt3 
    txt_def:txt_def 
    tab1:tab1 
    tab_def:tab_def 

    TabbedPanelItem: 
     id:tab_def 
     text: 'Today' 
     on_release:root.SHOW_DEF() 
     BoxLayout: 
      ScrollView: 
       Label: 
        id:txt_def 
        text:''  <=== !!!! 
        text_size: self.width, None 
        height: self.texture_size[1] 
        size_hint_y: None 
        halign: 'center' 
    TabbedPanelItem: 
     id:tab1 
     text: 'Mon' 
     on_release: root.SHOW_CONTENT(tab1.text,'txt1') 
     BoxLayout: 
      orientation: 'vertical' 
      BoxLayout: 
       ScrollView: 
        Label: 
         id:txt1 
         text: '' 
         text_size: self.width, None 
         height: self.texture_size[1] 
         size_hint_y: None 
         halign: 'center' 
      BoxLayout: 
       Button: 
        text: 'Edit' 
        on_press: root.EDIT(tab1.text) 
       Button: 
        text: 'Exit' 
        on_press: root.EXIT() 

的.py代碼

class GeneralForm(TabbedPanel): 
    shutil.copy('data','data_backup') 
    txt1 = ObjectProperty() 
    txt2 = ObjectProperty() 
    txt_def = ObjectProperty() 


    def SHOW_DEF(self): 
     now_date = datetime.date.today() 
     TIME=now_date.weekday() 
     if TIME==0: 
      DDAY='Mon' 
     elif TIME==1: 
      DDAY='Tue' 
     elif TIME==2: 
      DDAY='Wed' 
     elif TIME==3: 
      DDAY='Thu' 
     elif TIME==4: 
      DDAY='Fri' 
     elif TIME==5: 
      DDAY='Sat' 
     elif TIME==6: 
      DDAY='Sun' 

     DATA=GeneralForm.PARSE(self,DDAY) 
     DATA=DATA.split('|||') 
     DATA='\n'.join(DATA) 

     self.txt_def.text=DATA <======= !!! 

.py文件的全碼:

import shutil, datetime 
from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.properties import ObjectProperty 
from kivy.uix.tabbedpanel import TabbedPanel 
from kivy.uix.label import Label 
from kivy.uix.popup import Popup 
from kivy.uix.button import Button 
from kivy.uix.scrollview import ScrollView 
from kivy.uix.textinput import TextInput 
############################################################################### 

class GeneralForm(TabbedPanel): 
    shutil.copy('data','data_backup') 
    txt1 = ObjectProperty() 
    txt2 = ObjectProperty() 
    txt3 = ObjectProperty() 
    txt4 = ObjectProperty() 
    txt5 = ObjectProperty() 
    txt6 = ObjectProperty() 
    txt7 = ObjectProperty() 
    txt_def = ObjectProperty() 


    def SHOW_DEF(self): 
     now_date = datetime.date.today() 
     TIME=now_date.weekday() 
     if TIME==0: 
      DDAY='Mon' 
     elif TIME==1: 
      DDAY='Tue' 
     elif TIME==2: 
      DDAY='Wed' 
     elif TIME==3: 
      DDAY='Thu' 
     elif TIME==4: 
      DDAY='Fri' 
     elif TIME==5: 
      DDAY='Sat' 
     elif TIME==6: 
      DDAY='Sun' 

     DATA=GeneralForm.PARSE(self,DDAY) 
     DATA=DATA.split('|||') 
     DATA='\n'.join(DATA) 

     #box1=ScrollView() 
     #l1=Label(text=DATA,text_size=(self.width,None),halign='center',size_hint_y=None) 
     #box1.add_widget(l1) 
     #return box1 

     self.txt_def.text=DATA 


    def SHOW_CONTENT(self,D,TXT): 
     DATA=GeneralForm.PARSE(self,D) 
     DATA=DATA.split('|||') 
     DATA='\n'.join(DATA) 

     if TXT=='txt1': 
      self.txt1.text=DATA 
     elif TXT=='txt2': 
      self.txt2.text=DATA 
     elif TXT=='txt3': 
      self.txt3.text=DATA 
     elif TXT=='txt4': 
      self.txt4.text=DATA 
     elif TXT=='txt5': 
      self.txt5.text=DATA 
     elif TXT=='txt6': 
      self.txt6.text=DATA 
     elif TXT=='txt7': 
      self.txt7.text=DATA 

    def PARSE(self,D): 
     FILE=open('data') 
     RAW=FILE.read() 
     for i in RAW.split('====='): 
      i=i.strip() 

      if D in i: 
       DATA=i.splitlines() 
       DATA.remove(D) 
       DATA='\n'.join(DATA) 
       return(DATA) 


    def EDIT(self,D): 
     DATA=GeneralForm.PARSE(self,D) 
     DATA=DATA.split('|||') 
     DATA='\n'.join(DATA) 


     box1=BoxLayout(size_hint_y=6) 
     t1=TextInput(text=DATA) 
     box1.add_widget(t1) 

     box2=BoxLayout(size_hint_y=1) 
     b2=Button(text='Save') 
     b3=Button(text='Cancel') 
     box2.add_widget(b2) 
     box2.add_widget(b3) 

     box3=BoxLayout(orientation='vertical') 
     box3.add_widget(box1) 
     box3.add_widget(box2) 

     popup = Popup(content=box3,auto_dismiss=False,size_hint=(.75,.75),title='Edit') 
     b2.bind(on_press=lambda instance: self.SAVE_EDIT(instance, TXT=t1.text, DAY=D)) 
     b3.bind(on_press=popup.dismiss) 
     popup.open() 


    def SAVE_EDIT(self,instance,TXT,DAY): 
     TXT=TXT.strip() 
     if TXT=='': 
      pass 
     else: 
      TXT=TXT.split('\n') 
      TXT='|||'.join(TXT) 
      TMP_FILE=open('temp','w') 
      DATA=GeneralForm.PARSE(self,DAY) 
      for line in open('data'): 
       line=line.replace(DATA,TXT) 
       TMP_FILE.write(line) 
      TMP_FILE.close() 
      shutil.move('temp','data') 


    def EXIT(self): 
     App.get_running_app().stop() 


class TimeTable(App): 
    def build(self): 
     return GeneralForm() 

if __name__ == '__main__': 
    TimeTable().run() 
.kv文件的

全碼:

<GeneralForm>: 
    do_default_tab: False 
    #default_tab_text: 'Today' 
    #default_tab_content: root.SHOW_DEF() 

    txt1:txt1 
    txt2:txt2 
    txt3:txt3 
    txt4:txt4 
    txt5:txt5 
    txt6:txt6 
    txt7:txt7 
    txt_def:txt_def 
    tab1:tab1 
    tab_def:tab_def 

    TabbedPanelItem: 
     id:tab_def 
     text: 'Today' 
     on_release:root.SHOW_DEF() 
     BoxLayout: 
      ScrollView: 
       Label: 
        id:txt_def 
        text:'' 
        text_size: self.width, None 
        height: self.texture_size[1] 
        size_hint_y: None 
        halign: 'center' 
    TabbedPanelItem: 
     id:tab1 
     text: 'Mon' 
     on_release: root.SHOW_CONTENT(tab1.text,'txt1') 
     BoxLayout: 
      orientation: 'vertical' 
      BoxLayout: 
       size_hint_y:6 
       ScrollView: 
        Label: 
         id:txt1 
         text: '' 
         text_size: self.width, None 
         height: self.texture_size[1] 
         size_hint_y: None 
         halign: 'center' 
      BoxLayout: 
       size_hint_y:1 
       Button: 
        text: 'Edit' 
        on_press: root.EDIT(tab1.text) 
       Button: 
        text: 'Exit' 
        on_press: root.EXIT() 
    TabbedPanelItem: 
     id:tab2 
     text: 'Tue' 
     on_release: root.SHOW_CONTENT(tab2.text,'txt2') 
     BoxLayout: 
      orientation: 'vertical' 
      BoxLayout: 
       size_hint_y:6 
       ScrollView: 
        Label: 
         id:txt2 
         text: '' 
         text_size: self.width, None 
         height: self.texture_size[1] 
         size_hint_y: None 
         halign: 'center' 
      BoxLayout: 
       size_hint_y:1 
       Button: 
        text: 'Edit' 
        on_press: root.EDIT(tab2.text) 
       Button: 
        text: 'Exit' 
        on_press: root.EXIT() 
    TabbedPanelItem: 
     id:tab3 
     text: 'Wed' 
     on_release: root.SHOW_CONTENT(tab3.text,'txt3') 
     BoxLayout: 
      orientation: 'vertical' 
      BoxLayout: 
       size_hint_y:6 
       ScrollView: 
        Label: 
         id:txt3 
         text: '' 
         text_size: self.width, None 
         height: self.texture_size[1] 
         size_hint_y: None 
         halign: 'center' 
      BoxLayout: 
       size_hint_y:1 
       Button: 
        text: 'Edit' 
        on_press: root.EDIT(tab3.text) 
       Button: 
        text: 'Exit' 
        on_press: root.EXIT() 
    TabbedPanelItem: 
     id:tab4 
     text: 'Thu' 
     on_release: root.SHOW_CONTENT(tab4.text,'txt4') 
     BoxLayout: 
      orientation: 'vertical' 
      BoxLayout: 
       size_hint_y:6 
       ScrollView: 
        Label: 
         id:txt4 
         text: '' 
         text_size: self.width, None 
         height: self.texture_size[1] 
         size_hint_y: None 
         halign: 'center' 
      BoxLayout: 
       size_hint_y:1 
       Button: 
        text: 'Edit' 
        on_press: root.EDIT(tab4.text) 
       Button: 
        text: 'Exit' 
        on_press: root.EXIT() 
    TabbedPanelItem: 
     id:tab5 
     text: 'Fri' 
     on_release: root.SHOW_CONTENT(tab5.text,'txt5') 
     BoxLayout: 
      orientation: 'vertical' 
      BoxLayout: 
       size_hint_y:6 
       ScrollView: 
        Label: 
         id:txt5 
         text: '' 
         text_size: self.width, None 
         height: self.texture_size[1] 
         size_hint_y: None 
         halign: 'center' 
      BoxLayout: 
       size_hint_y:1 
       Button: 
        text: 'Edit' 
        on_press: root.EDIT(tab5.text) 
       Button: 
        text: 'Exit' 
        on_press: root.EXIT() 
    TabbedPanelItem: 
     id:tab6 
     text: 'Sat' 
     on_release: root.SHOW_CONTENT(tab6.text,'txt6') 
     BoxLayout: 
      orientation: 'vertical' 
      BoxLayout: 
       size_hint_y:6 
       ScrollView: 
        Label: 
         id:txt6 
         text: '' 
         text_size: self.width, None 
         height: self.texture_size[1] 
         size_hint_y: None 
         halign: 'center' 
      BoxLayout: 
       size_hint_y:1 
       Button: 
        text: 'Edit' 
        on_press: root.EDIT(tab6.text) 
       Button: 
        text: 'Exit' 
        on_press: root.EXIT() 
    TabbedPanelItem: 
     id:tab7 
     text: 'Sun' 
     on_release: root.SHOW_CONTENT(tab7.text,'txt7') 
     BoxLayout: 
      orientation: 'vertical' 
      BoxLayout: 
       size_hint_y:6 
       ScrollView: 
        Label: 
         id:txt7 
         text: '' 
         text_size: self.width, None 
         height: self.texture_size[1] 
         size_hint_y: None 
         halign: 'center' 
      BoxLayout: 
       size_hint_y:1 
       Button: 
        text: 'Edit' 
        on_press: root.EDIT(tab7.text) 
       Button: 
        text: 'Exit' 

「數據」 文件:

===== 
Mon 
1.00 - go 
===== 
Tue 
19.00 - go 
===== 
Wed 
20.00 - go 
===== 
Thu 
21.00 - go 
===== 
Fri 
22.00 - go 
===== 
Sat 
24.00 - go 
===== 
Sun 
25.00 - go 
===== 

回答

0

您曾經試圖打電話給SHOW_DEF()在類的構造函數?

@EDIT:試試這個

class GeneralForm(TabbedPanel): 
    def __init__(self, **kwargs): 
     super(GeneralForm, self).__init__(**kwargs) 
     shutil.copy('data','data_backup') 
     txt1 = ObjectProperty() 
     txt2 = ObjectProperty() 
     txt_def = ObjectProperty() 
     self.SHOW_DEF() 


    def SHOW_DEF(self): 
     now_date = datetime.date.today() 
     TIME=now_date.weekday() 
     if TIME==0: 
      DDAY='Mon' 
     elif TIME==1: 
      DDAY='Tue' 
     elif TIME==2: 
      DDAY='Wed' 
     elif TIME==3: 
      DDAY='Thu' 
     elif TIME==4: 
      DDAY='Fri' 
     elif TIME==5: 
      DDAY='Sat' 
     elif TIME==6: 
      DDAY='Sun' 

     DATA=GeneralForm.PARSE(self,DDAY) 
     DATA=DATA.split('|||') 
     DATA='\n'.join(DATA) 

     self.txt_def.text=DATA 

我所做的是我添加了一個構造函數,它告訴你的對象一旦創建它做什麼。你不應該在類中的指定函數或構造函數之外做任何事情,所以我也將你的變量移到了構造函數中,最後調用SHOW_DEF()。

+0

對不起,我是python新手,所以我不明白你的意思。你能解釋還是展示一個小例子? – 2015-02-11 07:39:49

+0

對不起,我剛剛用exmaple代碼編輯了答案:) – Nhor 2015-02-11 13:03:44

+0

如果我這樣做,我還有另一個問題。當我在某些選項卡中編輯某些信息並轉到第一個選項卡時,我看到 - 文本未更新。它仍處於啓動階段。 – 2015-02-11 14:43:40