I'm using 2 files with this process, the first being main.py
and the second ScreenManagement.kv
I've tried so many different "solutions" from Stack and other sites but nothing is working for me. I want to be able to click a button, and the button will take me to the next screen but also set the label in that screen to the text of the button from the first screen.
I labeled the button, label, and screens in question with comments. Thank you in advance for your help as this has been an ongoing problem :)
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'
You can use method get_screen
or attr. current_screen
to access any screen added to ScreenManager
(your root
widget) as,
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
Also I think you don't need the following lines of code as you already defined the root
in method build
.
<ScreenManager> # Naming dynamic class with default class name is discouraged.
transition: NoTransition()
MainOverview:
id: mainOverview
MachineStatusPage:
selectedMachine: mainOverview.selectedMachine