0

各要素 (Label、Button、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
=====
4

1 に答える 1

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() を呼び出しました。

于 2015-02-11T00:08:10.947 に答える