![](/img/trans.png)
[英]How to change a Label text on another screen by pressing a Button Python Kivy
[英]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 和其他站点的许多不同的“解决方案”,但没有什么对我有用。 我希望能够单击一个按钮,该按钮会将我带到下一个屏幕,但还将该屏幕中的标签设置为第一个屏幕中按钮的文本。
我用评论标记了有问题的按钮、标签和屏幕。 提前感谢您的帮助,因为这是一个持续存在的问题:)
主文件
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()
屏幕管理.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
或 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.