Kivy:如何在 TextInput 小部件中绑定文本以跟踪字符串变量
Kivy: How to bind text in TextInput widget to track a string variable
老实说,我不知道这是怎么回事。我做了这个例子只是为了说明我的问题。我在第一页读入了一个 .txt 文件。此文件中的字符串之一被保存。然后,当我转到屏幕 2 时,我希望 TextIpunt 小部件内的初始文本是从文件中提取的文本。我尝试使用 StringProporty 但就像我说的那样没有线索。
这是代码:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import ObjectProperty, StringProperty
month = ''
class FileChoose(BoxLayout):
mnth = ''
def select(self,filename):
try:
with open(filename[0],'r') as f:
for line in f:
i = [s.strip() for s in line.split(',')]
if i[0] == 'jan':
self.mnth = i[0]
month = self.month
except: pass
class popup(Popup):
pass
class Screen1(Screen):
def open(self):
pop = popup()
pop.open()
class Screen2(Screen):
m = StringProperty('')
def on_enter(self):
self.m = month
self.ids.pie.text = self.m
root = ScreenManager()
class MyApp(App):
def build(self):
root.add_widget(Screen1(name='scr1'))
root.add_widget(Screen2(name='scr2'))
return root
if __name__ == '__main__':
MyApp().run()
和 kv 文件:
<Screen1>:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Load file'
on_press: root.open()
Button:
text: 'next screen'
on_press: root.manager.current = 'scr2'
<Screen2>:
TextInput:
id: pie
size_hint: 0.6,0.2
text: ''
<popup>:
auto_dismiss: False
size_hint: 0.6,0.6
BoxLayout:
orientation: 'vertical'
FileChoose:
id: pie
size_hint: 1,0.8
dirselect: True
Button:
size_hint: 1,0.2
text: "dismiss"
on_press: root.dismiss()
<FileChoose>:
id: File
BoxLayout:
orientation: 'vertical'
FileChooserListView:
id: file2
on_selection: File.select(file2.selection)
我将月份设置为全局变量,因为我不知道如何访问父级为弹出窗口的 FileChoose 属性。任何人都可以帮助解决这个问题,也许不使用全局变量。谢谢
文本文件看起来像这样:请制作一个
feb
mar
jan
你的想法是对的。您的解决方案确实有效,但您需要在函数内将“month”声明为全局变量。否则,将定义一个新变量,仅对函数可见,同名。
我编辑了你的代码:
class FileChoose(BoxLayout):
mnth = ''
def select(self, filename):
global global_month
try:
with open(filename[0], 'r') as f:
for line in f:
i = [s.strip() for s in line.split(',')]
if i[0] == 'jan':
self.mnth = i[0]
global_month = "This works" # self.month
except:
pass
注意:我无法真正让您的函数从我的文件中读取该行。也许我做错了什么。因此,我在调用函数时为变量分配了一个硬编码字符串。我还将变量名称从“month”更改为“global_month”以表明它是一个全局变量。
一种更“pythonic”的方法是将函数“链接”在一起并将您的值作为函数参数传递。
老实说,我不知道这是怎么回事。我做了这个例子只是为了说明我的问题。我在第一页读入了一个 .txt 文件。此文件中的字符串之一被保存。然后,当我转到屏幕 2 时,我希望 TextIpunt 小部件内的初始文本是从文件中提取的文本。我尝试使用 StringProporty 但就像我说的那样没有线索。 这是代码:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import ObjectProperty, StringProperty
month = ''
class FileChoose(BoxLayout):
mnth = ''
def select(self,filename):
try:
with open(filename[0],'r') as f:
for line in f:
i = [s.strip() for s in line.split(',')]
if i[0] == 'jan':
self.mnth = i[0]
month = self.month
except: pass
class popup(Popup):
pass
class Screen1(Screen):
def open(self):
pop = popup()
pop.open()
class Screen2(Screen):
m = StringProperty('')
def on_enter(self):
self.m = month
self.ids.pie.text = self.m
root = ScreenManager()
class MyApp(App):
def build(self):
root.add_widget(Screen1(name='scr1'))
root.add_widget(Screen2(name='scr2'))
return root
if __name__ == '__main__':
MyApp().run()
和 kv 文件:
<Screen1>:
BoxLayout:
orientation: 'vertical'
Button:
text: 'Load file'
on_press: root.open()
Button:
text: 'next screen'
on_press: root.manager.current = 'scr2'
<Screen2>:
TextInput:
id: pie
size_hint: 0.6,0.2
text: ''
<popup>:
auto_dismiss: False
size_hint: 0.6,0.6
BoxLayout:
orientation: 'vertical'
FileChoose:
id: pie
size_hint: 1,0.8
dirselect: True
Button:
size_hint: 1,0.2
text: "dismiss"
on_press: root.dismiss()
<FileChoose>:
id: File
BoxLayout:
orientation: 'vertical'
FileChooserListView:
id: file2
on_selection: File.select(file2.selection)
我将月份设置为全局变量,因为我不知道如何访问父级为弹出窗口的 FileChoose 属性。任何人都可以帮助解决这个问题,也许不使用全局变量。谢谢
文本文件看起来像这样:请制作一个
feb
mar
jan
你的想法是对的。您的解决方案确实有效,但您需要在函数内将“month”声明为全局变量。否则,将定义一个新变量,仅对函数可见,同名。
我编辑了你的代码:
class FileChoose(BoxLayout):
mnth = ''
def select(self, filename):
global global_month
try:
with open(filename[0], 'r') as f:
for line in f:
i = [s.strip() for s in line.split(',')]
if i[0] == 'jan':
self.mnth = i[0]
global_month = "This works" # self.month
except:
pass
注意:我无法真正让您的函数从我的文件中读取该行。也许我做错了什么。因此,我在调用函数时为变量分配了一个硬编码字符串。我还将变量名称从“month”更改为“global_month”以表明它是一个全局变量。
一种更“pythonic”的方法是将函数“链接”在一起并将您的值作为函数参数传递。