【问题标题】:Kivy Scrollable Label and text file, label wont updateKivy 可滚动标签和文本文件,标签不会更新
【发布时间】:2016-03-17 10:21:44
【问题描述】:

Hy,问题在于,当我尝试用 kv 语言制作可滚动标签并在推送时在主屏幕上调用它时,当前的代码(此时它只是一个文本编辑器)一个按钮,我没有错误,那里什么都没有。我应该提到,文本是从存储的文件中获取的,唯一有效的版本是带有常规标签的。这是代码,我知道它有点长,但它很容易理解,所以请留在我身边。非常感谢您提供任何形式的意见,感谢您抽出宝贵时间。

#kivy.require("1.8.0")
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Line
from kivy.uix.gridlayout import GridLayout


kv = '''
#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManager:
    transition: FadeTransition()
    MainScreen:
    AddScreen:
    AppendScreen:


<ScatterTextWidget>:
    orientation: 'vertical'

    TextInput:
        id: main_input
        font_size: 14
        size_hint_y: None
        height: root.height - botones_layout.height
        font_color: [0.1,0.3,0.9,1]
        focus: True
        foreground_color: [0.2,0.5,0.9,1]
        cursor_color: [0,0,1,1]

    BoxLayout:
        id: botones_layout
        orientation: 'horizontal'
        height: 30
        Button:
            id: home_button
            text: "Back Home"            
        Button:
            id: save_button
            text: "Save to file"
            on_press: root.saveToFile("Archive.txt", main_input.text)


<AppendTextWidget>:
    orientation: 'vertical'

    TextInput:
        text: root.text
        id: main_input
        font_size: 14
        size_hint_y: None
        height: root.height - botones_layout.height
        font_color: [0.1,0.3,0.9,1]
        focus: True
        foreground_color: [0.2,0.5,0.9,1]
        cursor_color: [0,0,1,1]

    BoxLayout:
        id: botones_layout
        orientation: 'horizontal'
        height: 30
        Button:
            id: home_button
            text: "Back Home"
            on_release: app.root.current = "main"
        Button:
            id: save_button
            text: "Save"
            on_press: root.append(main_input.text)

#This does not work <--- <--- <---

<ScrollableLabel>:
    Label:
        text: root.text
        font_size: 15
        text_size: self.width, None
        color: [0,255,0,1]
        padding_x: 20
        size_hint_y: None
        pos_hint: {"left":1, "top":1}
        height: self.texture_size[1]


<MainScreen>:
    name: "main"
    FloatLayout:

        # This does work 

        Label:
            text: root.text
            font_size: 15
            text_size: self.width, None
            color: [0,255,0,1]
            padding_x: 20
            size_hint_y: None
            pos_hint: {"left":1, "top":1}
            height: self.texture_size[1]        

    ActionBar:
        pos_hint: {'top':1}
        ActionView:
            use_separator: True
            ActionPrevious:
                title: "Text"
                with_previous: False
            ActionOverflow:
            ActionButton:
                text: "New"
                on_release: app.root.current = "add"
            ActionButton:
                text: "Update"
                on_press: root.clicked()
            ActionButton:
                text: "Add"
                on_release: app.root.current = "append"


<AddScreen>:
    name: "add"
    FloatLayout:
         ScatterTextWidget

<AppendScreen>:
    name: "append"
    FloatLayout:
        AppendTextWidget        

'''


class ScatterTextWidget(BoxLayout):
    def saveToFile(self,name,text):
        f = open(name, "w")
        f.write("\n\n\n" + "    " + ">>>" + text + "test"*500)
        f.close()

class AppendTextWidget(BoxLayout):
    text = StringProperty("")
    def append(self,text):
        with open("Archive.txt", "a") as f:
            f.write("\n" + "    " + ">>>" + text)
            f.close()



 class ScrollableLabel(ScrollView):
     text = StringProperty('')
     pass


class MainScreen(Screen):
    text = StringProperty("")

    def clicked(self):
        with open("Archive.txt", "r") as f:
            contents = f.read()
            self.text = contents
    pass

class AddScreen(Screen):
    pass

class AppendScreen(Screen):
    pass    

class MyApp(App):

    def build(self):
        return Builder.load_string(kv)



if __name__ == '__main__':
    MyApp().run()

【问题讨论】:

    标签: python-3.x label scrollview kivy


    【解决方案1】:

    为什么会这样:

    您在 MainScreen 中的 text 从文件更新,然后传递给 Label 并加载文本。 ScrollableLabel.text 保持不变。

    为什么它不能像你期望的那样工作:

    你的类之间没有通信,因此只有 text changed 是一个实际的 self.text = MainScreen.text

    如何让它工作:

    要么在全局范围内使用某些东西,即在 App 类中使用变量,然后是 a = App.get_running_app() 并通过 a.&lt;variable&gt; 访问变量,或者使用 __init__ 在 MainScreen 中初始化 ScrollableLabel 并将其传递给 text

    所以它基本上是 this 的副本,并且是一个较旧的未简化问题的副本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2014-03-04
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多