Reputation: 105
I'm trying but failing on a simple task of changing a label nested in a different screen in Kivy with screenmanager.
I want to show "f_path" variable on "lbl_file_path" label. "lbl_file_path" label is on "W_MainMenu" screen. "f_path" is created on "W_FileSelector" screen.
How can I do this ? Any help is greatly appreciated. You may find the code below;
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()
Upvotes: 0
Views: 79
Reputation: 2888
You just need to access the right screen and its contents. One of the many ways would be using the method get_screen
as follows,
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
Other ways include binding directly from FileChooserListView
, defining custom variable in that class or in the App's class etc.
Upvotes: 1