TabbedPanel的事件处理程序

时间:2015-02-10 12:33:37

标签: python events kivy tabbed

我无法找到有关每个元素的事件处理程序的信息(如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
=====

1 个答案:

答案 0 :(得分: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()。

相关问题