Related
I have a TextInput and a button, and my aim is to be able to select some text from the TextInput and then push a button which calls a function that prints: selection_text, selection_to, and selection_from.
However, because the TextInput is not in focus when the button is pushed, the selection is lost and the function returns nothing AND/OR incorrect values.
I have seen this line of code in similar questions but I have not been able to figure out what it is meant to do, via messing with the code or looking at the TextInput docs, since I have only seen it called from within an 'on_focus' function
Clock.schedule_once(lambda dt: new_post_input.selection_text)
I have not been able to recreate the issue in an example program yet since it works when I have taken the widgets into their own program
main.py
from kivy.app import App
from kivy.properties import BooleanProperty, StringProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.modules import inspector
from kivy.core.window import Window
class NewPost(FloatLayout):
buttons_disabled = BooleanProperty(False)
cancle_button_disabled = BooleanProperty(True)
preview_text = StringProperty()
def make_bold(self, new_post_input, selected):
print('Selected = ' + selected)
print('Button has been pushed')
def preview_post(self, Button, cancleButton, TextInput, Label):
#self.preview_text = ('Hello\n' + TextInput.text)
self.buttons_disabled = True
self.cancle_button_disabled = False
Label.opacity = 1
Label.pos_hint = {'center_x': 0.5, 'center_y': 0.5}
TextInput.opacity = 0
cancleButton.opacity = 1
cancleButton.pos_hint = {'center_x': 0.5, 'center_y': 0.5}
Button.opacity = 0
Button.pos_hint = {'center_x': 0.5, 'center_y': -1}
#print(TextInput.text)
def back_from_preview(self, Button, previewButton, TextInput, Label):
self.buttons_disable = False
self.cancle_button_disabled = True
Label.opacity = 0
Label.pos_hint = {'center_x': 0.5, 'center_y': -1}
TextInput.opacity = 1
Button.opacity = 0
Button.pos_hint = {'center_x': 0.5, 'center_y': -1}
previewButton.opacity = 1
previewButton.pos_hint = {'center_x': 0.5, 'center_y': 0.5}
class SelectionApp(App):
def build(self):
inspector.create_inspector(Window, SelectionApp)
SelectionApp().run()
selection.kv
PageLayout:
Label:
text: 'Swipe to the next page'
font_size: dp(50)
Label:
text: 'And again'
canvas.before:
Color:
rgba: (0,.8,.8,1)
RoundedRectangle:
size: self.size
radius: [dp(6),]
pos: self.x, self.y
NewPost:
id: new_post
canvas.after:
Color:
rgba: 0,1,0,1
Line:
width: dp(1.6)
rounded_rectangle:(self.x, self.y, self.width, self.height, dp(5))
BoxLayout:
id: main_new_post
orientation: 'vertical'
padding: '10dp', '16dp'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
canvas.before:
Color:
rgba: 0, 0, 0, 1
Rectangle:
pos: self.pos
size: self.size
AnchorLayout:
size_hint: 1, .05
anchor_x: 'right'
anchor_y: 'center'
Label:
text: 'New Post'
size_hint: None, None
size: '100dp', '30dp'
color: .8, 0, 0, 1
font_size: '16dp'
#pos_hint: {'center_x': 0.8, 'center_y': 0.5}
Label:
size_hint: 1, .035
text: ''
font_size: '1dp'
BoxLayout:
orientation: 'vertical'
padding: '10dp', 0
size_hint: 1, .38
FloatLayout:
TextInput:
id: new_post_input
#disabled: new_post.buttons_disabled
opacity: 1
background_color: 0, 0, 0, 1
foreground_color: 1, 1, 1, 1
background_disable_normal: True
multiline: True
font_size: '15dp'
padding: '10dp', '10dp'
selection_color: (.8, 0, 0, .5)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
allow_copy: True
#on_focus: new_post.on_focus(self)
canvas.after:
Color:
rgba: 1,1,1,1
Line:
width: dp(1.6)
rounded_rectangle:(self.x, self.y, self.width, self.height, dp(5))
BoxLayout:
orientation: 'vertical'
id: preview_new_post_label
opacity: 0
pos_hint: {'center_x': 0.5, 'center_y': -1}
canvas.before:
Color:
rgba: (.8,.8,.8,1)
RoundedRectangle:
size: self.size
radius: [dp(6),]
pos: self.x, self.y
canvas:
Color:
rgba:0.8,0,0,1
Line:
width: dp(1.6)
rounded_rectangle:(self.x,self.y,self.width,self.height, dp(5))
ScrollView:
always_overscroll: False
bar_color: (0,0,0,0)
Label:
text: ('Someone:\n' + new_post_input.text)
padding: "10dp", "12dp"
size_hint: None, None
height: self.texture_size[1]
width: self.parent.width
font_size: "12dp"
text_size: self.width, None
color: 0,0,0,1
multiline: True
markup: True
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .08
padding: '10dp', 0
AnchorLayout:
anchor_x: 'left'
anchor_y: 'center'
Label:
text: 'Text Modification'
font_size: '12dp'
size_hint: None, None
BoxLayout:
orientation: 'vertical'
padding: '10dp', 0
size_hint: 1, .12
BoxLayout:
orientation: 'horizontal'
#padding: '10dp', 0
canvas.after:
Color:
rgba: 1, 1, 1, 1
Line:
width: 1.6
rounded_rectangle:(self.x, self.y, self.width, self.height, 5)
ScrollView:
GridLayout:
id: container_x
size_hint_x: None
rows: 1
col_default_width: dp(95)
width: self.minimum_width
spacing: dp(7)
Button:
text: "Bold"
font_size: '14dp'
color: 1, 1, 1, 1
background_color: .2, .2, .2, 0
on_press: new_post.make_bold(new_post_input, new_post_input.selection_text)
canvas.before:
Color:
rgba: (.2,.2,.2,0) if self.state=='normal' else (.3,.3,.3,1)
RoundedRectangle:
pos: self.pos
size: self.size
radius: [dp(6),]
canvas:
Color:
rgba: (1,1,1,1) if self.state=='normal' else (1,1,1,1)
Line:
width: dp(1.2)
rounded_rectangle:(self.x,self.y,self.width,self.height, dp(6))
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .08
#padding: '10dp', 0
AnchorLayout:
anchor_x: 'left'
anchor_y: 'center'
Label:
text: 'Post Category'
font_size: '12dp'
size_hint: None, None
BoxLayout:
orientation: 'vertical'
padding: '10dp', 0
size_hint: 1, .12
BoxLayout:
orientation: 'horizontal'
#padding: '10dp', 0
canvas.after:
Color:
rgba: 1, 1, 1, 1
Line:
width: 1.6
rounded_rectangle:(self.x, self.y, self.width, self.height, 5)
ScrollView:
GridLayout:
id: container_x
size_hint_x: None
rows: 1
col_default_width: dp(95)
width: self.minimum_width
spacing: dp(7)
ToggleButton:
text: "Opinion"
font_size: '14dp'
color: 1, 1, 1, 1
background_color: .2, .2, .2, 0
on_press: new_post.update_catagory(self, 'opinions')
canvas.before:
Color:
rgba: (.2,.2,.2,0) if self.state=='normal' else (.3,.3,.3,1)
RoundedRectangle:
pos: self.pos
size: self.size
radius: [dp(6),]
canvas:
Color:
rgba: (1,1,1,1) if self.state=='normal' else (1,1,1,1)
Line:
width: dp(1.2)
rounded_rectangle:(self.x,self.y,self.width,self.height, dp(6))
Demo program working: YouTube link
Thank you for any suggestions/solutions :)
Update
It turns out that the selection seems to stop working when the TextInput is within a PageLayout with more than 2 pages in it, (Which it is in my program).
I have created a rough demo program to show the problem and have put it in .py and .kv
You can achieve that by creating a custom TextInput instance and overriding one of its method.
class MyTextInput(TextInput):
selected_text = StringProperty("")
# Use this prop. instead of 'selection_text'.
def cancel_selection(self):
self.selected_text = self.selection_text
super().cancel_selection()
Now use this instance and the new property wherever you need.
In .kv,
FloatLayout:
MyTextInput:
id: new_post_input
.
.
.
Button:
text: "Bold"
font_size: '14dp'
color: 1, 1, 1, 1
background_color: .2, .2, .2, 0
on_press: new_post.make_bold(new_post_input, new_post_input.selected_text)
I've created an app with a main page that is going to have multiple sections with recycleView data. I have added the first but when I try to add a second (eventually adding 4 to replace the APPS, OTHER, etc sections) the second recycleView shows up on top of the first one (see top left of image) instead of next to it like I would expect. I tried putting both of the recycleView widgets inside a boxlayout but the second RV still overlays the top.
The information should be filling the second box on the right here instead of overlaying on the far top left
Screen:
MDNavigationLayout:
ScreenManager:
id: screenManager
Screen:
name: "monitor"
CoverImage:
source: 'images/monitor.png'
# Darken the photo
canvas:
Color:
rgba: 0, 0, 0, .6
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Administration'
left_action_items: [["menu", lambda x: nav_drawer.set_state('toggle')]]
elevation:5
BoxLayout:
ServerListWidget:
MountListWidget:
Please let me know if you need to see any additional code. I've also tried using a GridLayout with 2 cols
<MountListWidget>:
id: mountListWidget
recycleView: recycleView_mounts
BoxLayout:
padding: dp(20)
spacing: dp(10)
BoxLayout:
spacing: dp(10)
BoxLayout:
canvas.before:
Color:
rgba: 1,1,1,.1
# Use a float layout to round the corners
RoundedRectangle:
pos: self.pos
size: self.size
RecycleView:
id: recycleView_mounts
viewclass: 'MountWidget'
RecycleGridLayout:
cols: 1
default_size: self.parent.width, dp(60)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
width: self.minimum_width
spacing: dp(155), dp(0)
<ServerListWidget>:
id: serverListWidget
recycleView: recycleView
BoxLayout:
padding: dp(20)
spacing: dp(10)
BoxLayout:
orientation: "horizontal"
BoxLayout:
canvas.before:
Color:
rgba: 1,1,1,.1
# Use a float layout to round the corners
RoundedRectangle:
pos: self.pos
size: self.size
RecycleView:
id: recycleView
viewclass: 'ServerWidget'
RecycleGridLayout:
cols: 1
default_size: self.parent.width / 5, dp(60)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
width: self.minimum_width
spacing: dp(155), dp(0)
<CoverImage#CoverBehavior+Image>:
reference_size: self.texture_size
<MountWidget>:
BoxLayout:
#size_hint_max_x: dp(360)
size_hint_min_x: dp(150)
orientation: "horizontal"
BoxLayout:
orientation: "horizontal"
Button:
# Use the on_press to print troubleshooting info, otherwise comment out
on_press: print(self.background_color)
padding: dp(10), dp(10)
text_size: self.size
font_size: dp(18)
background_color: .5,1,1,.6
halign: "left"
valign: "top"
size: 1,1
text: str(root.name)
bold: True
color: (1,1,1)
<ServerWidget>:
BoxLayout:
#size_hint_max_x: dp(360)
size_hint_min_x: dp(150)
orientation: "horizontal"
BoxLayout:
orientation: "horizontal"
Button:
# Use the on_press to print troubleshooting info, otherwise comment out
on_press: print(self.background_color)
padding: dp(10), dp(10)
text_size: self.size
font_size: dp(22) if root.has_issue else dp(18)
background_color: .5,1,1,.6 #utils.get_random_color(alpha=.6)
halign: "left"
valign: "top"
size: 1,1
# Show last 4 of server name
text: str(root.name).upper()[-4:]
bold: True
color: utils.get_color_from_hex(warn) if root.has_issue else (1,1,1)
Button:
background_color: .5,1,1,.6
text_size: self.size
font_size: dp(22) if root.work >= 85 and root.work < 90 else dp(26) if root.work >= 90 else dp(18)
valign: "center"
halign: "center"
text: str(root.work)
padding: dp(8), dp(8)
bold: True if root.work > 90 else False
# yellow red white
color: (1,1,0) if root.work >= 85 and root.work < 90 else utils.get_color_from_hex(warn) if root.work >= 90 else (1,1,1)
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
Can someone tell me how to call function def demo() on double click of Label Item1,Item2 ?
test.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
def convert_data(data):
l = []
for item in data:
for key, value in item.items():
l.append({'text': key, 'value': value})
return l
class test():
def demo(self):
print('Demo')
class Invoice(Screen):
def abc(self):
arr = ({'Item1': ''},{'Item2': 1000})
self.rv.data = convert_data(arr)
class MyApp(App):
def build(self):
return Builder.load_file('test.kv')
if __name__ == '__main__':
MyApp().run()
test.kv
<Button#Button>:
font_size: 15
size_hint_y:None
height: 30
<Label#Label>:
font_size: 15
size_hint_y:None
height: 30
<Item#GridLayout>:
cols: 2
text: ""
value: 0
padding : 5, 0
spacing: 10, 0
Label:
size_hint_x: .35
text: root.text
halign: 'left'
valign: 'middle'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Label:
size_hint_x: .15
text: str(root.value)
halign: 'right'
valign: 'middle'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Invoice:
rv: rv
BoxLayout:
orientation: "vertical"
padding : 15, 15
BoxLayout:
orientation: "vertical"
padding : 5, 5
size_hint: .6, None
pos_hint: {'x': .18,}
BoxLayout:
orientation: "horizontal"
padding : 5, 5
spacing: 10, 10
size: 800, 40
size_hint: 1, None
Button:
text: "Show"
size_hint_x: .05
spacing_x: 30
on_press:root.abc()
BoxLayout:
orientation: "horizontal"
size_hint: 1, 1
BoxLayout:
orientation: "vertical"
size_hint: .5, 1
padding : 0, 15
spacing: 10, 10
size: 500, 30
BoxLayout:
RecycleView:
id: rv
viewclass: 'Item'
RecycleBoxLayout:
default_size: None, dp(30)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
you can create a subclass of your Label and attach the double_tap event to it
class MyLabel(Label):
def on_touch_down(self, touch):
if touch.is_double_tap:
demo()