Hy, problém je v tom, že so súčasným kódomktorý v tomto okamihu nie je ničím iným ako textovým editorom, kedykoľvek sa pokúsim vytvoriť posúvateľný štítok v jazyku kv a stlačením jediného tlačidla ho vyvolať na hlavnej obrazovke, nezobrazí sa mi žiadna chyba, tam proste nič nie je. Mal by som spomenúť, že text je prevzatý z uloženého súboru a jediná verzia, ktorá funguje, je s bežným štítkom. Toto je kód, viem, že je trochu dlhý, ale je ľahko pochopiteľný, takže ostaňte so mnou. Akýkoľvek vstup je veľmi ocenený a ďakujem vám, že ste si našli čas.
#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("nnn" + " " + ">>>" + 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()
odpovede:
0 pre odpoveď č. 1Prečo to funguje:
tvoj text
na hlavnej obrazovke sa aktualizuje zo súboru, potom sa odovzdá štítku a načíta sa text. ScrollableLabel.text
zostáva nezmenený.
Prečo to nefunguje tak, ako očakávate:
Medzi vašimi triedami teda neexistuje žiadna komunikácia text
zmenená je skutočná self.text
= MainScreen.text
Ako to funguje:
Buď použite niečo v globálnom rozsahu, to znamená premenné vo vašom App
triedy, potom a = App.get_running_app()
a prístup k premennej cez a.<variable>
alebo použitie __init__
inicializujte svoj ScrollableLabel na hlavnej obrazovke a odovzdajte ho text
Takže je to v podstate duplikát toto a ten je duplikátom staršej zjednodušenej otázky.