I was brainstorming an idea I had for a project but the one part I got stuck at was collecting a variable from a text input box and displaying its Value as a Label in another screen, I tried putting it in a variable then just calling it as the text for the label in the kivy file but it always came back as an error, what am I doing wrong.
Kivy-
WindowManager:
Enter_Name
List
<Enter_Name>
airline: input_1
name: 'enter_name'
id: enter_nom
FloatLayout:
cols: 3
size: root.size
Label:
text: "Name of Airline?"
size_hint: 1, 0.3
pos_hint: {"x": 0, "top":1}
TextInput:
multiline: False
name: 'input_one'
id: input_1
size_hint: 0.6, 0.06
pos_hint: {"x": 0.20, "top":0.6}
Button:
size_hint: 0.2, 0.1
pos_hint: {"x": 0.4, "top":0.4}
text: "Enter"
on_release:
app.root.current = 'list'
root.line()
<List>
name: 'list'
Python -
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.uix.popup import Popup
import time
from kivy.properties import StringProperty
class Enter_Name(Screen):
input_1 = StringProperty()
def line(self):
self.gon = self.ids.input_1.text
print(self.gon)
pass
class List(Screen):
Enter_Name.line
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("pot.kv")
class am4(App):
def build(self):
return kv
if __name__ == "__main__":
am4().run()
Here is a modified version of your code that does what I think you want:
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
class Enter_Name(Screen):
def line(self):
App.get_running_app().root.get_screen('list').lab_text = self.airline.text
class List(Screen):
pass
class WindowManager(ScreenManager):
pass
# kv = Builder.load_file("pot.kv")
kv = Builder.load_string('''
WindowManager:
Enter_Name
List
<Enter_Name>
airline: input_1
name: 'enter_name'
id: enter_nom
FloatLayout:
cols: 3
size: root.size
Label:
text: "Name of Airline?"
size_hint: 1, 0.3
pos_hint: {"x": 0, "top":1}
TextInput:
multiline: False
name: 'input_one'
id: input_1
size_hint: 0.6, 0.06
pos_hint: {"x": 0.20, "top":0.6}
Button:
size_hint: 0.2, 0.1
pos_hint: {"x": 0.4, "top":0.4}
text: "Enter"
on_release:
app.root.current = 'list'
root.line()
<List>
lab_text: ''
name: 'list'
Label:
text: root.lab_text
''')
class am4(App):
def build(self):
return kv
if __name__ == "__main__":
am4().run()
I have added a Label
to the List
Screen
, as well as a lab_text
property that is used as the text
of the Label
. The line()
method now just sets the value of that lab_text
.
I have used load_string()
instead of load_file()
just for my own convenience.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.