Python - Kivy - 屏幕管理器 - 使用文本输入问题更改按钮文本

时间:2017-09-07 20:19:30

标签: python user-interface kivy

我目前正在与Kivy合作进行GUI设计我正在寻找一种方法,可以通过另一个屏幕上的TextInput更改按钮文本。

我的屏幕1有一个按钮,可用作“标签”,我有另一个按钮可以进入屏幕2。

屏幕2是一个带有Textinput的键盘,在那里我把我要设置的数字放在屏幕1的“标签”按钮上。

使用名为“ENTER”的按钮,我想返回到屏幕1并更新按钮“label”中的新文本。但我无法弄清楚如何正确地做到这一点。

以下是项目代码的一小部分 main.py

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

from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.graphics import Line

from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput

class Main_Screen(Screen):
    pass

class Input_Number_Inch_Screen(Screen):
    pass

class Input_Screen(Screen):
    pass

class Screen_Management(ScreenManager):
    pass

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

class Screen3App(App):

    def build(self):
        return presentation

Screen3App().run()

screen3.kv 文件:

    Screen_Management:
        id: screen_management
        transition: FadeTransition()
        Main_Screen:
            id: main_screen
            name: "main_screen_name"
            manager: screen_management
        Input_Screen:
            id: tire_setup_screen_id
            name: "tire_setup_screen_name"
            manager: screen_management
        Input_Number_Inch_Screen:
            name: "inch_screen"
            manager: screen_management

<Main_Screen>:
    canvas:
        Color:
            rgb: [.30, .30, .30]
        Rectangle:
            pos: self.pos
            size: self.size
    Button:
        background_color: .52, .52, .52, 1
        bold: 1
        color: .0078,.67,.69,1
        size_hint: .2, 1
        pos_hint: {"x": 0, "center_y": .5}
        on_release: app.root.current = "tire_setup_screen_name"
        text: " INPUTS "
        font_size: 30

    # Screen 1: Input Screen
    <Input_Screen>:
        canvas:
            Color:
                rgb: [.30, .30, .30]
            Rectangle:
                pos: self.pos
                size: self.size
        GridLayout:
            cols: 2
            pos: (160,150)
            size_hint: (.8, .8)
            Button:
                background_color: .52, .52, .52, 1
                bold: 1
                color: .0078,.67,.69,1
                font_size: 30
                text: "INPUT\n(Inch)"
                size_hint_x: None
                width: 150
                on_release: app.root.current = "inch_screen"
                # This button will go to the screen2

            Button:
                id: inch_input
                background_color: .52, .52, .52, 1
                bold: 1
                color: .0078,.67,.69,1
                font_size: 100
                text: "THIS IS THE TEXT THAT I WANT TO UPDATE"

    # Screen 2: Input Inch Screen Data
    <Input_Number_Inch_Screen>:
        canvas:
            Color:
                rgb: [.30, .30, .30]
            Rectangle:
                pos: self.pos
                size: self.size

        GridLayout:
            orientation: 'vertical'
            display: entry
            rows: 6
            padding: 10
            spacing: 10

    # This is the TextInput 
            BoxLayout:
                TextInput:
                    id: entry
                    font_size: 75
                    multiline: False

    # This will be the button that would go back to the screen and update
    # the button text with the new text entered in the TextInput
            BoxLayout:
                spacing: 10
                Button:
                    background_color: .52, .52, .52, 1
                    bold: 1
                    color: .0078,.67,.69,1
                    font_size: 40
                    text:"ENTER"
                    on_release: app.root.current = "tire_setup_screen_name"
                    on_press: app.root.inch_input.text = entry.text

感谢你的时间,任何评论都会很有帮助。

1 个答案:

答案 0 :(得分:0)

请在screen3.kv中替换以下内容:

        on_press: app.root.inch_input.text = entry.text

使用:

        on_press: root.manager.ids.tire_setup_screen_id.ids.inch_input.text = entry.text

实施例

main.py

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

from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.graphics import Line

from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput


class Main_Screen(Screen):
    pass


class Input_Number_Inch_Screen(Screen):
    pass


class Input_Screen(Screen):
    pass


class Screen_Management(ScreenManager):
    pass


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


class Screen3App(App):

    def build(self):
        return presentation


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

screen3.kv

#:kivy 1.10.0
#:import FadeTransition kivy.uix.screenmanager.FadeTransition

Screen_Management:
    id: screen_management
    transition: FadeTransition()
    Main_Screen:
        id: main_screen
        name: "main_screen_name"
        manager: screen_management
    Input_Screen:
        id: tire_setup_screen_id
        name: "tire_setup_screen_name"
        manager: screen_management
    Input_Number_Inch_Screen:
        name: "inch_screen"
        manager: screen_management

<Main_Screen>:
    canvas:
        Color:
            rgb: [.30, .30, .30]
        Rectangle:
            pos: self.pos
            size: self.size
    Button:
        background_color: .52, .52, .52, 1
        bold: 1
        color: .0078,.67,.69,1
        size_hint: .2, 1
        pos_hint: {"x": 0, "center_y": .5}
        on_release: root.manager.current = "tire_setup_screen_name"
        text: " INPUTS "
        font_size: 30

# Screen 1: Input Screen
<Input_Screen>:
    canvas:
        Color:
            rgb: [.30, .30, .30]
        Rectangle:
            pos: self.pos
            size: self.size

    GridLayout:
        cols: 2
        pos: (160,150)
        size_hint: (.8, .8)

        Button:
            background_color: .52, .52, .52, 1
            bold: 1
            color: .0078,.67,.69,1
            font_size: 30
            text: "INPUT\n(Inch)"
            size_hint_x: None
            width: 150
            on_release: root.manager.current = "inch_screen"
            # This button will go to the screen2

        Button:
            id: inch_input
            background_color: .52, .52, .52, 1
            bold: 1
            color: .0078,.67,.69,1
            font_size: 100
            text: "THIS IS THE TEXT THAT I WANT TO UPDATE"

# Screen 2: Input Inch Screen Data
<Input_Number_Inch_Screen>:
    canvas:
        Color:
            rgb: [.30, .30, .30]
        Rectangle:
            pos: self.pos
            size: self.size

    GridLayout:
        orientation: 'vertical'
        display: entry
        rows: 6
        padding: 10
        spacing: 10

# This is the TextInput
        BoxLayout:
            TextInput:
                id: entry
                font_size: 75
                multiline: False

# This will be the button that would go back to the screen and update
# the button text with the new text entered in the TextInput
        BoxLayout:
            spacing: 10
            Button:
                background_color: .52, .52, .52, 1
                bold: 1
                color: .0078,.67,.69,1
                font_size: 40
                text:"ENTER"
                on_release: root.manager.current = "tire_setup_screen_name"
                on_press: root.manager.ids.tire_setup_screen_id.ids.inch_input.text = entry.text

Outpu

enter image description here