0

I can't find info about event handlers for each element (like Label,Button,TabbedPanel, etc)

I have a problem with default showing first tab in TabbedPanel.

When programme is started it shows first tab "tab_def" and it's empty. But I want to see text which get through SHOW_DEF(). If I click another tab and after that click first tab - I get what I want.

How can I fix it? Thanks in advance.

.kv code

<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 code

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   <======= !!!

FULL code of .py file:

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()

FULL code of .kv file:

<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'

"data" file:

=====
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 Answers1

0

Have you tried to to call SHOW_DEF() in your class constructor?

@EDIT: Try this

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

What I've done here is I added a constructor, which tells your object what to do once it's created. You shouldn't do anything outside a specified function or constructor in a class like you did, so I've also moved your variables to the constructor and called SHOW_DEF() at the end.

Nhor
  • 3,860
  • 6
  • 28
  • 41
  • Pardon me, I'm new in python, so I don't understand what you mean. Can you explain or show a small example? – Antony Makaruk Feb 11 '15 at 07:39
  • If I do it, I have another problem. When I edit some info in some tab and go to the first tab I see - text isn't updated. It remains in the state in which was at startup. – Antony Makaruk Feb 11 '15 at 14:43
  • Could you send whole your code so I can run it on my PC? – Nhor Feb 11 '15 at 15:20