使用屏幕管理器更改嵌套在 Kivy 不同屏幕中的标签
Changing a label nested in a different screen in Kivy with screenmanager
我正在尝试使用 screenmanager 更改嵌套在 Kivy 不同屏幕中的标签的简单任务,但失败了。
我想在“lbl_file_path”标签上显示“f_path”变量。 “lbl_file_path”标签在“W_MainMenu”屏幕上。 “f_path”是在“W_FileSelector”屏幕上创建的。
我该怎么做?任何帮助是极大的赞赏。您可能会在下面找到代码;
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
绑定、在那个 class 或应用的 class 中定义自定义变量等
我正在尝试使用 screenmanager 更改嵌套在 Kivy 不同屏幕中的标签的简单任务,但失败了。
我想在“lbl_file_path”标签上显示“f_path”变量。 “lbl_file_path”标签在“W_MainMenu”屏幕上。 “f_path”是在“W_FileSelector”屏幕上创建的。
我该怎么做?任何帮助是极大的赞赏。您可能会在下面找到代码;
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
绑定、在那个 class 或应用的 class 中定义自定义变量等