Home > OS >  Kivy, how to update the text parameter in TextInput when switching to another window?
Kivy, how to update the text parameter in TextInput when switching to another window?

Time:08-15

I am making an application with different functions, one of them is Notes. I want to implement a transition to another window in which the text field will be located when a button is pressed. The problem is that when I switch, I cannot set the text parameter for TextInput to whatever I want, since the function responsible for switching to another window is in another class.

Unfortunately, the text parameter is being changed but not displayed in the window.. I'm just learning how to build Kivy applications, any help would be greatly appreciated.

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)



CodePudding user response:

The problem is with your line of code:

ThirdWindow().write()

This code is creating a new instance of ThirdWindow and calling its write() method. But the new instance of ThirdWindow is not the instance that is in your App. The correct instance of ThirdWindow is created by your Builder.load_file() call, which creates a WindowManager with an instance of ThirdWindow as one of its children. So you can access the correct instance of ThirdWindow via that WindowManager, like this:

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