当从第一个屏幕转到第二个屏幕时,我想传递一个变量作为参数,以便kivymd可以从存储在excel文件中的文本更新第二个屏幕。以下是我的应用程序功能的框架:
用户通过kivymd中的导航抽屉到达屏幕1,屏幕1在两个小型可点击MD卡上向用户显示两个选项:
“将文本更改为1”
“将文本更改为2”
单击其中一个后,应用程序切换到屏幕2,屏幕上只有一张大MD卡,此MD卡上的文本应更改以反映用户选择的选项。
然而,kivy正在从excel文件中提取要显示在大MD卡上的文本。我想从屏幕1传递到屏幕2的变量只是一个数字(1或2),它将告诉kivy它应该从excel文件中的哪一行提取文本
如果用户单击“将文本更改为1”,则第一个屏幕应通过“1”作为参数 row_x
到功能 def change_text()
(请参见screen 2.py),以便excel第1行中的文本可以显示在第二个屏幕上。我怎样才能做到这一点?
我一共有4个文件;3个是.py文件(一个用于主应用程序,一个用于屏幕1,一个用于屏幕2)和excel文件
注:在下面的代码中,屏幕1和2分别称为元素1和2
main.py:
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from element_1 import element_1_screen
from element_2 import element_2_screen
MainNavigation = '''
<ContentNavigationDrawer>:
ScrollView:
MDList:
OneLineListItem:
text: 'Go to Element 1'
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "go_to_element_1_screen"
Screen:
MDToolbar:
id: toolbar
pos_hint: {"top": 1}
elevation: 10
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDNavigationLayout:
x: toolbar.height
ScreenManager:
id: screen_manager
Screen:
name: "words_nav_item"
element_1_screen:
name: "go_to_element_1_screen"
element_2_screen:
name: "go_to_element_2_screen"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
'''
class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class mainApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "Red"
return Builder.load_string(MainNavigation)
mainApp().run()
屏幕1/元素1
from kivy.lang import Builder
from kivymd.uix.screen import MDScreen
element_1_contents = '''
<element_1_screen>:
MDGridLayout:
rows: 2
size: root.width, root.height
pos_hint: {"center_x": .8, "center_y": .2}
spacing: 40
MDCard:
orientation: 'vertical'
size_hint: None, None
size: "360dp", "120dp"
ripple_behavior: True
on_release:
root.manager.current = "go_to_element_2_screen"
MDLabel:
id: LabelTextID
text: "Change Text to 1"
halign: 'center'
MDCard:
orientation: 'vertical'
size_hint: None, None
size: "360dp", "120dp"
ripple_behavior: True
on_release:
root.manager.current = "go_to_element_2_screen"
MDLabel:
id: LabelTextID
text: "Change Text to 2"
halign: 'center'
'''
class element_1_screen(MDScreen):
def __init__(self,**kwargs):
super().__init__(**kwargs)
Builder.load_string(element_1_contents)
屏幕2/元素2
from kivy.lang import Builder
from kivymd.uix.screen import MDScreen
import openpyxl
element_2_contents = '''
<element_2_screen>:
MDCard:
orientation: 'vertical'
size_hint: None, None
size: "360dp", "360dp"
pos_hint: {"center_x": .5, "center_y": .5}
ripple_behavior: True
focus_behavior: True
on_release: root.manager.current = "go_to_element_1_screen"
MDLabel:
id: TextID
text: "NOTHING HAS CHANGED"
halign: 'center'
MDLabel:
text: "(Click here to return)"
halign: 'center'
'''
class element_2_screen(MDScreen):
def __init__(self,**kwargs):
super().__init__(**kwargs)
path = "data.xlsx"
self.wb_obj = openpyxl.load_workbook(path)
self.sheet_obj = self.wb_obj.active
Builder.load_string(element_2_contents)
def change_text(self, row_x=0):
row_number = self.sheet_obj.cell(row_x, column=1)
self.ids.TextID.text = str(row_number.value)
excel文件在a列中只有两个条目:
Row 1: You have chosen 1
Row 2: You have chosen 2
1条答案
按热度按时间ve7v8dk21#
我找到了答案,现在它完美无瑕。reddit(u/username\u randint)上有人帮了我,他们是这么说的:
屏幕管理器有一个
get_screen()
方法。您可以使用它获取第二个屏幕的示例并调用change_text()
方法。在切换屏幕的同一位置,添加另一行:另一张卡也是一样,只需输入2而不是1。