So im trying to get a variable added to screen and the id fro that is in a widget Im going to add,
so if for example I have this:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
kv = """
Screen:
cal:cal
side: side
GridLayout:
rows: 1
cols:2
spacing:0
GridLayout:
rows: 5
cols:1
Button:
text: "Cube"
# on_press: app.mode = self.text
on_press: app.setMode(self)
Button:
text: "Cuboid"
on_press: app.setMode(self)
Button:
text: "Cylinder"
on_press: app.setMode(self)
Button:
text: "Cone"
on_press: app.setMode(self)
Button:
text: "Sphere"
on_press: app.setMode(self)
FloatLayout:
Label:
text: "The Volume and surface area of a {}:".format(app.mode)
pos_hint: {"x":0.1, "y":0.8}
text_size: self.size
FloatLayout:
id:cal
Label:
text:"Side:"
pos_hint: {"x":1.1, "y":0.7}
text_size: self.size
Label:
text:"Volume:"
pos_hint: {"x":1.1, "y":0.65}
text_size: self.size
Label:
text:"Surface Area:"
pos_hint: {"x":1.1, "y":0.6}
text_size: self.size
TextInput:
size_hint: (.4, None)
height: 26
multiline: False
pos_hint: {"x":1.24, "y":0.7}
id: side
text: app.sideText
on_text_validate: app.Cube_on_side_change(self)
Label:
text: app.volume
pos_hint: {"x":1.27, "y":0.65}
text_size: self.size
Label:
text: app.surface_area
pos_hint: {"x":1.355, "y":0.6}
text_size: self.size
"""
cube = """
FloatLayout:
Label:
text:"Side:"
pos_hint: {"x":1.1, "y":0.7}
text_size: self.size
Label:
text:"Volume:"
pos_hint: {"x":1.1, "y":0.65}
text_size: self.size
Label:
text:"Surface Area:"
pos_hint: {"x":1.1, "y":0.6}
text_size: self.size
TextInput:
size_hint: (.4, None)
height: 26
multiline: False
pos_hint: {"x":1.24, "y":0.7}
id: side
text: app.sideText
on_text_validate: app.Cube_on_side_change(self)
Label:
text: app.volume
pos_hint: {"x":1.27, "y":0.65}
text_size: self.size
Label:
text: app.surface_area
pos_hint: {"x":1.355, "y":0.6}
text_size: self.size"""
cuboid = """
FloatLayout:
id:main
Label:
text:"Length:"
pos_hint: {"x":1.1, "y":0.7}
text_size: self.size
Label:
text:"Breadth:"
pos_hint: {"x":1.1, "y":0.65}
text_size: self.size
Label:
text:"Height:"
pos_hint: {"x":1.1, "y":0.6}
text_size: self.size
TextInput:
size_hint: (.4, None)
height: 26
multiline: False
pos_hint: {"x":1.24, "y":0.7}
id: length
text: app.sideText
on_text_validate: app.Cuboid_on_side_change(self)
TextInput:
size_hint: (.4, None)
height: 26
multiline: False
pos_hint: {"x":1.24, "y":0.65}
id: breadth
text: app.sideText
on_text_validate: app.Cube_on_side_change(self)
TextInput:
size_hint: (.4, None)
height: 26
multiline: False
pos_hint: {"x":1.24, "y":0.6}
id: height
text: app.sideText
on_text_validate: app.Cuboid_on_side_change()
Label:
text: "Volume:"
pos_hint: {"x":1.1, "y":0.55}
text_size: self.size
Label:
text: "Surface Area:"
pos_hint: {"x":1.1, "y":0.5}
text_size: self.size
Label:
text: app.volume
pos_hint: {"x":1.27, "y":0.55}
text_size: self.size
Label:
text: app.surface_area
pos_hint: {"x":1.355, "y":0.5}
text_size: self.size"""
cone = """
FloatLayout:
Label:
text:"Radius:"
pos_hint: {"x":1.1, "y":0.7}
text_size: self.size
Label:
text:"Height:"
pos_hint: {"x":1.1, "y":0.65}
text_size: self.size
TextInput:
size_hint: (.4, None)
height: 26
multiline: False
pos_hint: {"x":1.24, "y":0.7}
id: Radius
text: app.sideText
on_text_validate: app.Cuboid_on_side_change(self)
TextInput:
size_hint: (.4, None)
height: 26
multiline: False
pos_hint: {"x":1.24, "y":0.65}
id: height
text: app.sideText
on_text_validate: app.Cube_on_side_change(self)
Label:
text: "Volume:"
pos_hint: {"x":1.1, "y":0.6}
text_size: self.size
Label:
text: "Surface Area:"
pos_hint: {"x":1.1, "y":0.55}
text_size: self.size
Label:
text: app.volume
pos_hint: {"x":1.27, "y":0.6}
text_size: self.size
Label:
text: app.surface_area
pos_hint: {"x":1.355, "y":0.55}
text_size: self.size
"""
screens = {
"Cube": cube,
"Cuboid": cuboid,
"Cone": cone
}
class MyApp(App):
sideText = StringProperty("")
surface_area = StringProperty("0 cm²")
volume = StringProperty("0 cm³")
mode = StringProperty("Cube")
def build(self):
self.screen = Builder.load_string(kv)
return self.screen
def setMode(self, btn):
self.volume = "0 cm³"
self.surface_area = "0 cm³"
self.mode = btn.text
self.screen.cal.clear_widgets()
self.screen.cal.add_widget(Builder.load_string(screens[self.mode]))
def Cube_on_side_change(self, instance):
try:
value = float(instance.text)
num = True
except ValueError:
# failed to convert
num = False
print("Numbers only idiot.")
def cubeCalc(val):
return {
"volume": val * val * val,
"surface_area": (val * val) * 6
}
if num:
result = cubeCalc(value)
self.volume = "{:.2f} cm³".format(result["volume"])
self.surface_area = "{:.2f} cm²".format(result["surface_area"])
def Cuboid_on_side_change(self):
height = self.screen.cal.ids.main.height.text
try:
value = float(height)
num = True
except ValueError:
# failed to convert
num = False
print("Numbers only idiot.")
def cubeCalc(val):
return {
"volume": val * val * val,
"surface_area": (val * val) * 6
}
if num:
result = cubeCalc(value)
self.volume = "{:.2f} cm³".format(result["volume"])
self.surface_area = "{:.2f} cm²".format(result["surface_area"])
if __name__ == "__main__":
MyApp().run()
on_text_validate of the TextInput with id:height in the string cuboid, I want to get the text from the text input with something like: self.screen.main.height.text, however, to do that I would have to add main:main and height:height to Screen. How would I do this?
I suggest refactoring your code. Rather than re-inventing the capabilities of ScreenManager, just use ScreenManager. In your kv string, replace the FloatLayout with:
ScreenManager:
CubeScreen:
CuboidScreen:
ConeScreen:
and use your additional kv strings (like cube) as the basis for additional rules in your kv string. Something like:
<CubeScreen>:
Label:
text: "The Volume and surface area of a Cube:"
pos_hint: {"x":0.1, "y":0.8}
text_size: self.size
FloatLayout:
Label:
text:"Side:"
pos_hint: {"x":0.1, "y":0.7}
text_size: self.size
.
.
.
And define each Screen class in your py, like:
class CubeScreen(Screen):
def get_cube_area(self):
.
.
.
And in each of the new classes (like CubeScreen) you can define methods for calculating area, volume, etc, and you can access the TextInputs easily using their ids. And your on_text_validate in each Screen can just call the appropriate method from that Screen (like on_text_validate: root.get_cube_area()).
Related
Basically I've created a toolbar with two widgets. One widget (account) should change to a new window when pressed. The other (menu) should open three buttons when it is pressed. When you press on of the three buttons it should also change to a new window.
I don't know how to implement that...
Can anyone help me?
Here's my code:
kv = """
<MenuScreen>:
name: 'mainmenu'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'background.jpg'
Screen:
BoxLayout:
orientation: 'vertical'
MDTopAppBar:
title: 'Fitness App'
left_action_items: [["menu", lambda x: app.navigation_draw()]]
right_action_items: [["account", lambda x: app.callback()]]
elevation:5
MDLabel:
text: 'hello world'
halign: 'center'
BoxLayout:
spacing: 1
orientation: "vertical"
Label:
text: "Welcome back"
font_size: 100
color : 0,0,0,1
Button:
text: 'Get Started'
font_size:100
color: 0, 0, 1, 1
background_color: 0, 0, 0, 1
size_hint: 1, 1
pos_hint: {"x":0, "top":1}
on_release:
root.manager.current = 'screen2'
root.manager.transition.direction = "left"
Button:
text: 'Leave App'
font_size: 100
color: 0, 0, 1, 1
background_color: 0, 0, 0, 1
size_hint: 1, 1
pos_hint: {"x":0, "top":0}
on_release: root.manager.current = app.exit_software()
<Screen2>:
name: 'screen2'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'background.jpg'
BoxLayout:
spacing: 1
orientation: "vertical"
ScrollView:
id: scroll_view
always_overscroll: False
BoxLayout:
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Label:
id: label
text: "Please choose your problem Area"
color : 0,0,0,1
size_hint: None, None
pos_hint: {"x":0, "top":1}
font_size: 50
size: self.texture_size
Button:
text: 'Shoulders'
font_size: 30
color : 0,0,1,1
on_release:
root.manager.current = 'shoulders'
root.manager.transition.direction = "left"
Button:
text: 'Knees'
font_size: 30
color : 0,0,1,1
background_color: 0, 0, 0, 1
size_hint_y: 1
on_release:
root.manager.current = 'knees'
root.manager.transition.direction = "left"
Button:
text: 'Back'
font_size: 30
color : 0,0,1,1
background_color: 0, 0, 0, 1
size_hint_y: 1
on_release:
root.manager.current = 'back'
root.manager.transition.direction = "left"
Button:
text: 'Back to main menu'
font_size: 30
color : 0,0,1,1
background_color: 0, 0, 0, 1
size_hint_y: 1
on_release:
root.manager.current = 'mainmenu'
root.manager.transition.direction = "right"
<Screen_shoulders>:
name: 'shoulders'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'shoulders.png'
BoxLayout:
spacing: 1
orientation: "vertical"
ScrollView:
id: scroll_view
always_overscroll: False
BoxLayout:
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Label:
id: label
text: "Choose an activity"
font_size: 50
color : 0,0,0,1
size: self.texture_size
Button:
text: 'Activity1'
font_size: 50
color : 0,0,0,1
size_hint_y: 0.3
on_release:
root.manager.current = 'shouldersActivity1'
root.manager.transition.direction = "right"
Button:
text: 'Activity2'
font_size: 50
color : 0,0,0,1
size_hint_y: 0.3
on_release: app.add_text()
Button:
text: 'Back to main menu'
font_size: 20
color : 0,0,0,1
size_hint_y: 0.1
on_release:
root.manager.current = 'screen2'
root.manager.transition.direction = "right"
ScreenManager:
id: screen_manager
MenuScreen:
id: menu_scr
Screen2:
id: scr_2
"""
class MenuScreen(Screen):
pass
class Screen2(Screen):
pass
class AddTextApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def build(self):
return Builder.load_string(kv)
def add_text(self):
self.root.ids.scr_2.ids.label.text += f"Some new Text\n"
self.root.ids.scr_2.ids.scroll_view.scroll_y = 0
def callback(self, instance_action_top_appbar_button):
print(instance_action_top_appbar_button)
#staticmethod
def exit_software():
App.get_running_app().stop()
def navigation_draw(self):
# open new window
def callback(self):
# open new window
if __name__ == '__main__':
AddTextApp().run()
I don't know how to make the widgets change window when they're pressed.
Good day everyone! I created a function and this is located on my screen "Contact" called "resetcampos" in my main.py, then I created a Popup in my kv.file. I need to access my function "resetcampos" from my Button that I have in the Popup. what should I write where it's written "on_press: -----" for accesing to my function "resetcampos" that is in my main.py?
py.main
from tokenize import Number
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.core.spelling import Spelling
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
from kivy.properties import StringProperty, NumericProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.scrollview import ScrollView
import smtplib
Config.set('graphics', 'width', 360)
Config.set('graphics', 'height', 640)
kv= Builder.load_file('test6.kv')
class MyLayout(Widget):
pass
def spinner_clicked(self,value):
pass
#sm= ScreenManager()
#screen= Screen(name='second')
#self.ids.Label1.text= f'You selected: {value}'
#Define a RecycleView
class RV (Screen):
pass
#Definine our different screens
class FirstWindow(Screen):
#Variables Shopping Cart
var1= StringProperty("images/shoppingcart.png")
var2= StringProperty("images/shoppingcart2.png")
#Functions Items
def list_tornillo(self):
tornillo= self.ids.input_tornillo.text
#tornillo1= str(float(tornillo) * 0.1) if tornillo else "0.0"
tornillo1= str(round(float(tornillo) * 0.1,2)) if tornillo else "0.0"
tornillo2= tornillo1 + "0"
self.ids.label_tornillo.text= tornillo2
def list_andamio(self):
andamio= self.ids.input_andamio.text
andamiod= andamio.isdigit()
if andamiod == True:
andamio1= str(round(float(andamio) * 180,2)) if andamio else "0.0"
andamio2= andamio1 + "0"
self.ids.label_andamio.text= andamio2
else:
self.ids.input_andamio.text= "0"
def list_vigueta(self):
vigueta= self.ids.input_vigueta.text
vigueta1= str(round(float(vigueta) * 200,2)) if vigueta else "0.0"
vigueta2= vigueta1 + "0"
self.ids.label_vigueta.text= vigueta2
def list_perno(self):
perno= self.ids.input_perno.text
perno1= str(round(float(perno) * 0.20,2)) if perno else "0.0"
perno2= perno1 + "0"
self.ids.label_perno.text= perno2
def list_tuerca(self):
tuerca= self.ids.input_tuerca.text
tuerca1= str(round(float(tuerca) * 0.35,2)) if tuerca else "0.0"
tuerca2= tuerca1 + "0"
self.ids.label_tuerca.text= tuerca2
#self.ids.input_tornillo.text= self.ids.label_tornillo.text
def press_on(self):
self.ids.input_tornillo.text= self.ids.label_tornillo.text
#self.ids.my_label.text= "You Pressed the Button"
#self.ids.Shoppingcart1.source= self.var2
def press_off(self):
self.ids.Shoppingcart1.source= self.var1
class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
pass
class FourthWindow(Screen):
def message(self):
pass
def resetcampos (self):
#self.window_manager.get_screen("Contact").ids.nombre.text = "" - Enter another class
self.ids.nombre.text = ""
self.ids.telefono.text = ""
self.ids.correo.text = ""
self.ids.asunto.text = ""
self.ids.mensaje.text = ""
class WindowManager(ScreenManager):
pass
class AwesomeApp(App):
def build(self):
return MyLayout()
if __name__ == '__main__':
AwesomeApp().run()
Kv.Main
<MyLayout>
BoxLayout:
orientation:"vertical"
size: root.width, root.height
GridLayout:
cols:3
rows:3
size_hint: 1,None
height: 65
Spinner:
id: spinner_id
text: "Menu"
values: ["Catalogue","Buy","Payment Methods", "Contact"]
on_text: root.spinner_clicked(spinner_id.text)
on_text: window_manager.current = self.text # uses an id defined in the WindowManager section
#on_text: root.manager.current= "second" - Just work in Screens
Image:
source: 'images/ferrepluslogo2.png'
Button:
text:"Buy"
WindowManager:
id: window_manager
FirstWindow:
SecondWindow:
ThirdWindow:
FourthWindow:
<FirstWindow>:
name: "Catalogue"
BoxLayout:
orientation:'vertical'
pos_hint: {'top': 1}
size: root.width, root.height
size_hint_y: .55
GridLayout:
size:(root.width, root.height)
size_hint_x: None
size_hint_y: None
cols:4
height: self.minimum_height
Label:
text: "Items"
#font_size: 25
Label:
text: "Number"
Label:
text: "Price"
Label:
text: "Add to Cart"
ScrollView:
size_hint_y: .80
pos_hint: {'x':0, 'y': .11}
do_scroll_x: True
do_scroll_y: True
GridLayout:
size:root.width, root.height
size_hint_x: None
size_hint_y: None
cols:4
#height: self.minimum_height
#Label:
# text: "Items"
#font_size: 25
#Label:
# text: "Number"
#Label:
# text: "Price"
#Label:
# text: "Add to Cart"
Label:
text: "Tornillos"
text_size: self.size
halign: 'center'
valign: 'bottom'
Image:
id: image_tornillos
allow_stretch: True
keep_ratio: True
size_hint: 0.2,0.2
width: 60
height: 80
#pos_hint: {'center_x':1, 'center_y':1}
source: "images/tornillo.png"
center_x: self.parent.center_x
center_y: self.parent.center_y+10
TextInput:
id: input_tornillo
text: ""
halign: "right"
font_size: 18
multiline: True
on_text: root.list_tornillo()
#size_hint: (1, .15)
Label:
id: label_tornillo
text: "0.0"
#text: root.ids.input_tornillo.text*2
Button:
id: shopping1
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
#on_press: root.press_on()
#on_release: root.press_off()
on_press: root.ids.Shoppingcart1.source= root.var2
on_release: root.ids.Shoppingcart1.source= root.var1
Image:
id: Shoppingcart1
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: root.var1
center_x: self.parent.center_x
center_y: self.parent.center_y
#------------------------
Label:
text: "Andamios"
text_size: self.size
halign: 'center'
valign: 'bottom'
Image:
id: image_andamios
allow_stretch: True
keep_ratio: True
size_hint: 0.2,0.2
width: 60
height: 80
#pos_hint: {'center_x':1, 'center_y':1}
source: "images/Andamios.png"
center_x: self.parent.center_x
center_y: self.parent.center_y
TextInput:
id: input_andamio
text: ""
halign: "right"
font_size: 18
#size_hint: (1, .15)
on_text: root.list_andamio()
Label:
id: label_andamio
text: "0.0"
Button:
id: shopping2
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
#on_press: root.press_on()
#on_release: root.press_off()
on_press: root.ids.Shoppingcart2.source= root.var2
on_release: root.ids.Shoppingcart2.source= root.var1
Image:
id: Shoppingcart2
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: "images/shoppingcart.png"
center_x: self.parent.center_x
center_y: self.parent.center_y
#-----------------------------------------------------------
#Image:
# id: image_viguetas
# allow_stretch: True
# keep_ratio: True
# size_hint: 0.2,0.2
# width: 40
# height: 60
# #pos_hint: {'center_x':1, 'center_y':1}
# source: "images/viguetas.jpg"
# center_x: self.parent.center_x
# center_y: self.parent.center_y
Label:
text: "Viguetas"
text_size: self.size
halign: 'center'
valign: 'bottom'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: "images/viguetas.jpg"
TextInput:
id: input_vigueta
text: ""
halign: "right"
font_size: 18
#size_hint: (1, .15)
on_text: root.list_vigueta()
Label:
id: label_vigueta
text: "0.0"
Button:
id: shopping3
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
#on_press: root.press_on()
#on_release: root.press_off()
on_press: root.ids.Shoppingcart3.source= root.var2
on_release: root.ids.Shoppingcart3.source= root.var1
Image:
id: Shoppingcart3
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: "images/shoppingcart.png"
center_x: self.parent.center_x
center_y: self.parent.center_y
#------------------------------------------------------------
#Image:
# id: image_pernos
# allow_stretch: True
# keep_ratio: True
# size_hint: 0.2,0.2
# width: 40
# height: 60
# #pos_hint: {'center_x':1, 'center_y':1}
# source: "images/pernos.png"
# center_x: self.parent.center_x
# center_y: self.parent.center_y
Label:
text: "Pernos"
text_size: self.size
halign: 'center'
valign: 'bottom'
canvas.before:
Rectangle:
pos: self.center_x-39, self.center_y-21
#pos: self.pos
size: 77,50
source: "images/pernos.png"
TextInput:
id: input_perno
text: ""
halign: "right"
font_size: 18
#size_hint: (1, .15)
on_text: root.list_perno()
Label:
text: "0.0"
id: label_perno
Button:
id: shopping4
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
#on_press: root.press_on()
#on_release: root.press_off()
on_press: root.ids.Shoppingcart4.source= root.var2
on_release: root.ids.Shoppingcart4.source= root.var1
Image:
id: Shoppingcart4
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: "images/shoppingcart.png"
center_x: self.parent.center_x
center_y: self.parent.center_y
#------------------------------------------------------------
Label:
text: "Tuercas"
text_size: self.size
halign: 'center'
valign: 'bottom'
canvas.before:
Rectangle:
pos: self.center_x-39, self.center_y-21
#pos: self.pos
size: 77,50
source: "images/tuercas.png"
TextInput:
id: input_tuerca
text: ""
halign: "right"
font_size: 18
#size_hint: (1, .15)
on_text: root.list_tuerca()
Label:
id: label_tuerca
text: "0.0"
Button:
id: shopping5
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
#on_press: root.press_on()
#on_release: root.press_off()
on_press: root.ids.Shoppingcart5.source= root.var2
on_release: root.ids.Shoppingcart5.source= root.var1
Image:
id: Shoppingcart5
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: "images/shoppingcart.png"
center_x: self.parent.center_x
center_y: self.parent.center_y
#----------------------------------------------
Label:
text: "Tejas"
text_size: self.size
halign: 'center'
valign: 'bottom'
canvas.before:
Rectangle:
pos: self.center_x-39, self.center_y-21
#pos: self.pos
size: 77,50
source: "images/Teja.png"
TextInput:
id: input_teja
text: ""
halign: "right"
font_size: 18
#size_hint: (1, .15)
Label:
text: "0.0"
Button:
id: shopping6
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
#on_press: root.press_on()
#on_release: root.press_off()
on_press: root.ids.Shoppingcart6.source= root.var2
on_release: root.ids.Shoppingcart6.source= root.var1
Image:
id: Shoppingcart6
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: "images/shoppingcart.png"
center_x: self.parent.center_x
center_y: self.parent.center_y
#----------------------------------------------
Label:
text: "Tableros"
text_size: self.size
halign: 'center'
valign: 'bottom'
canvas.before:
Rectangle:
pos: self.center_x-39, self.center_y-21
#pos: self.pos
size: 77,50
source: "images/tableros.png"
TextInput:
id: input_tablero
text: ""
halign: "right"
font_size: 18
#size_hint: (1, .15)
Label:
text: "0.0"
Button:
id: shopping7
#text: "Hello"
#font_size: 32
allow_stretch: True
keep_ratio: True
size_hint: .25,.30
pos_hint: {'center_x':0.5, 'center_y':0.5}
background_color: 0,0,0,0
#on_press: root.press_on()
#on_release: root.press_off()
on_press: root.ids.Shoppingcart7.source= root.var2
on_release: root.ids.Shoppingcart7.source= root.var1
Image:
id: Shoppingcart7
allow_stretch: True
keep_ratio: True
size_hint: 0.5,0.5
width: 60
height: 60
pos_hint: {'center_x':0.5, 'center_y':0.5}
source: "images/shoppingcart.png"
center_x: self.parent.center_x
center_y: self.parent.center_y
#-------------------------------------------
Button:
text:""
background_color: 0,0,0,0
Button:
text:""
background_color: 0,0,0,0
Button:
text:""
background_color: 0,0,0,0
Button:
text:""
background_color: 0,0,0,0
#------------------------------------------------------------
<RV>:
ScrollView:
size_hint_y: .73
pos_hint: {'x':0, 'y': .11}
do_scroll_x: True
do_scroll_y: True
GridLayout:
size:(root.width, root.height)
size_hint_x: None
size_hint_y: None
cols:2
height: self.minimum_height
Button:
text:"las"
Button:
text:"cosas"
Button:
text:"de la vida"
Button:
text:"las"
Button:
text:"cosas"
Button:
text:"de la vida"
<SecondWindow>:
name: "Buy"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Buy Screen"
font_size: 32
<ThirdWindow>:
name: "Payment Methods"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Payment Methods Screen"
font_size: 32
<FourthWindow>:
name: "Contact"
BoxLayout:
orientation: "vertical"
#pos_hint: {'top': 1}
#size: root.width, root.height
#size_hint_y: .55
Label:
id:Escribir
text:" Escríbenos:"
pos_hint: {'top': 1}
size: root.width, root.height
size_hint_y: .55
font_size: 20
bold: True
#----
#text_size: self.size
#valign: 'center'
#halign: 'left'
GridLayout:
#padding: 15
cols:2
#size_hint: None,None
#height: 65
#size_hint: .7,1
BoxLayout:
size_hint: 0.5,1
orientation:"vertical"
Label:
text:"Nombre:"
Label:
text:"Telefono:"
Label:
text:"Email:"
Label:
text:"Asunto:"
BoxLayout:
padding: [0,0,10,0] #left, top, right, bottom
orientation:"vertical"
TextInput:
text: ""
id: nombre
TextInput:
text: ""
id: telefono
TextInput:
text: ""
id:correo
TextInput:
text: ""
id: asunto
GridLayout:
cols:2
#size_hint: .7,1
Label:
text:"Mensaje:"
size_hint: .5,1
BoxLayout:
orientation:"vertical"
padding: [0,0,10,0]
TextInput:
id:mensaje
BoxLayout:
orientation:"vertical"
Label:
text: ""
pos_hint: {'top': 1}
size: root.width, root.height
size_hint_y: .55
GridLayout:
cols:3
Label:
text: ""
Button:
text:"Enviar Mensaje"
on_press: root.message()
on_press: Factory.MyPopup().open()
Label:
text: ""
Label:
text:""
Label:
text: " Contacto:"
font_size: 20
bold: True
#pos_hint: {'top': 1}
#size: root.width, root.height
#size_hint_y: .55
GridLayout:
cols: 2
BoxLayout:
orientation:"vertical"
#size_hint: .3,1
Label:
text:" Telefono:"
size_hint: .2,1
Label:
text:"Email:"
BoxLayout:
orientation:"vertical"
padding: [0,0,90,0]
Label:
text:"198797978"
Label:
text:"washington#hotmail.com"
Label:
text: ""
Label:
text: ""
#:import Factory kivy.factory.Factory
<MyPopup#Popup>
auto_dismiss: False
size_hint: 0.8,0.2
pos_hint: {"x": 0.1, "top":0.9}
title: "Atención"
BoxLayout:
orientation:"vertical"
size: root.width, root.height
Label:
text: "Su mensaje fue enviado exitosamente"
font_size: 15
Button:
text:"Cerrar"
font_size: 15
on_release: root.dismiss()
on_press: main.FourthWindow.resetcampos()
#on_press: window_manager.resetcampos()
I try to make a gui for a text based RPG, and now I want to align the text of some labels to the top left, but "halign:" and "valign:" don't seem to do anything.
So how do I align the text inside my labels? Is there something I have done horribly wrong?
This is how the GUI looks at this moment and I marked where the text should be with green arrows:
This is how my .kv file looks:
BoxLayoutExample:
<BackgroundColor#Widget>:
background_color: 1,1,1,1
canvas.before:
Color:
rgba: root.background_color
Rectangle:
size: self.size
pos: self.pos
<BackgroundLabel#Label+BackgroundColor>:
background_color: 0, 0, 0, 0
<BoxLayoutExample>:
orientation: "vertical"
BoxLayout:
orientation:"horizontal"
BackgroundLabel:
background_color: 1,0,0,1
text: "Placeholder Text\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
halign: "left"
valign: "top"
font_size: "10sp"
size_hint: .5, 1
Label:
text: "Placeholder Map/Enemy #TEST TEST TEST TEST TEST \nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
halign: "left"
valign: "top"
size_hint: 1, 1.3
BoxLayout:
orientation:"vertical"
size_hint: .5, 1
Label:
background_color: 1,1,1,.5
text: "Placeholder Stats\nHP\nMP\nDMG\nXP\nLVL"
halign: "left"
valign: "top"
size_hint: 1, .3
ScrollView:
size_hint: 1, .7
scroll_distance: 100
Label:
text: "Placeholder Inventory\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
size_hint: None, None
size: self.texture_size
halign: "left"
valign: "top"
BoxLayout:
orientation: "horizontal"
size: "60dp","60dp"
size_hint: None,None
Label:
size: "60dp","60dp"
size_hint: None,None
Button:
text: "go\nnorth"
size: "60dp","60dp"
size_hint: None,None
Label:
size: "60dp","60dp"
size_hint: None,None
BoxLayout:
orientation: "horizontal"
size: "180dp","60dp"
#pos: "0dp","60dp"
size_hint: None,None
Button:
text: "go\nwest"
size: "60dp","60dp"
#pos: "0dp","60dp"
size_hint: None,None
pos_hint: {"x":0}
Button:
text: "go\nsouth"
size: "60dp","60dp"
size_hint: None,None
Button:
text: "go\neast"
size: "60dp","60dp"
#pos: "0dp","60dp"
size_hint: None,None
Label:
size: "60dp","60dp"
size_hint: None,None
Button:
text: "use\nitem"
size: "60dp","60dp"
size_hint: None,None
Button:
text: "equip\ngear"
size: "60dp","60dp"
size_hint: None,None
Button:
text: "unequip\ngear"
size: "60dp","60dp"
size_hint: None,None
Thanks for your help, I really appreciate it.
You need to add this argument in your label:
text_size: self.size
Then the halign and valign arguments will work properly, for example:
Label:
text: "Placeholder Map/Enemy #TEST TEST TEST TEST TEST \nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST\nTEST"
text_size: self.size
halign: "left"
valign: "top"
size_hint: 1, 1.3
More details in the official kivy documentation.
I am developing an application in Python and with the kivy library. In the past version of the App, it had only one window, and the code worked well, however, I need to implement multi-windows, and the problem is that I can't reference the ids inside the windows I created.
main.py
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, ListProperty
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.widget import Widget
#-----------------------------------------------------------------------------------------------------------------------
Window.softinput_mode = 'below_target'
Window.clearcolor = [1, 1, 0.9, 0.9]
class FirstWindow(Screen):
pass
class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class CustomPopup(Popup):
pass
class MainApp(App):
texture = ObjectProperty()
def open_popup(self):
the_popup = CustomPopup()
the_popup.open()
def build(self):
self.title = 'Myapp'
self.texture = Image(source = 'wave.png').texture
# self.first_window.ids.x.text
# self.screen_manager = ScreenManager()
# self.FirstWindow = FirstWindow()
# screen = Screen(name = 'first')
# screen.add_widget(self.FirstWindow)
# self.screen_manager.add_widget(screen)
# primeiro = self.screen_manager.get_screen("first")
def calcular(self, *args):
#>>>>> The problem happens here, I believe that the code can no longer get the data from the ids because
they are no longer in root. and I don't know how to reference them. The problem didn't happen before
because the kivy code had only one window <<<<<<<<<<<
s_x = self.root.ids.x.text
s_y = self.root.ids.y.text
s_z = self.root.ids.z.text
s_rpa = self.root.ids.rpa.text
s_rt = self.root.ids.rt.text
s_rpi = self.root.ids.rpi.text
s_t = self.root.ids.t.text
s_ii = self.root.ids.ii.text
s_ie = self.root.ids.ie.text
s_ac = self.root.ids.ac.text
#-----------------------------------------------------------------------------------------------------------------------
# Conditionals and variables:
if (s_x == ''):
s_x = 6
if (s_y == ''):
s_y = 6
if (s_z == ''):
s_z = 3
if (s_ie == ''):
s_ie = 20000
if (s_ii == ''):
s_ii = 300
if (s_t == ''):
s_t = 0.88
if (s_rpi == ''):
s_rpi = 0.3
if (s_rt == ''):
s_rt = 0.7
if (s_rpa == ''):
s_rpa = 0.5
if (s_ac == ''):
s_ac = 90
x = float(s_x)
y = float(s_y)
z = float(s_z)
rpi = float(s_rpi)
rt = float(s_rt)
rpa = float(s_rpa)
t = float(s_t)
ac = float(s_ac)
ii = float(s_ii)
ie = float(s_ie)
#-----------------------------------------------------------------------------------------------------------------------
# Equacions:
apa = 2*((x*z)+(y*z))
api = x * y
at = x * y
a = apa + api + at
r = ((rpa * apa) + (rpi * api) + (rt * at)) / a
fld = (ii/ie)*100
w = (fld*a*(1-(r ** 2))) / (t*ac)
w = round(w, 2)
w = str(w)
w = w.replace(".", ",")
w = w +" m²"
print(w)
#-----------------------------------------------------------------------------------------------------------------------
# >>>>>>>>The problem with ids also happens here <<<<<<<<<<
# Button calculate:
if (( t<=0 or t>1 ) or ( rpa<=0 or rpa>=1 ) or ( rpi<=0 or rpi >=1 ) or ( rt<=0 or rt>=1 ) or (ac<=0 or ac>180)):
the_popup = Popup(title='Erro', content=Label(id='_result', text='Valor fornecido invalido.'),size_hint=(.5, .2), separator_color=[1, 1, 0.6, 0.8])
the_popup.open()
else:
self.root.ids.resultado.text = w
self.root.ids.resultado.opacity = 1
if (ac > 90):
self.root.ids.tipojanela.text = 'Janela azimutal'
self.root.ids.tipojanela.opacity = 1
else:
self.root.ids.tipojanela.opacity = 0
#-----------------------------------------------------------------------------------------------------------------------
def exit(self):
App.get_running_app().stop()
aplicativo = MainApp()
aplicativo.run()
main.kv
<Button>:
background_down: ''
<CustomPopup>:
size_hint: 1,.7
auto_dismiss: False
title: 'Ajuda'
separator_color: 1,1,0.6,0.8
FloatLayout:
id: primeiro_float
Label:
text: '- O valor para a transmitância deve ser maior que 0 e menor ou igual a 1 \n - Os valores para as refletâncias devem estar entre 0 e 1 \n - O ângulo de céu visível deve ser maior que 0° e menor ou igual a 180°"'
font_size: 25
text_size: self.size
halign: 'center'
valign: 'middle'
size: primeiro_float.size
pos: primeiro_float.pos
Button:
size_hint: None, None
width: self.texture_size[0] - dp(10)
height: self.texture_size[0] - dp(10)
pos_hint: {'center_x': .5, 'y': .05}
halign:'right'
valign: 'top'
text: 'fechar'
color: 0,0,0,0
border: 0,0,0,0
background_normal: 'close.png'
background_down: 'close.png'
on_press: root.dismiss()
WindowManager:
FirstWindow:
SecondWindow:
ThirdWindow:
<FirstWindow>:
name: 'first'
id: first_window
FloatLayout:
canvas:
Rectangle:
pos: self.pos
size: self.size
texture: app.texture
GridLayout:
cols:1
ActionBar:
background_color: 1,1,1,1
background_image: ''
ActionView:
use_separator: True
ActionPrevious:
background_image:''
background_down: ''
background_normal: ''
background_color: ''
source: ''
app_icon: 'fld.png'
previous_image: ''
color: 0,0,0,1
ActionGroup:
background_normal: 'list1.png'
background_down: 'list2.png'
source:''
mode: 'spinner'
size_hint: None, None
width: '50sp'
height: '50sp'
border: 0,0,0,0
ActionButton:
background_color: 0.3,1,0.6,1
source:''
text: 'Ajuda'
on_press: app.open_popup()
id:ajuda
ActionButton:
background_color: 0.3,1,0.6,1
background_normal: ''
text: 'Sair'
id:sair
on_release: app.exit()
Label:
canvas.before:
Color:
rgba: 1,1,0.6,0.8
Rectangle:
pos: self.pos
size: self.size
color: 0,0,0,1
size_hint_y: None
height: self.font_size + dp(10)
text: 'Ambiente'
halign: 'center'
valign: 'middle'
GridLayout:
size_hint: None, None
width: root.width
height: self.minimum_height
padding: 10, 10, 10, 10
spacing: dp(10)
cols:4
Label:
text: 'Comprimento (m)'
color: 0.5,0.5,0.5,1
text_size: self.size
font_size: '11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size + dp(20)
TextInput:
text: '6'
id: x
cursor_color: 0,0,0,1
size_hint_y: None
height: self.font_size + dp(15)
input_filter:'float'
multiline: False
write_tab: False
on_text_validate: y.focus = True
Label:
text: 'Refletância do piso ]0;1['
color: 0.5,0.5,0.5,1
text_size: self.size
font_size: '11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size + dp(20)
TextInput:
text: '0.3'
id: rpi
cursor_color: 0,0,0,1
input_filter:'float'
multiline: False
write_tab: False
on_text_validate: rt.focus = True
Label:
text: 'Largura (m)'
color: 0.5,0.5,0.5,1
text_size: self.size
font_size:'11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size + dp(20)
TextInput:
text: '6'
id: y
cursor_color: 0,0,0,1
input_filter:'float'
multiline: False
write_tab: False
on_text_validate: z.focus = True
Label:
text: 'Refletância do teto ]0;1['
color: 0.5,0.5,0.5,1
text_size: self.size
font_size: '11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size + dp(20)
TextInput:
text: '0.7'
id: rt
cursor_color: 0,0,0,1
multiline: False
input_filter:'float'
multiline: False
write_tab: False
on_text_validate: rpa.focus = True
Label:
text: 'Pé-direito (m)'
color: 0.5,0.5,0.5,1
text_size: self.size
font_size: '11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size + dp(20)
TextInput:
text: '3'
id: z
cursor_color: 0,0,0,1
input_filter:'float'
multiline: False
write_tab: False
on_text_validate: rpi.focus = True
Label:
text: 'Refletância das paredes ]0;1['
color: 0.5,0.5,0.5,1
text_size: self.size
font_size: '11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size + dp(20)
on_text_validate:
TextInput:
text: '0.5'
id: rpa
cursor_color: 0,0,0,1
input_filter:'float'
multiline: False
write_tab: False
on_text_validate: t.focus = True
Label:
canvas.before:
Color:
rgba: 1,1,0.6,0.8
Rectangle:
pos: self.pos
size: self.size
color: 0,0,0,1
size_hint_y: None
height: self.font_size + dp(10)
text: 'Abertura'
halign: 'center'
valign: 'middle'
GridLayout:
size_hint: None, None
width: root.width
height: self.minimum_height
padding: 10, 10, 10, 10
spacing: dp(10)
cols:2
Label:
text: 'Transmitância ]0;1]'
color: 0.5,0.5,0.5,1
text_size: self.size
font_size: '11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size + dp(20)
TextInput:
text: '0.88'
id: t
cursor_color: 0,0,0,1
input_filter:'float'
multiline: False
write_tab: False
on_text_validate: ac.focus = True
Label:
canvas.before:
Color:
rgba: 1,1,0.6,0.8
Rectangle:
pos: self.pos
size: self.size
color: 0,0,0,1
size_hint_y: None
height: self.font_size + dp(10)
text: 'Obstrução'
halign: 'center'
valign: 'middle'
GridLayout:
size_hint: None, None
width: root.width
height: self.minimum_height
padding: 10, 10, 10, 10
spacing: dp(10)
cols:2
Label:
text: 'Ângulo de céu visível (°)'
color: 0.5,0.5,0.5,1
text_size: self.size
font_size: '11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size + dp(20)
TextInput:
text: '90'
id: ac
cursor_color: 0,0,0,1
multiline: False
write_tab: False
input_filter:'float'
on_text_validate: ie.focus = True
Label:
canvas.before:
Color:
rgba: 1,1,0.6,0.8
Rectangle:
pos: self.pos
size: self.size
color: 0,0,0,1
size_hint_y: None
height: self.font_size + dp(10)
text: 'Iluminâncias'
halign: 'center'
valign: 'middle'
GridLayout:
size_hint: None, None
width: root.width
height: self.minimum_height
padding: 10, 10, 10, 10
spacing: dp(10)
cols:2
Label:
text: 'Iluminância externa difusa (lx)'
color: 0.5,0.5,0.5,1
text_size: self.size
font_size: '11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size +dp(20)
TextInput:
text: '20000'
id: ie
cursor_color: 0,0,0,1
multiline: False
write_tab: False
input_filter: 'float'
on_text_validate: ii.focus = True
Label:
text: 'Iluminância interna média no Plano de Trabalho (lx)'
color: 0.5,0.5,0.5,1
text_size: self.size
font_size: '11sp'
halign: 'center'
valign: 'middle'
size_hint_y: None
height: self.font_size +dp(20)
TextInput:
text: '300'
id: ii
cursor_color: 0,0,0,1
multiline: False
write_tab: False
input_filter: 'float'
on_text_validate: bt.focus = True
FloatLayout:
Button:
pos_hint: {'center_x': .5, 'center_y': .6}
width: '220sp'
height: '45sp'
size_hint: None, None
color: 0,0,0,1
background_normal: 'calcu1.png'
background_down: 'calcu2.png'
#background_color: 1,1,0.9,0.9
border: 0,0,0,0
id: bt
text: u'Calcular a área da janela'
font_size: '17sp'
on_release: app.calcular()
FloatLayout:
Label:
text: 'v1.0.3 - Beta'
pos_hint: {'center_x': 0.85, 'center_y': .2}
color: 0,0,0,1
font_size: '14sp'
Button:
size_hint: None, None
width: '25sp'
height: '25sp'
pos_hint: {'center_x': .94, 'y': .4}
#halign:'right'
#valign: 'top'
border: 0,0,0,0
background_normal: 'int1.png'
background_down: 'int2.png'
on_release:
app.root.current = 'second'
root.manager.transition.direction = 'left'
Button:
size_hint: None, None
width: '25sp'
height: '25sp'
pos_hint: {'center_x': .94, 'y': .9}
#halign:'right'
#valign: 'top'
border: 0,0,0,0
background_normal: 'cont.png'
background_down: 'cont.png'
on_release:
app.root.current = 'third'
root.manager.transition.direction = 'left'
Label:
id: resultado
opacity: 0
pos_hint: {'center_x': .5, 'center_y': .9}
width: self.texture_size[0] + dp(20)
size_hint_x: None
size_hint_y: .4
text: ''
color: 0,0,0,1
canvas.before:
Color:
rgba: 1,1,0.6,0.8
RoundedRectangle:
radius:{(10.0,), (10.0,), (10.0,), (10.0,)}
size: self.size
pos: self.pos
Label:
id: tipojanela
opacity: 0
pos_hint: {'center_x': .5, 'center_y': .35}
width: self.texture_size[0] + dp(20)
size_hint_x: None
size_hint_y: .4
text: ''
color: 0,0,0,1
canvas.before:
Color:
rgba: 1,1,0.6,0.8
RoundedRectangle:
radius:{(10.0,), (10.0,), (10.0,), (10.0,)}
size: self.size
pos: self.pos
<SecondWindow>:
name: 'second'
id: second_window
BoxLayout:
orientation: 'vertical'
size: root.width, root.height
Label:
text: 'Segunda janela'
font_size: 32
Button:
text: 'Voltar pra primeira janela'
font_size: 32
on_release:
app.root.current = 'first'
root.manager.transition.direction = 'right'
<ThirdWindow>:
name: 'third'
id: second_window
BoxLayout:
orientation: 'vertical'
size: root.width, root.height
Label:
text: 'terceira janela'
font_size: 32
Button:
text: 'Voltar pra primeira janela'
font_size: 32
on_release:
app.root.current = 'first'
root.manager.transition.direction = 'right'
Error on:
on_release: app.calcular()
Error:
File "C:\Users\Gilson Carvalho\Desktop\TropWin 01-04-2021\Arriscando Tudo 2\main.py", line 63, in calcular
s_x = self.root.ids.x.text
File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
Could anyone help me with this?
You are correct, the ids are no longer in self.root, because that is now the ScreenManager (WindowManager). The way to access those ids now is through the get_screen() method of ScreenManager. For example, replace:
s_x = self.root.ids.x.text
with:
s_x = self.root.get_screen('first').ids.x.text
I'm quite new to Kivy (started yesterday) and am trying to create a simple enough app that has input boxes for several values of height and area to calculate volumes. I cant find any working methods of doing this. So far all I have got is this:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string("""
<MenuScreen>:
FloatLayout:
Label:
text: 'Please Select an Area to Work With:'
pos: 230, 490
size_hint: .15, .05
font_size: 23
Button:
text: "A"
pos: 230, 100
size_hint: .4,.1
font_size: 23
on_press: root.manager.current = 'settings'
Button:
text: "B"
pos: 230, 210
size_hint: .4,.1
font_size: 23
on_press: root.manager.current = 'settings'
Button:
text: "C"
pos: 230, 320
size_hint: .4,.1
font_size: 23
on_press: root.manager.current = 'settings'
Button:
text: "D"
pos: 230, 420
size_hint: .4,.1
font_size: 23
on_press: root.manager.current = 'settings'
<SettingsScreen>:
GridLayout:
Label:
text: 'Room 1'
pos: 6, 460
size_hint: .15, .05
font_size: 23
Label:
text: 'Room 2'
pos: 6, 420
size_hint: .15, .05
font_size: 23
Label:
text: 'Room 3'
pos: 6, 380
size_hint: .15, .05
font_size: 23
Label:
text: 'Room 4'
pos: 6, 340
size_hint: .15, .05
font_size: 23
Label:
text: 'Room 5'
pos: 6, 300
size_hint: .15, .05
font_size: 23
Label:
text: 'Room 6'
pos: 6, 260
size_hint: .15, .05
font_size: 23
TextInput:
text1: "0"
multiline: False
pos: 200,420
font_size: 23
on_text: viewer.text = self.text1
size_hint: .001, .001
TextInput:
text2: "0"
multiline: False
pos: 200, 420
font_size: 23
on_text: viewer.text = self.text2
size_hint: .001, .001
TextInput:
text3: "0"
multiline: False
pos: 200,380
font_size: 23
on_text: viewer.text = self.text3
size_hint: .001, .001
TextInput:
text4: "0"
multiline: False
pos: 200,340
font_size: 23
on_text: viewer.text = self.text4
size_hint: .001, .001
TextInput:
text5: "0"
multiline: False
pos: 200,300
font_size: 23
on_text: viewer.text = self.text5
size_hint: .001, .001
TextInput:
text6: "0"
multiline: False
pos: 200,240
font_size: 23
on_text: viewer.text = self.text6
size_hint: .001, .001
""")
# Declare both screen
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
Im planning to have the second page unique for each button pressed, but want Any help would be appreciated.
I hope I got to understand your question. You are asking for methods to do this. The plain answer is that you have the whole Python to do all the operations you want. Kivy is a library that provides a lot of components to design GUI. It also provides you with a language that is parsed by the Builder.load_string(). Here is an example that might be more or less what you are looking for. It is sort of a calculator on the first screen. The second screen is empty and you can move between them with the bottom buttons.
The calculator on the first screen has two InputTexts and two buttons (Sum and Product). The Sum Button has the implementation of a sum directly on the kivy language. The Product Button calls a method in the root (an instance of Calc). The method doesn't exist by itself. I created in the python code below the kivy section. There is some comments on the code for what I am saying.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
Builder.load_string("""
<Calc>:
# This are attributes of the class Calc now
a: _a
b: _b
result: _result
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
ScreenManager:
size_hint: 1, .9
id: _screen_manager
Screen:
name: 'screen1'
GridLayout:
cols:1
TextInput:
id: _a
text: '3'
TextInput:
id: _b
text: '5'
Label:
id: _result
Button:
text: 'sum'
# You can do the opertion directly
on_press: _result.text = str(int(_a.text) + int(_b.text))
Button:
text: 'product'
# Or you can call a method from the root class (instance of calc)
on_press: root.product(*args)
Screen:
name: 'screen2'
Label:
text: 'The second screen'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'bottom'
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .1
Button:
text: 'Go to Screen 1'
on_press: _screen_manager.current = 'screen1'
Button:
text: 'Go to Screen 2'
on_press: _screen_manager.current = 'screen2'""")
class Calc(FloatLayout):
# define the multiplication of a function
def product(self, instance):
# self.result, self.a and self.b where defined explicitely in the kv
self.result.text = str(int(self.a.text) * int(self.b.text))
class TestApp(App):
def build(self):
return Calc()
if __name__ == '__main__':
TestApp().run()