python and kivy input text in first windows after click button print ( text ) as label ( update label ) in second widows

十年热恋 提交于 2021-02-08 11:37:49

问题


I want create simple app and I'm new in python, this app contain two windows , at first windows have one button (Save) and two input text. at the second windows we have four label. after click on button in first windows the label ( username 1 print here ) in second windows change according text input in first windows.enter image description here enter image description here

when I run my code the label not change, any one can help me what is wrong.

python code

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.properties import ObjectProperty

class Main_windows(Screen):
    f_user = ObjectProperty(None)
    player = ObjectProperty(None)

    def btn_save(self):
       self.player=self.f_user.text
class Second_windows(Screen):
    pass
class Windows_manager(ScreenManager):
    pass
kv=Builder.load_file("vop.kv")

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

kivy code :

## Collect our windows to windows manager ##
Windows_manager:
  Main_windows:
  Second_windows:


<Main_windows>:
  f_user:f_user



  name:"main_windows"
  BoxLayout:
    orientation:"vertical"
    Label:
        text: "Please insert Name of First Player"

    TextInput:
        id: f_user
        multiline:False
    Label:
        text: "please insert Name of Second Player"
    TextInput:
        id: s_user
        multiline:False

    Button:
        text:"Save"
        on_release:

            app.root.current="second_windows"
            root.manager.transition.direction="up"
            root.btn_save()


<Second_Windows>:

  player:player


  name:"second_windows"
  BoxLayout:
    orientation :"vertical"

    BoxLayout:
        orientation:"horizontal"

        Label:
            id:player
            text:"User number one"
        Label:
            id:player2
            text:"User number Two"
    BoxLayout:
        orientation:"horizontal"
        Label:
            text: "user name 1 print here"
        Label:
            text: "user name 2 print here"
    Button:
        text:"Back"
        on_release:
            app.root.current="main_windows"
            root.manager.transition.direction="down"

回答1:


In the code, the main objective is getting data from one class to the other. This method works only when we are dealing with Screen and ScreenManager. I'm also trying to figure out how we can transfer data between classes. If I figured out the other method, I will update my answer.

python code:

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

class Main_windows(Screen):
    f_user = ObjectProperty(None)
       
class Second_windows(Screen):
    player1 = ObjectProperty(None)

class Windows_manager(ScreenManager):
    pass

class Vop(App):
    def build(self):
        return Builder.load_file("vop.kv")
    
if __name__ == "__main__":
    Vop().run()

kivy code:

## Collect our windows to windows manager ##

#: kivy 2.0.0

Windows_manager:
    Main_windows:
    Second_windows:

<Main_windows>:
    f_user:f_user

    name:"main_windows"

    BoxLayout:
        orientation:"vertical"
        Label:
            text: "Please insert Name of First Player"

        TextInput:
            id: f_user
            multiline:False
        Label:
            text: "please insert Name of Second Player"
        TextInput:
            id: s_user
            multiline:False

        Button:
            text:"Save"
            on_release:
                app.root.current="second_windows"
                root.manager.transition.direction="up"
                root.manager.get_screen('second_windows').player1.text = root.f_user.text

<Second_Windows>:

    name:"second_windows"

    player1: player1

    BoxLayout:
        orientation :"vertical"

        BoxLayout:
            orientation:"horizontal"

            Label:
                text:"User number one"
            Label:
                text:"User number Two"

        BoxLayout:
            orientation:"horizontal"
            Label:
                id: player1
                text: '' # change here
            Label:
                id: player2
                text: "user name 2 print here"
        Button:
            text:"Back"
            on_release:
                app.root.current="main_windows"
                root.manager.transition.direction="down"

App Screenshots



来源:https://stackoverflow.com/questions/65746479/python-and-kivy-input-text-in-first-windows-after-click-button-print-text-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!