使用 Kivy 屏幕,如何根据屏幕 1 上按下的按钮的文本更改屏幕 2 中的标签?
Using Kivy screens, how do I change the label from screen 2 based on the pressed button's text on screen 1?
我在这个过程中使用了 2 个文件,第一个是 main.py
,第二个是 ScreenManagement.kv
我已经尝试了来自 Stack 和其他网站的许多不同的“解决方案”,但没有任何效果为了我。我希望能够单击一个按钮,该按钮会将我带到下一个屏幕,但还将该屏幕中的标签设置为第一个屏幕中按钮的文本。
我用评论标记了有问题的按钮、标签和屏幕。预先感谢您的帮助,因为这是一个持续存在的问题:)
main.py
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
#: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'
您可以使用方法 get_screen
or attr. current_screen
访问添加到 ScreenManager
(您的 root
小部件)的任何屏幕,因为,
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
此外,我认为您不需要以下代码行,因为您已经在方法 build
中定义了 root
。
<ScreenManager> # Naming dynamic class with default class name is discouraged.
transition: NoTransition()
MainOverview:
id: mainOverview
MachineStatusPage:
selectedMachine: mainOverview.selectedMachine
我在这个过程中使用了 2 个文件,第一个是 main.py
,第二个是 ScreenManagement.kv
我已经尝试了来自 Stack 和其他网站的许多不同的“解决方案”,但没有任何效果为了我。我希望能够单击一个按钮,该按钮会将我带到下一个屏幕,但还将该屏幕中的标签设置为第一个屏幕中按钮的文本。
我用评论标记了有问题的按钮、标签和屏幕。预先感谢您的帮助,因为这是一个持续存在的问题:)
main.py
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
#: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'
您可以使用方法 get_screen
or attr. current_screen
访问添加到 ScreenManager
(您的 root
小部件)的任何屏幕,因为,
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
此外,我认为您不需要以下代码行,因为您已经在方法 build
中定义了 root
。
<ScreenManager> # Naming dynamic class with default class name is discouraged.
transition: NoTransition()
MainOverview:
id: mainOverview
MachineStatusPage:
selectedMachine: mainOverview.selectedMachine