Kivy,当切换到另一个窗口时,如何更新TextInput中的文本参数



我正在制作一个具有不同功能的应用程序,其中之一就是Notes。我想实现到另一个窗口的转换,当按下按钮时,文本字段将位于该窗口中。问题是,当我切换时,我无法将TextInput的文本参数设置为我想要的任何值,因为负责切换到另一个窗口的函数在另一个类中。

很遗憾,文本参数正在更改,但没有显示在窗口中。。我只是在学习如何构建Kivy应用程序,任何帮助都将不胜感激。

python.py

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.animation import Animation
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from functools import partial
Window.size = (500, 700)
Window.clearcolor = "#1b1a28"

class MainWindow(Screen):
def go_window(self, window, widget, *args):
self.manager.current = window
animate = Animation(duration=0.4, background_color=(96/255, 85/255, 83/255, 1), color=(1,1,1,1))
animate.start(widget)
def animation(self, widget, *args):
animate = Animation(duration=0.1)
animate += Animation(size_hint=(1.2,1.2),duration=0.1,background_color=(0,0,0,-1))
animate += Animation(size_hint=(1,1),duration=0.1,color=(0,0,1,0))
animate.start(widget)
def on_press(self, widget, *args):
if widget.gid == "accounts":
self.animation(widget)
Clock.schedule_once(partial(self.go_window, "second", widget), 0.5)
if widget.gid == "notes":
self.animation(widget)
Clock.schedule_once(partial(self.go_window, "third", widget), 0.5)
ThirdWindow().write() #I call a function from another class, hoping that it will work(
class SecondWindow(Screen):
pass

class ThirdWindow(Screen):
def __init__(self, **kwargs):
super(ThirdWindow, self).__init__(**kwargs)
Window.bind(on_request_close=self.exit_program)
def go_back(self, widget, *args):
if widget.gid == "notes":
print(self.ids.notes2.text)
self.manager.current = "main"
def write(self):
self.ids.notes2.text = "test"
print(1)

def exit_program(self, *args):
print(self.ids.notes2.text)

class WindowManager(ScreenManager):
pass

kv = Builder.load_file("widgets.kv")

class MyApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyApp().run()

file.kv

WindowManager:
MainWindow:
SecondWindow:
ThirdWindow:
<MainWindow>:
name: "main"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
id: text
text: "Wellcome!"
halign: "center"
font_size: 50
size_hint: (1, .3)
color: "#dcd3c2"
bold: True
ScrollView:
GridLayout:
cols: 2
spacing: 5
padding: 22, 0, 22, 50
size_hint: (1, 1.3)
Button:
gid: "accounts"
text: "Accounts"
font_size: 40
background_normal: ''
background_color: "#605553"
bold: True
on_release: root.on_press(self)
Button:
gid: "notes"
text: "Notes"
font_size: 40
background_normal: ''
background_color: "#605553"
bold: True
on_release: root.on_press(self)
Button:
gid: "people"
background_normal: ''
background_color: "#605553"
bold: True
text: "People"
font_size: 40
on_release: root.on_press(self)
Button:
background_normal: ''
background_color: "#605553"
bold: True
text: "Test"
font_size: 40
on_release: root.on_press(self)
Button:
background_normal: ''
background_color: "#605553"
bold: True
text: "Test"
font_size: 40
on_release: root.on_press(self)
Button:
background_normal: ''
background_color: "#605553"
bold: True
text: "Test"
font_size: 40
Button:
background_normal: ''
background_color: "#605553"
bold: True
text: "Test"
font_size: 40
on_release: root.on_press(self)
Button:
background_normal: ''
background_color: "#605553"
bold: True
text: "Test"
font_size: 40
on_release: root.on_press(self)
Button:
background_normal: ''
background_color: "#605553"
bold: True
text: "Test"
font_size: 40
on_release: root.on_press(self)
Button:
background_normal: ''
background_color: "#605553"
bold: True
text: "Test"
font_size: 40
on_release: root.on_press(self)
<SecondWindow>
name: "second"
BoxLayout:
orientation: "vertical"
TextInput:
text: "Hello"
Button:
text: "Go Back"
on_release: app.root.current = "main"
<ThirdWindow>:
name: "third"
the_time: notes2
BoxLayout:
orientation: "vertical"
TextInput:
id: notes2
font_size: 35
background_color: "#1D2847"
foreground_color: "#F5B662"
size_hint: (1, 1.8)
Button:
gid: "notes"
background_normal: ''
background_color: "#605553"
bold: True
text: "Back"
font_size: 40
size_hint: (1, 0.2)
on_release: root.go_back(self)

问题出在您的代码行:

ThirdWindow().write()

这段代码正在创建ThirdWindow的一个新实例并调用其write()方法。但是ThirdWindow的新实例不是您的App中的实例。ThirdWindow的正确实例由Builder.load_file()调用创建,该调用将创建一个WindowManager,其中ThirdWindow的实例是其子实例之一。因此,您可以通过WindowManager访问ThirdWindow的正确实例,如下所示:

thirdWindow = self.manager.get_screen('third')
thirdWindow.write()

最新更新