Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

postgresql - Creatings tabs programatically by calling the info from database. How to avoid recurrence problem? Python - Kivy

In my code down below. When I click the second tab the code checks the database, if the database has tables, it takes the names of the tables and create tabs within a predefined tab in kv file. I use database because the tabs disappear after I reload the app. In other words, I don't know how to save and I do not want it to save in the app memory. My aim later on is to fill the created tabs with the database information I called. But for this time I need the avoid the recurrence problem which is: when I click the second tab as I mentioned, it creates the database, if I leave the tab, and reopen again, it adds the tabs that are recalled before. I tried to solve it by looking for giving ids to the tabs and stop recurrence by stopping the addition by checking the names with their ids. However, I could not manage it.

class MyLayout(TabbedPanel):
# Meta tablosundaki sat?rlar? tab olarak ?a??r
def tabcreator(self, *args):

    try:
        print("data ?ekiliyor")
            # cur.execute("CREATE TABLE student(id SERIAL PRIMARY KEY, name VARCHAR);")
            # cur.execute("INSERT INTO student (name) VALUES(%s)",("Cristina",))
            # metalar sütunundan rowlar? ?eker
        cur.execute("""SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'""")
        # fetchall bütün rowlard?r
        meta_rows = cur.fetchall()
        # meta_rows - tuple olarak rowlar? ?eker - e?er rowlar?n say?s? 0dan büyükse ?ekme i?lemi ba?lar
        if len(meta_rows) > 0:
            # tupledan rowlar? ?eker, her ?eki?te bir tab a?ar ve ekler
            for rows in meta_rows:
                meta_panel = self.ids.meta_panel
                tabnew = TabbedPanelHeader(text=str(rows[0]).upper())
                meta_panel.add_widget(tabnew)
                print(self.tab_list)
    except psycopg2.ProgrammingError as e:
        print("hata kodu i?lendi" + str(e))
        return
    except psycopg2.InternalError as b:
        print("hata kodu 2"+ str(b))
        return

# tablo ismi olu?turunca metalar i?inde bir row a??l?r bu databaseden ?ektikten sonra olur.
def tablecreator(self, table, *args):
    # CREATE DATABASE and TAB
    cur.execute('CREATE TABLE '+table+'(id SERIAL PRIMARY KEY, name VARCHAR);')
    meta_panel = self.ids.meta_panel
    tabnew = TabbedPanelHeader(text=table)
    meta_panel.add_widget(tabnew)
    return

kivy

#:import Factory kivy.factory.Factory
<MyPopup@Popup>
    # pop up kendine sadece 1 tane widget al?r. Birka? ?ey koymak istiyorsan. Boxlayout falan yap?p i?ine
    #atman gerekiyor.
    # kutu d???na dokununca kaybolsun mu? - hay?r
    auto_dismiss: False
    size_hint: .4, .4
    #top .9 = yukar?n?n %90'n?nda dursun diye. x: saga sola
    pos_hint: {"center_x": 0.5, "top": .8}
    title: "Herhangi bir veri yok. Veri girin"
    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        Label:
            text: "Herhangi bir veri yok. Veri girin"
            text: "Meta girin"
            size_hint: 1, .2

        TextInput:
            id: table_input
            text: "Metan?z"
            size_hint: 1, .2
            font_size: 20
        BoxLayout:
            orientation: "horizontal"
            size: root.width, root.height
            size_hint: 1, .2

            Button:
                text: "Olu?tur"
                font_size: 20
                id: create_db
                on_release:app.root.add_data(table_input.text)
                on_press: root.dismiss()

            Button:
                id: buttontx
                text: "Vazge?"
                font_size: 20
                on_release: app.root.switch_to(app.root.ids.delft)
                on_press: root.dismiss()



<MyLayout@TabbedPanel>
    do_default_tab: True
    default_tab: delft
    #orant?sal kü?ültür
    size_hint: .99, .99
    #ekran?n ortas?na bütün kareyi koyar
    pos_hint: {'center_x': .5, 'center_y': .5}
    # tablar? ortalar - bottom tablar? alta atar left_top, left_mid, left_bottom, top_left, top_mid, top_right,
    #right_top, right_mid, right_bottom, bottom_left, bottom_mid, bottom_right
    tab_pos: 'top_mid'

    TabbedPanelItem:
        id: delft
        text: "Anasayfa"

        BoxLayout:
            Label:
                id: saat
                text: "Press the Button =>"
            Label:
                id: tarih
                text: "Press the Button =>"

    TabbedPanelItem:
        text: "Metalar"
        on_press: app.root.tabcreator()
        on_release:
            if len(meta_panel.tab_list)< 2 : Factory.MyPopup().open()

        TabbedPanel:
            id: meta_panel
            do_default_tab: False
            #orant?sal kü?ültür
            size_hint: .99, .99
            #ekran?n ortas?na bütün kareyi koyar
            pos_hint: {'center_x': .5, 'center_y': .5}
            # tablar? ortalar - bottom tablar? alta atar left_top, left_mid, left_bottom, top_left, top_mid, top_right,
            #right_top, right_mid, right_bottom, bottom_left, bottom_mid, bottom_right
            tab_pos: 'left_mid'


            TabbedPanelItem:
                text: "ADD"
                id: add_data
                on_press: Factory.MyPopup().open()

                BoxLayout:
                    Label:
                        id: ekle
                        text: "deneme"


    TabbedPanelItem:
        text: "Tab 3"
question from:https://stackoverflow.com/questions/65864285/creatings-tabs-programatically-by-calling-the-info-from-database-how-to-avoid-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...