在 Kivy 1.10 和 Python 2.7.9 中使用 ScreenManager 获取 TextInput 值
Getting TextInput value with ScreenManager in Kivy 1.10 and Python 2.7.9
将 Kivy 1.10.0 与 Python 2.7.9 结合使用 我正在尝试获取用户在单击按钮 (my_button2) 时输入的 TextInput 值。虽然我已经能够获取这与 GridLayout 一起使用似乎我正在使用的方法不适用于 ScreenManager 和 BoxLayout 。收到的错误是:AttributeError: 'ScreenTwo' object has no attribute 'inpt' when my_button2
点击 'Next Screen ' 按钮后,我会转到用户输入文本值的页面,'print' 按钮应该打印它
请看下面:
import kivy
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
class ScreenOne(Screen):
def __init__ (self,**kwargs):
super (ScreenOne, self).__init__(**kwargs)
my_box1 = BoxLayout(orientation='vertical')
my_button1 = Button(text="Next Screen ",size_hint_y=None,size_y=100)
my_button1.bind(on_press=self.changer)
my_box1.add_widget(my_button1)
self.add_widget(my_box1)
def changer(self,*args):
self.manager.current = 'screen2'
class ScreenTwo(Screen):
def __init__(self,**kwargs):
super (ScreenTwo,self).__init__(**kwargs)
layout = BoxLayout(orientation='vertical')
self.add_widget(layout)
inpt = TextInput(text="Some text ",size_y=50)
layout.add_widget(inpt)
my_button2 = Button(text="Print ")
my_button2.bind(on_press=self.click)
layout.add_widget(my_button2)
Home_btn = Button(text="Back")
Home_btn.bind(on_press=self.home)
layout.add_widget(Home_btn)
def click(self,my_button2):
entered_value = self.inpt.text
print entered_value
def home(self,*args):
self.manager.current = 'screen1'
class TestApp(App):
def build(self):
my_screenmanager = ScreenManager()
screen1 = ScreenOne(name='screen1')
screen2 = ScreenTwo(name='screen2')
my_screenmanager.add_widget(screen1)
my_screenmanager.add_widget(screen2)
return my_screenmanager
if __name__ == '__main__':
TestApp().run()
第二屏
当您使用 self 时,您正在尝试访问 class 的成员,但在您的情况下 inpt 它不是,因为它是一个变量 any,如果您想成为 [=20] 的成员=] 你必须提出自我,在你的情况下改变:
inpt = TextInput(text="Some text ",size_y=50)
layout.add_widget(inpt)
至:
self.inpt = TextInput(text="Some text ",size_y=50)
layout.add_widget(self.inpt)
注意:如果您不会遇到很多此类问题,我建议您阅读 OOP 基础知识。
将 Kivy 1.10.0 与 Python 2.7.9 结合使用 我正在尝试获取用户在单击按钮 (my_button2) 时输入的 TextInput 值。虽然我已经能够获取这与 GridLayout 一起使用似乎我正在使用的方法不适用于 ScreenManager 和 BoxLayout 。收到的错误是:AttributeError: 'ScreenTwo' object has no attribute 'inpt' when my_button2
点击 'Next Screen ' 按钮后,我会转到用户输入文本值的页面,'print' 按钮应该打印它
请看下面:
import kivy
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
class ScreenOne(Screen):
def __init__ (self,**kwargs):
super (ScreenOne, self).__init__(**kwargs)
my_box1 = BoxLayout(orientation='vertical')
my_button1 = Button(text="Next Screen ",size_hint_y=None,size_y=100)
my_button1.bind(on_press=self.changer)
my_box1.add_widget(my_button1)
self.add_widget(my_box1)
def changer(self,*args):
self.manager.current = 'screen2'
class ScreenTwo(Screen):
def __init__(self,**kwargs):
super (ScreenTwo,self).__init__(**kwargs)
layout = BoxLayout(orientation='vertical')
self.add_widget(layout)
inpt = TextInput(text="Some text ",size_y=50)
layout.add_widget(inpt)
my_button2 = Button(text="Print ")
my_button2.bind(on_press=self.click)
layout.add_widget(my_button2)
Home_btn = Button(text="Back")
Home_btn.bind(on_press=self.home)
layout.add_widget(Home_btn)
def click(self,my_button2):
entered_value = self.inpt.text
print entered_value
def home(self,*args):
self.manager.current = 'screen1'
class TestApp(App):
def build(self):
my_screenmanager = ScreenManager()
screen1 = ScreenOne(name='screen1')
screen2 = ScreenTwo(name='screen2')
my_screenmanager.add_widget(screen1)
my_screenmanager.add_widget(screen2)
return my_screenmanager
if __name__ == '__main__':
TestApp().run()
第二屏
当您使用 self 时,您正在尝试访问 class 的成员,但在您的情况下 inpt 它不是,因为它是一个变量 any,如果您想成为 [=20] 的成员=] 你必须提出自我,在你的情况下改变:
inpt = TextInput(text="Some text ",size_y=50)
layout.add_widget(inpt)
至:
self.inpt = TextInput(text="Some text ",size_y=50)
layout.add_widget(self.inpt)
注意:如果您不会遇到很多此类问题,我建议您阅读 OOP 基础知识。