问题描述
在我下面的代码中。当我单击第二个选项卡时,代码会检查数据库,如果数据库有表,它会获取表的名称并在 kv 文件的预定义选项卡中创建选项卡。我使用数据库是因为在我重新加载应用程序后选项卡消失了。换句话说,我不知道如何保存,我不希望它保存在应用程序内存中。我稍后的目标是用我调用的数据库信息填充创建的选项卡。但是这次我需要避免重复出现的问题,即:当我点击我提到的第二个选项卡时,它会创建数据库,如果我离开该选项卡,然后再次重新打开,它会添加之前调用的选项卡。我试图通过为选项卡提供 id 来解决它,并通过检查名称及其 id 来停止添加来停止重复。但是,我无法管理它。
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
基维
#: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,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"
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)