簡體   English   中英

使用 screenmanager 在 Kivy 中更改嵌套在不同屏幕中的標簽

[英]Changing a label nested in a different screen in Kivy with screenmanager

我正在嘗試使用屏幕管理器更改嵌套在 Kivy 中不同屏幕中的標簽的簡單任務,但失敗了。

我想在“lbl_file_path”標簽上顯示“f_path”變量。 “lbl_file_path”標簽位於“W_MainMenu”屏幕上。 在“W_FileSelector”屏幕上創建“f_path”。

我怎樣才能做到這一點 ? 任何幫助是極大的贊賞。 您可以在下面找到代碼;

from logging import root
from charset_normalizer import from_path
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.properties import ObjectProperty

f_path = ""

class MyWinMan(ScreenManager):
    pass

class W_MainMenu(Screen):


    def transform_to_filechooser(self):

        Window.size = (700, 950)
        Window.top = 50
        Window.left = 100
        self.lbl_file_path.text = f_path
            

class W_FileSelector(Screen):

    def transform_to_main(self):

        Window.size = (700, 280)
        Window.top = 50
        Window.left = 100


    def selected(self, filename):

        try:
            print(filename[0])
            global f_path
            f_path = filename[0]
        except:
            pass


kv = Builder.load_string("""

MyWinMan:

    W_MainMenu:
    W_FileSelector:

<W_MainMenu>:
    
    lbl_file_path: lbl_file_path_k

    name: "win_Main"

    BoxLayout:

        orientation: "vertical"
        size: root.width, root.height
        padding: 40
        spacing: 10

        BoxLayout:

            orientation: "horizontal"
            size: root.width, root.height
            padding: 0
            spacing: 10

            Button:
                text:'Browse for Source Excel File'
                font_size: 20
                on_release: 
                    app.root.current = "win_FS"
                    root.manager.transition.direction = "up"
                    root.transform_to_filechooser()

            Image:
                source:""
                size_hint: ( 0.2, 1)

        Label:
            text:'Selected Excel File Path'
            size_hint: ( 1, 0.4)
            font_size: 18
            color: ( 180/255, 180/255, 180/255, 1)
            background_color: ( 50/255,50/255,50/255,1)
            canvas.before:
                Color:
                    rgba: self.background_color
                Rectangle:
                    pos: self.pos
                    size: self.size
        
        Label:
            text: "Initial Text"
            # text: f_path
            id: lbl_file_path_k
            size_hint: ( 1, 0.4)
            font_size: 18
            color: ( 50/255, 50/255, 50/255,1)
            background_color: ( 180/255, 180/255, 180/255, 1)
            canvas.before:
                Color:
                    rgba: self.background_color
                Rectangle:
                    pos: self.pos
                    size: self.size
            
<W_FileSelector>:
    
    name: "win_FS"
    id: my_widget

    BoxLayout:

        orientation: "vertical"
        size: root.width, root.height
        padding: 50
        spacing: 20

        Label:
            text:'Please select the file...'
            size_hint: ( 1, 0.1)
            font_size: 20

        FileChooserListView:
            id: filechooser
            path: "."
            on_selection: 
                my_widget.selected(filechooser.selection)

        Button:
            text:'OK'
            font_size: 20
            size_hint: ( 1, 0.1)
            on_release: 
                app.root.current = "win_Main"
                root.manager.transition.direction = "down"
                root.transform_to_main()
""")


Window.size = (700, 280)
Window.top = 50
Window.left = 100


class MyApp(App):

    def build(self):
        self.title = "Data Importer"
        return kv


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

您只需要訪問正確的屏幕及其內容。 許多方法之一是使用get_screen方法,如下所示,

    def selected(self, filename):

        try:
            # Access the target screen.
            main_screen = self.manager.get_screen("win_Main")
            # Access its target label.
            file_path_label = main_screen.ids.lbl_file_path_k
            # Set the text.
            file_path_label.text = filename[0]
            print(filename[0])
#            global f_path
            f_path = filename[0]
        except:
            pass

其他方法包括直接從FileChooserListView綁定,在該類或 App 的類中定義自定義變量等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM