简体   繁体   中英

'kivy.properties.ObjectProperty' object has no attribute 'text'

I am new to kivy module so i am trying to understand it. The goal of this code is to just create a text file with some information in it. This is my python file:

from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen, ScreenManager, WipeTransition


class CustomPopup(Popup):
    popup_conferma = ObjectProperty(None)

    def dismiss_popup(self):
        self.dismiss()

class SecondWindow(Screen):
    result_label = ObjectProperty(None)
    def go_back(self):
        sm.switch_to(screens[0], direction = "right")

class MainWindow(Screen):
    submit_button = ObjectProperty(None)
    output_label = ObjectProperty(None)
    nome = ObjectProperty(None)
    cognome = ObjectProperty(None)
    data_di_nascita = ObjectProperty(None)
    luogo_di_nascita = ObjectProperty(None)

    def conferma_invio_form(self):
        custom_popup = CustomPopup(title="Conferma invio form", size_hint=(0.5,0.5), popup_conferma = self.send_form)
        custom_popup.open()

    def send_form(self):
        self.output_label.text = "Sent!!!"
        form = open("C:\\Users\\form.txt","w")
        form.write(f"Nome: {self.nome.text}\nCognome: {self.cognome.text}\nData di nascita: {self.data_di_nascita.text}\nLuogo di nascita: {self.luogo_di_nascita.text}")
        SecondWindow.result_label.text = f"Nome: {self.nome.text}\nCognome: {self.cognome.text}\nData di nascita: {self.data_di_nascita.text}\nLuogo di nascita: {self.luogo_di_nascita.text}"
        sm.switch_to(screens[1], direction = "left")



class WindowManager(ScreenManager):
    pass

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

sm = WindowManager()
screens = [MainWindow(name="MainWindow"), SecondWindow(name="SecondWindow")]
for screen in screens:
    sm.add_widget(screen)
    sm.current = "MainWindow"

class MainApp(App):
    def build(self):
        return sm

sample_app = MainApp()
sample_app.run()

This is my kv file:

<BlueLabel@Label>:
    color: (0,0.2,0.5,1)

<TitleLabel@Label>:
    canvas.before:
        Color:
            rgba:(0.5,0,1,1)
        Rectangle:
            size: self.size
            pos: self.pos
    font_size: 30
    bold: True
    color: (1,1,1,1)

<MainWindow>:
    submit_button: submit_button
    output_label: output_label
    nome: nome
    cognome: cognome
    data_di_nascita: data_di_nascita
    luogo_di_nascita: luogo_di_nascita
    BoxLayout:
        id: main_box
        canvas.before:
            Color:
                rgba:(1,1,1,1)
            Rectangle:
                size: self.size
                pos: self.pos
        orientation: "vertical"
        BoxLayout:
            id: title
            size_hint_y: 0.20
            TitleLabel:
                text: "Form da compilare"
        BoxLayout:
            id: fields
            orientation: "vertical"
            BoxLayout:
                size_hint_y: None
                height: 30
                BlueLabel:
                    text: "Nome"
                TextInput:
                    id: nome
                    multiline: False
            BoxLayout:
                size_hint_y: None
                height: 30
                BlueLabel:
                    text: "Cognome"
                TextInput:
                    id: cognome
                    multiline: False
            BoxLayout:
                size_hint_y: None
                height: 30
                BlueLabel:
                    text: "Luogo di nascita"
                TextInput:
                    id: luogo_di_nascita
                    multiline: False
            BoxLayout:
                size_hint_y: None
                height: 30
                BlueLabel:
                    text: "Data di nascita"
                TextInput:
                    id: data_di_nascita
                    multiline: False
            BoxLayout:
                BlueLabel:
                    id: output_label
        BoxLayout:
            id: button
            size_hint_y: .2
            Label:
            Button:
                id: submit_button
                background_normal: ""
                background_color: (0,0.2,0.5,1)
                text: "Invio"
                on_press:
                    self.background_color = (1,0.2,0.5,0.8)
                on_release:
                    self.background_color = (0,0.2,0.5,1)
                    root.conferma_invio_form()
            Label:

<CustomPopup>:
    popup_conferma: popup_conferma
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "Sei sicuro di voler inviare il form?"
        BoxLayout:
            size_hint_y: 0.3
            Button:
                text: "Annulla"
                on_release:
                    root.dismiss_popup()
            Button:
                id: popup_conferma
                text: "Conferma"
                on_release:
                    root.popup_conferma()
                    root.dismiss_popup()

<SecondWindow>:
    result_label: result_label
    BoxLayout:
        Button:
            text: "<-Indietro"
            on_release:
                root.go_back()
        BoxLayout:
            TitleLabel:
                text: "Form Compilato"
            Label:
                id: result_label`

I cant understand why i obtain this error by running it:

SecondWindow.result_label.text = f"Nome: {self.nome.text}\nCognome: {self.cognome.text}\nData di nascita: {self.data_di_nascita.text}\nLuogo di nascita: {self.luogo_di_nascita.text}"
 AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'

even if the output_label istance has got the same text attribute. Thanks for help!

Here the problem is where you try to change the text label of the SecondWindow from the MainWindow. To do that you need first to get the SecondWindow from the ScreenManager. Try to do it this way:

s = self.manager.get_screen('secondwindow') s.result_label.text="your_text"

This should fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM