从 TextInput 保存 txt 文件,然后在单独的 Window/Class Kivy 中重新加载它

Saving txt file from TextInput and then re loading it in separate Window/Class Kivy

正在尝试创建网站的基本版本 "leafly"。 我想使用用户输入在数据集中查找一行并显示用户输入的所有信息。

我将用户的TextInput保存到一个txt文件中,然后在下一个window中打开文本文件。 txt 文件保存良好,但出于某种原因,它只会在我关闭应用程序后加载正确的文本。 因此,如果用户在 InputText 框中输入文本,它将保存到 txt 文件,但是当我尝试在下一个 window 中显示该 txt 文件时,它只会加载加载程序之前的文本文件。 如果我 运行 程序再次运行,以前的用户输入可以工作,但我希望它可以在不关闭应用程序的情况下运行。

我是 python 的新手,所以这可能会解释您看到的任何奇怪的代码哈哈。 我正在使用的数据集可以在这里找到:https://www.kaggle.com/kingburrito666/cannabis-strains

我也知道这有多有趣和荒谬哈哈,但如果你能帮忙,我将不胜感激!

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
import pandas as pd
from kivy.properties import StringProperty
from pathlib import Path

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)

df = pd.read_csv("cannabis.csv", sep=",")

class Menu(Screen):
    pass

class MainWindow(Screen):
    pass

class SecondWindow(Screen):
    st = str("")
    str(df[df['Strain'].str.match(st)])
    type = ObjectProperty(None)

    def btn(self):

        self.st = self.st + str(self.type.text)
        self.st = str(self.st)

        print(self.st, file=open("text.txt", "w"))

然后

class ThirdWindow(Screen):

    with open('text.txt', 'r') as myfile:
        data = myfile.read().strip()

    strain = StringProperty(str(df[df['Strain'].str.match(str(data))]))

class WindowManager(ScreenManager):
    pass

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

class MyMainApp(App):
    def build(self):
        return kv

if __name__== "__main__":
    MyMainApp().run()

.kv 文件

WindowManager:
    Menu
    MainWindow:
    SecondWindow:
    ThirdWindow:

<Menu>:
    name: "menu"

    GridLayout:
        cols:1

        Button:
            text: "Launch My Weed"
            on_release:
                app.root.current = "main"

然后

<MainWindow>:
    name: "main"

    GridLayout:
        cols:1

        GridLayout:
            cols:2

            Label:
                text: "Password: "

            TextInput:
                id: password
                multiline: False

        Button:
            text: "Login"
            on_release:
            app.root.current = "second" if password.text == "password" else "main"
            root.manager.transition.direction = "left"

然后

<SecondWindow>:
    name: "second"
    type: type



    GridLayout:
        cols:1

        GridLayout:
            cols:2

            Label:
                text: "Strain?: "

            TextInput:
                id: type
                multiline: False



        Button:
            text: "Tell Me About My Weed"
            on_release:


            app.root.current = "third"
            root.manager.transition.direction = "down"
            root.btn()



<ThirdWindow>:
    name: "third"


    GridLayout:
        cols:1

        GridLayout:
            cols:2




            Label:
                text : root.strain

你在ThirdWindow的class定义中读取文本文件,它在启动时发生,如果你希望它在你进入屏幕时发生,你可以将这段代码放在on_pre_enter(或 on_enter,但这将在转换完成后发生,因此如果它更改内容,则可能 "flash")。

class ThirdWindow(Screen):
    strain = StringProperty()

    def on_pre_enter(self, *args):
        with open('text.txt', 'r') as myfile:
            data = myfile.read().strip()
            self.strain = str(df[df['Strain'].str.match(str(data))])