首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Kivy屏幕,如何根据按下的屏幕1上的文本更改屏幕2中的标签?

使用Kivy屏幕,如何根据按下的屏幕1上的文本更改屏幕2中的标签?
EN

Stack Overflow用户
提问于 2022-05-15 18:57:57
回答 1查看 32关注 0票数 0

在这个过程中,我使用了两个文件,第一个是main.py,第二个是ScreenManagement.kv,我尝试了许多来自Stack和其他站点的不同的“解决方案”,但是没有什么对我有用。我想要能够点击一个按钮,该按钮将带我到下一个屏幕,但也设置标签在该屏幕上的文本按钮从第一个屏幕。

我在按钮、标签和屏幕上加上了注释。提前感谢您的帮助,因为这是一个持续的问题:)

main.py

代码语言:javascript
复制
from kivy.app import App
from kivy.graphics import Color, Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.colorpicker import ColorPicker
from py.fileImport import readMachine
from kivy.lang import Builder
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen, ScreenManager, NoTransition
from kivy.properties import StringProperty

Builder.load_file("kv/ScreenManagement.kv")

class MainOverview(Screen): #This is the first screen in question
    selectedMachine = StringProperty("")
class MachineStatusPage(Screen): #This is the second screen in question
    selectedMachine = StringProperty("test")
class AddMachinePage(Screen):
    pass
class AddJobPage(Screen):
    pass

class MainApp(App):   

    def build(self):
        
        self.root = root = ScreenManager()
        root.bind(size=self._update_rect, pos=self._update_rect)
        screen1 = MainOverview(name='MainOverview')
        screen2 = MachineStatusPage(name='MachineStatusPage')
        screen3 = AddMachinePage(name='AddMachinePage')
        screen4 = AddJobPage(name='AddJobPage')
        root.add_widget(screen1)
        root.add_widget(screen2)
        root.add_widget(screen3)
        root.add_widget(screen4)
        

        with root.canvas.before:
            Color(0, 1, 1, .6)  # colors range from 0-1 not 0-255
            self.rect = Rectangle(size=root.size, pos=root.pos)
        return root

    def _update_rect(self, instance, value):
        self.rect.pos = instance.pos
        self.rect.size = instance.size

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

ScreenManagement.kv

代码语言:javascript
复制
#:kivy 1.10.1
#: import NoTransition kivy.uix.screenmanager.NoTransition

<AnchorGridCell@AnchorLayout>:
    anchor_x: 'center'
    anchor_y: 'center'
<SubmitButton@Button>:
    text: 'Submit'
    font_size: self.width / 8
    size_hint: .4, .1
<CancelButton@Button>:
    text: 'Cancel'
    font_size: self.width / 8
    size_hint: .4, .1
<MachineButton@Button>:
    font_size: self.width / 20
    text_size: self.size
    halign: 'center'
    valign: 'center'
<MachineStatusButton@Button>:
    font_size: self.width / 9
    text_size: self.size
    halign: 'center'
    valign: 'center'
    size_hint: .3, .06
<XButton@Button>:
    text: 'X'
    size_hint: None, None
<InputLabel@Label>:
    font_size: self.width / 8
    text_size: self.size
    halign: 'left'
    valign: 'center'
<OutputLabel@Label>:
    font_size: self.width / 8
    text_size: self.size
    halign: 'right'
    valign: 'center'
<TextCollection@TextInput>:
    multiline: False
    size_hint: 1, .7
    font_size: self.width / 7
    
<ScreenManager>
    transition: NoTransition()
    MainOverview:
        id: mainOverview
    MachineStatusPage:
        selectedMachine: mainOverview.selectedMachine
<MainOverview>:
    FloatLayout:
        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'top'
            GridLayout:
                cols: 1
                row_force_default: True
                row_default_height: self.width / 13
                padding: self.width / 15
                AnchorLayout:
                    anchor_x: 'center'
                    anchor_y: 'bottom'
                    Label:
                        text: 'All Currently Added Machines'
                        text_size: self.size
                        font_size: self.width / 16
                        halign: 'center'
                        valign: 'bottom'
                AnchorLayout:
                    anchor_x: 'center'
                    anchor_y: 'top'
                    GridLayout:
                        cols: 1
                        row_force_default: True
                        row_default_height: self.width / 8
                        col_default_width: self.width / 4
                        padding: self.width / 25
                        spacing: self.width / 25, self.width / 50
                        AnchorGridCell:

###############################################################################

                            MachineButton: #This button is the one in question
                                id: machine1
                                text: "Tsugami 5"
                                on_press: root.selectedMachine = machine1.text; root.manager.current = 'MachineStatusPage'

###############################################################################

                        AnchorGridCell:
                            MachineButton:
                                text: 'Tsugami 6'
                                on_press: root.manager.current = 'MachineStatusPage'
                        AnchorGridCell:
                            MachineButton:
                                text: 'Tsugami 7'
                                on_press: root.manager.current = 'MachineStatusPage'
                        AnchorGridCell:
                            MachineButton:
                                text: 'Tsugami 8'
                                on_press: root.manager.current = 'MachineStatusPage'
            AnchorLayout:
                anchor_x: 'right'
                anchor_y: 'bottom'
                MachineButton:
                    text: 'Add New Machine'
                    font_size: self.width / 8
                    size_hint: .4, .1
                    on_press: root.manager.current = 'AddMachinePage'
                                    
                                
<MachineStatusPage>:
    FloatLayout:
        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'top'
            GridLayout:
                cols: 1
                rows: 2
                row_force_default: True
                row_default_height: self.width / 13
                padding: self.width / 8
                AnchorLayout:
                    anchor_x: 'center'
                    anchor_y: 'bottom'

#####################################################################

                    Label: #This label is the label in question
                        id: selectedMachine
                        text: root.selectedMachine
                        text_size: self.size
                        font_size: self.width / 12
                        halign: 'center'
                        valign: 'bottom'

#####################################################################

                AnchorLayout:
                    anchor_x: 'center'
                    anchor_y: 'top'
                    GridLayout:
                        cols: 2
                        row_force_default: True
                        row_default_height: self.width / 8
                        col_default_width: self.width / 4
                        padding: self.width / 25
                        spacing: self.width / 25, self.width / 50
                        AnchorGridCell:
                            InputLabel:
                                text: 'Current Job: '
                        AnchorGridCell:
                            OutputLabel:
                                text: '414-44-1'
                        AnchorGridCell:
                            InputLabel:
                                text: 'Parts Left: '
                        AnchorGridCell:
                            OutputLabel:
                                text: '250'
                        AnchorGridCell:
                            InputLabel:
                                text: 'Time Left: '
                        AnchorGridCell:
                            OutputLabel:
                                text: '3 Days'
            AnchorLayout:
                anchor_x: 'left'
                anchor_y: 'bottom'
                Button:
                    text: 'Go Back'
                    font_size: self.width / 8
                    size_hint: .4, .1
                    on_press: root.manager.current = 'MainOverview'
            AnchorLayout:
                anchor_x: 'right'
                anchor_y: 'bottom'
                Button:
                    text: 'Add New Job'
                    font_size: self.width / 8
                    size_hint: .4, .1
                    on_press: root.manager.current = 'AddJobPage'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-15 20:16:39

您可以使用方法get_screen或attr。current_screen来访问添加到ScreenManager (您的root小部件)的任何屏幕,

代码语言:javascript
复制
                            MachineButton: #This button is the one in question
                                id: machine1
                                text: "Tsugami 5"
                                on_press:
                                    root.manager.current = 'MachineStatusPage'
                                    root.manager.current_screen.selectedMachine = self.text
                                    # Or,
                                    # root.manager.get_screen('MachineStatusPage').selectedMachine = self.text

此外,我认为您不需要下面的代码行,因为您已经在方法root中定义了build

代码语言:javascript
复制
<ScreenManager> # Naming dynamic class with default class name is discouraged.
    transition: NoTransition()
    MainOverview:
        id: mainOverview
    MachineStatusPage:
        selectedMachine: mainOverview.selectedMachine
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72251339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档