在 Popup 的 .kv 中使用 switch_to 使用工厂打开 .kv 中的另一个选项卡!我做不到

Using switch_to within .kv from Popup using factory to open another tab within the .kv! I could not achieve it

我的第一个目标 是使用工厂函数打开选项卡 id:delft从 .kv 文件上的按钮单击弹出部分。(按钮 ID:buttontx)当我单击按钮时,我收到此错误: on_press: Factory.MyLayout().switch_to(MyLayout.ids.delft) NameError: name 'MyLayout' is not defined.

我的第二个目标是从弹出窗口.kv 创建数据库并在选项卡中显示名称。但是我无法启动它,因为我正在寻找解决我的主要问题的方法。在下方您可能会看到我的 .py 和 .kv 代码。

.PY

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.tabbedpanel import TabbedPanel
import time
import smtplib
import psycopg2

Builder.load_file('tabs.kv')


def sendmail():
    username = 'kASASD'
    subject = "SAAT 11"
    password = 'ddsa'
    body = 'DENEME'
    msg = "\r\n".join([
        "From: " + username,
        "To: " + username,
        "Subject: " + subject,
        "",
        body
    ])
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.ehlo()
    server.starttls()
    server.login(username, password)
    print('sending')
    server.sendmail(username, username, msg.encode("utf-8", errors="ignore"))
    server.quit()
    print('sent')


class MyLayout(TabbedPanel):
    def updates(self, *args):
        # saat, dakika saniye
        self.ids.saat.text = time.strftime("%H:%M:%S")
        # gün ay yıl gün
        self.ids.tarih.text = time.strftime("%d %B %Y %A")
        # 23de mail at
        if time.strftime("%H:%M:%S") == "23:03:00":
            sendmail()



class AwesomeApp(App):

    def build(self):
        t = MyLayout()
        Clock.schedule_interval(t.updates, 1)
        return t



if __name__ == '__main__':
    AwesomeApp().run()

#: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

            Button:
                id: buttontx
                text: "Vazgeç"
                font_size: 20
                on_press: Factory.MyLayout().switch_to(MyLayout.ids.delft)

<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_release:
            if inner_meta.text == "Empty" : Factory.MyPopup().open()

        TabbedPanel:
            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:
                id: inner_meta
                text:"Empty"



    TabbedPanelItem:
        text: "Tab 3"

        Image:
            source: "/charac.png"

我认为不需要使用 Factory 打开标签页。只需更改:

            on_press: Factory.MyLayout().switch_to(MyLayout.ids.delft)

至:

            on_press: app.root.switch_to(app.root.ids.delft)

代码 Factory.MyLayout() 正在创建 MyLayout 的新实例(不是在您的 build() 方法中创建的实例)。而且这个新实例不是您的 GUI 的一部分。代码 MyLayout.ids.delft 导致错误,因为 kivy 试图将 MyLayout 作为变量求值,通常包含对某个小部件实例的引用。即使 kivy 将 MyLayout 识别为 class,它也不会包含 kv 文件中定义的 ids,因为它只出现在 MyLayout 的实例中.该修复程序使用 app.root 访问 MyLayout 的实例,该实例是 App.

的根小部件