我需要做最简单的任务,即我从第一个屏幕文本输入接收的输入影响第二个屏幕上的结果,我已经尝试了我知道的所有事情,例如全局变量..存储到变量然后使用和甚至把课程放在一起似乎没什么用。什么是从第一个屏幕获取输入并将其存储到我稍后可以在第二个屏幕上使用的变量的最简单方法..下面有一段代码
def screenswitch(self):
sm.current= 'settings'
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 2
self.spacing = [5,140]
self.test=Label(text='nothing')
self.add_widget(self.test)
self.add_widget(Label(text='Gender',color=(1, 0, 0, 1), font_size= 42))
self.gender = TextInput(text='why',multiline=False,font_size=32,write_tab=False)
self.add_widget(self.gender)
self.gender.bind(text=self.test.setter('text'))
self.btn1=(Button(text='Submit'))
self.add_widget(self.btn1)
self.btn1.bind(on_press=screenswitch)
class MenuScreen(Screen):
def __init__(self,**kwargs):
super(MenuScreen, self).__init__(**kwargs)
self.loginscreen = LoginScreen()
self.add_widget(self.loginscreen)
class TestScreen(BoxLayout):
def __init__(self, **kwargs):
super(TestScreen, self).__init__(**kwargs)
self.loginscreen = LoginScreen()
print self.loginscreen.gender.text
self.orientation = 'vertical'
self.add_widget(Label(text=self.loginscreen.gender.text))
self.add_widget(Label(text='password'))
class SettingsScreen(Screen):
def __init__(self,**kwargs):
super(SettingsScreen,self).__init__(**kwargs)
self.testscreen = TestScreen()
self.add_widget(self.testscreen)
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
class MyApp(App):
def build(self):
return sm
if __name__ == '__main__':
MyApp().run()
所以无论我在第二个屏幕上做什么,第一个标签总是显示"为什么"虽然我确信gender.text正在改变,我正在改变它 我做错了什么?
答案 0 :(得分:0)
最简单的方法是Kivy方式:
MyApp
app.get_running_app().your_class_variable_name
from kivy.app import app
这意味着您可以使用kivy方式创建中间变量来管理两个模块之间的通信。您也可以使用python方式,它使用单个模块来保存所有共享变量。然后,您只需import
所需的模块,即可访问其值。有很多方法,这个很快,因为你不需要新文件。
这是保存2个屏幕的主文件,2个屏幕是在同一个文件中还是在2个不同的文件中并不重要,这里我将它们全部放在main.py
中仅用于快速破解。如果需要,您可以将它们分开。
每个屏幕都有3个按钮
您可以看到两个屏幕如何访问相同的共享变量并进行更改
我在这里显示两种方式来访问共享变量:
- 如何在.kv
文件中访问它
- 如何在.py
文件中访问它。
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
Builder.load_string("""
<LoginScreen>:
BoxLayout:
orientation:'vertical'
Label:
text: 'I am LoginScreen'
Label:
id: lbl1
Button:
text: 'Read'
on_press: root.press_read()
Button:
text: 'Change'
on_press:
app.MY_NUMBER = app.MY_NUMBER + 1
root.ids.lbl1.text = 'SharedVar is ' + str(app.MY_NUMBER)
Button:
text: 'Go to ScreenTwo'
on_press: app.sm.current = "screen_2"
<MenuScreen>:
BoxLayout:
orientation:'vertical'
Label:
text: 'I am MenuScreen'
Label:
id: lbl2
Button:
text: 'Read'
on_press: root.press_read()
Button:
text: 'Change'
on_press:
app.MY_NUMBER = app.MY_NUMBER + 1
root.ids.lbl2.text = 'SharedVar is ' + str(app.MY_NUMBER)
Button:
text: 'Go to ScreenOne'
on_press: app.sm.current = "screen_1"
""")
class LoginScreen(Screen):
def press_read(self):
app = App.get_running_app()
self.ids.lbl1.text = "SharedVar is " + str(app.MY_NUMBER)
class MenuScreen(Screen):
def press_read(self):
app = App.get_running_app()
self.ids.lbl2.text = "SharedVar is now " + str(app.MY_NUMBER)
class HandSetApp(App):
MY_NUMBER = 0
sm = ScreenManager()
def build(self):
HandSetApp.sm.add_widget(ScreenOne(name='screen_1'))
HandSetApp.sm.add_widget(ScreenTwo(name='screen_2'))
return HandSetApp.sm
if __name__ == '__main__':
HandSetApp().run()
答案 1 :(得分:0)
应该是:
class HandSetApp(App):
MY_NUMBER = 0
sm = ScreenManager()
def build(self):
HandSetApp.sm.add_widget(LoginScreen(name='screen_1'))
HandSetApp.sm.add_widget(MenuScreen(name='screen_2'))
return HandSetApp.sm
然后程序将运行。