首页 > 解决方案 > 我也想将用户在其他屏幕的一个屏幕上输入的文本用作标题(按钮或标签)

问题描述

目前我将用户输入的所有文本存储在一个文件中。当我运行此代码时,屏幕五个按钮文本 root.a 和 root.b 是以前的条目,但不是当前输入的文本输入。

这意味着在第一次执行中,我在屏幕 4 上输入了 firstname1 和 lastname1。在屏幕 5 的按钮中,我看到了 ( with ),因为这是第一次创建文件,因此其中没有文本。

在第二次执行中,我在屏幕四中输入了 firstname2 和 lastname2,但是当我移动到屏幕五时,它在按钮的文本中显示“firstname1 with firstname1”,但我期待“firstname2 with lastname2”,这是我在当前执行期间输入的内容,这是蟒蛇代码:

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

from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.config import Config
from kivy.base import runTouchApp

from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty, NumericProperty, StringProperty


root = FloatLayout()

class ScreenManagement(ScreenManager):
    pass

class A(Screen):
    pass

class B(Screen):
    pass

class C(Screen):
    pass

class ScreenFour(Screen):
    group_text_input = ObjectProperty()
    disability_text_input = ObjectProperty()

    grouptextinput = StringProperty('')
    disabilitytextinput = StringProperty('')

    global grouptextinput, disabilitytextinput

    def submit_textinput(self):
        self.grouptextinput = self.group_text_input.text
        print("group of people: {}".format(self.grouptextinput))
        self.save()

        self.disabilitytextinput = self.disability_text_input.text
        print("with following disability: {}".format(self.disabilitytextinput))
        self.save2()

    def save(self):
        with open("grouptextinput.txt", "w") as fobj:
            fobj.write(str(self.grouptextinput))
    def save2(self):
        with open("disabilitytextinput.txt", "w") as fobj:
            fobj.write(str(self.disabilitytextinput))            
    pass

class Help(Screen):
    pass

class ScreenFive(Screen):
    box = ScreenFour()
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    print 'file is opened'
    a = myfile.readline(14)
    print a

    myfile = open('disabilitytextinput.txt','r')
    print 'file is opened'
    b = myfile.readline(14)
    print b

##    a = box.grouptextinput
##    b = box.grouptextinput

    pass

class ScreenSix(Screen):
    pass

class One(Screen):
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    a = myfile.readline(14)

    myfile = open('disabilitytextinput.txt','r')
    b = myfile.readline(14)

    pass

class Two(Screen):
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    a = myfile.readline(14)

    myfile = open('disabilitytextinput.txt','r')
    b = myfile.readline(14)

    pass

class Three(Screen):
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    a = myfile.readline(14)

    myfile = open('disabilitytextinput.txt','r')
    b = myfile.readline(14)

    pass

class Four(Screen):
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    a = myfile.readline(14)

    myfile = open('disabilitytextinput.txt','r')
    b = myfile.readline(14)

    pass

class Five(Screen):
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    a = myfile.readline(14)

    myfile = open('disabilitytextinput.txt','r')
    b = myfile.readline(14)

    pass

class Six(Screen):
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    a = myfile.readline(14)

    myfile = open('disabilitytextinput.txt','r')
    b = myfile.readline(14)

    pass

class Seven(Screen):
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    a = myfile.readline(14)

    myfile = open('disabilitytextinput.txt','r')
    b = myfile.readline(14)

    pass

class Eight(Screen):
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    a = myfile.readline(14)

    myfile = open('disabilitytextinput.txt','r')
    b = myfile.readline(14)

    pass

class Nine(Screen):
    a = StringProperty()
    b = StringProperty()


    myfile = open('grouptextinput.txt','r')
    a = myfile.readline(14)

    myfile = open('disabilitytextinput.txt','r')
    b = myfile.readline(14)

    pass

presentation = Builder.load_file("main.kv")

class MainApp(App):



    def build(self):
        return presentation

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

这是基维代码:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    A:
    B:
    C:
    ScreenFour:
    Help:
    ScreenFive:
    ScreenSix:
    One:
    Two:
    Three:
    Four:
    Five:
    Six:
    Seven:
    Eight:
    Nine:

<A>:
    name: "llll"
    Button:
        on_release: app.root.current = "iiii"
        Image:
            source: '************'
            center_x: self.parent.center_x
            center_y: self.parent.center_y

<Intentions>:
    name: "iiii"
    Image:
        source: '***********'
        y: self.parent.y 
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: 'Next'
        id: button
        font_size: 40
        size_hint: None, None
        on_release: app.root.current = "pppp"
        pos: 590,50
        size: 200, 60

<Picot>:
    name: "pppp"
    Image:
        source: '****************'
        y: self.parent.y 
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: 'Next'
        id: button
        font_size: 40
        size_hint: None, None
        on_release: app.root.current = "screenfour"
        pos: 590,50
        size: 200, 60   

<ScreenFour>
    name: "screenfour"
    group_text_input: group
    disability_text_input: disability
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: '***************'

    Label:
        text: "**********"
        font_size: '47sp'
        pos: 0, 100
        color: 0,0,0,1
        bold: True

    TextInput:
        id: group
        multiline: False
        size_hint_x: .3
        size_hint_y: .1
        hint_text: '************'
        pos: 90, 220
        font_size: '29sp'

    Label:
        text:"********"
        font_size: '40sp'
        pos: 0, -50
        color: 0,0,0,1
        bold: True

    TextInput:
        id: disability
        multiline: True
        size_hint_x: .3
        size_hint_y: .1
        hint_text: '*********'
        pos: 465, 220
        font_size: '29sp'

    Button:
        text: 'Next'
        id: button
        font_size: 40
        size_hint: None, None
        on_release: app.root.current = "screenfive"
        on_release: root.submit_textinput()
        pos: 590,50
        size: 200, 60

    Button:
        text: 'Help!'
        id: button
        font_size: 30
        size_hint: None, None
        on_release: app.root.current = "help"
        pos: 50,50
        size: 100, 60

<Help>
    name: "help"
    Image:
        source: '******************'
        y: self.parent.y 
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: '*********'
        id: button
        font_size: 40
        size_hint: None, None
        on_release: app.root.current = "screenfour"
        pos: 590,50
        size: 200, 60

<ScreenFive>
    name:"screenfive"
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: '***************'

    Label:
        text: '********'
        font_size: '60sp'
        pos: 0, 150
        color: 0,0,0,1
        bold: True

    Label:
        text: '**********'
        font_size: '53sp'
        pos: 0, 75
        color: 0,0,0,1
        bold: True

    Label:
        text: '*******'
        font_size: '60sp'
        pos: 0, 0
        color: 0,0,0,1
        bold: True

    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size: 760, 100
        size_hint: None, None
        pos: 20,150
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1

    Button:
        text: 'Continue'
        id: button
        font_size: 40
        size: 200, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 590,50 

<ScreenSix>
    name: "menu"
    canvas:
        Color:
            rgb: .5, .5, .5
        Rectangle:
            size: self.size

    GridLayout:
        cols: 3
        spacing: 20
        padding: [20,50]
        Label:
            text: ''
        Label:
            text: '************'
            font_size: '19sp'
        Label:
            text: ''
        Button:
            text: '********'
            #on_press: root.manager.current = 'settings'
            on_release: app.root.current = "one"
        Button:
            text: '*******'
            on_release: app.root.current = "two"
        Button:
            text: '*****'
            on_release: app.root.current = "three"
        Button:
            text: '********'
            on_release: app.root.current = "four"
        Button:
            text: '********'
            on_release: app.root.current = "five"
        Button:
            text: '********'
            on_release: app.root.current = "six"
        Button:
            text: '*******'
            on_release: app.root.current = "seven"
        Button:
            text: '*********'
            on_release: app.root.current = "eight"
        Button:
            text: '***********'
            on_release: app.root.current = "nine"

<One>
    name: "one"
    Image:
        source: '********'
        y: self.parent.y
        x: self.parent.x
        allow_stretch: True

    Button:
        text: 'Back To Menu'
        id: button
        font_size: 40
        size: 300, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 100,13
        allow_stretch: True

    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size_hint: None, None
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1
        pos: 0,520
        size: 800, 80

<Two>
    name: "two"
    Image:
        source: '*******'
        y: self.parent.y
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: 'Back To Menu'
        id: button
        font_size: 40
        size: 300, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 100,13
    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size_hint: None, None
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1
        pos: 0,520
        size: 800, 80

<Three>
    name: "three"
    Image:
        source: '***********'
        y: self.parent.y
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: 'Back To Menu'
        id: button
        font_size: 40
        size: 300, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 100,13
    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size_hint: None, None
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1
        pos: 0,520
        size: 800, 80

<Four>
    name: "four"
    Image:
        source: '*************'
        y: self.parent.y
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: 'Back To Menu'
        id: button
        font_size: 40
        size: 300, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 100,13
    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size_hint: None, None
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1
        pos: 0,520
        size: 800, 80

<Five>
    name: "five"
    Image:
        source: '********************'
        y: self.parent.y
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: 'Back To Menu'
        id: button
        font_size: 40
        size: 300, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 100,13
    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size_hint: None, None
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1
        pos: 0,520
        size: 800, 80

<Six>
    name: "six"
    Image:
        source: '************'
        y: self.parent.y
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: 'Back To Menu'
        id: button
        font_size: 40
        size: 300, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 100,13
    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size_hint: None, None
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1
        pos: 0,520
        size: 800, 80

<Seven>
    name: "seven"
    Image:
        source: '*******************'
        y: self.parent.y
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: 'Back To Menu'
        id: button
        font_size: 40
        size: 300, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 100,13
    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size_hint: None, None
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1
        pos: 0,520
        size: 800, 80

<Eight>
    name: "eight"
    Image:
        source: '********************'
        y: self.parent.y
        x: self.parent.x
        allow_stretch: True
        size: 800, 750      
    Button:
        text: 'Back To Menu'
        id: button
        font_size: 40
        size: 300, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 100,13
    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size_hint: None, None
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1
        pos: 0,520
        size: 800, 80

<Nine>
    name: "nine"
    Image:
        source: '***************'
        y: self.parent.y
        x: self.parent.x
        allow_stretch: True
        size: 800, 750
    Button:
        text: 'Back To Menu'
        id: button
        font_size: 40
        size: 300, 60
        size_hint: None, None
        on_release: app.root.current = "menu"
        pos: 100,13
    Button:
        text: root.a + ' WITH ' + root.b
        font_size: 40
        text_size: self.width, None
        size_hint: None, None
        halign: 'center'
        background_color: (0, 0, 0, 1)
        color: 1, 1, 1, 1
        pos: 0,520
        size: 800, 80

我对 python 和 kivy 都是新手,这是阻止我构建我的第一个应用程序的最后一件事。如果有人能帮助我,我将非常感激。

标签: pythonkivy

解决方案


首先,您的代码存在一些问题。在每种情况下,您都有:

a = StringProperty()
b = StringProperty()

在你的类定义中,你遵循它:

a = myfile.readline(14)

b = myfile.readline(14)

这将您替换StringProperty为普通的String,因此消除了文件中带有和 之类的引用的root.a链接。root.bkv

为了使它起作用,我将您的定义更改ScreenFive为:

class ScreenFive(Screen):
    box = ScreenFour()
    a = StringProperty()
    b = StringProperty()

    def __init__(self, **kwargs):
        super(ScreenFive, self).__init__(**kwargs)

        myfile = open('grouptextinput.txt','r')
        print 'file is opened'
        self.a = myfile.readline(14)
        print self.a

        myfile = open('disabilitytextinput.txt','r')
        print 'file is opened'
        self.b = myfile.readline(14)
        print self.b

以上保持a和允许绑定工作b。下一步是在您希望更改值时进行更新。为此,我将您的方法修改为:StringPropertykvabScreenFivesubmit_textinputScreenFour

def submit_textinput(self):
    self.grouptextinput = self.group_text_input.text
    print("group of people: {}".format(self.grouptextinput))
    App.get_running_app().sm.get_screen("screenfive").a = self.grouptextinput
    self.save()

    self.disabilitytextinput = self.disability_text_input.text
    print("with following disability: {}".format(self.disabilitytextinput))
    App.get_running_app().sm.get_screen("screenfive").b = self.disabilitytextinput
    self.save2()

这些行:

App.get_running_app().sm.get_screen("screenfive").a = self.grouptextinput

和:

App.get_running_app().sm.get_screen("screenfive").b = self.disabilitytextinput

更新中的StringPropertyScreenFive

我也改变了你的MainApp

class MainApp(App):

    def build(self):
        self.sm = presentation
        return presentation

保留对ScreenManager.


推荐阅读