When I try to access TextInput that has id total_area from my python code, I get this error: AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'. I used debugger to see whats wrong and it seems to me that variables in python code are not getting linked to those in kv file. Help
Python code:
class RootWidget(Widget):
Config.set('graphics', 'resizable', '0')
Config.set('graphics', 'height', '720')
Config.set('graphics', 'width', '480')
koatuu_obl_cent = ObjectProperty()
koatuu_city = ObjectProperty()
kitchen_area = ObjectProperty()
qt_room = ObjectProperty()
floor = ObjectProperty()
qt_floor = ObjectProperty()
total_area = ObjectProperty()
living_area = ObjectProperty()
year_building = ObjectProperty()
#classmethod
def button_OnPress(self):
print(self.total_area.text)
class DeepEval(App):
def build(self):
return RootWidget()
if __name__ == '__main__':
DeepEval().run()
.kv:
#:import Factory kivy.factory.Factory
<MyPopup#Popup>:
auto_dismiss: False
size_hint: None, None
size: 400, 400
<RootWidget>
koatuu_obl_cent: koatuu_obl_cent
koatuu_city: koatuu_city
kitchen_area: kitchen_area
qt_room: qt_room
floor: floor
qt_floor: qt_floor
total_area: total_area
living_area: living_area
year_building: year_building
Image:
source: 'imgs/background.jpg'
size: self.texture_size
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
size: root.width, root.height
BoxLayout:
orientation: 'vertical'
height: 350
width: 225
size_hint_x: None
size_hint_y: None
spacing: 22.5
TextInput:
id: total_area
multiline: False
hint_text: "Param12"
halign: 'center'
foreground_color: .5, .5, .5, .75
height: 33
width: 225
size_hint_x: None
size_hint_y: None
TextInput:
id: kitchen_area
multiline: False
hint_text: "Param1"
halign: 'center'
foreground_color: .5, .5, .5, .75
height: 33
width: 225
size_hint_x: None
size_hint_y: None
TextInput:
id: living_area
multiline: False
hint_text: "Param1"
halign: 'center'
foreground_color: .5, .5, .5, .75
height: 33
width: 225
size_hint_x: None
size_hint_y: None
TextInput:
id: qt_room
multiline: False
hint_text: "Param1"
halign: 'center'
foreground_color: .5, .5, .5, .75
height: 33
width: 225
size_hint_x: None
size_hint_y: None
TextInput:
id: floor
multiline: False
hint_text: "Param1"
halign: 'center'
foreground_color: .5, .5, .5, .75
height: 33
width: 225
size_hint_x: None
size_hint_y: None
TextInput:
id: qt_floor
multiline: False
hint_text: "Param1"
halign: 'center'
foreground_color: .5, .5, .5, .75
height: 33
width: 225
size_hint_x: None
size_hint_y: None
TextInput:
id: year_building
multiline: False
hint_text: "Param1"
halign: 'center'
foreground_color: .5, .5, .5, .75
height: 33
width: 225
size_hint_x: None
size_hint_y: None
Button:
id: koatuu_obl_cent
text: 'Open popup'
on_release: Factory.MyPopup().open()
height: 33
width: 225
size_hint_x: None
size_hint_y: None
Button:
id: koatuu_city
text: 'Open popup'
on_release: Factory.MyPopup().open()
height: 33
width: 225
size_hint_x: None
size_hint_y: None
AnchorLayout:
anchor_x: "center"
anchor_y: "bottom"
padding: 0,0,0,25
Button:
text: "Calculate"
size_hint_x: None
size_hint_y: None
height: 50
width: 135
on_press: root.button_OnPress()
AnchorLayout:
anchor_x: "right"
anchor_y: "bottom"
Button:
text: "cam"
size_hint_x: None
size_hint_y: None
height: 45
width: 45
#classmethod
def button_OnPress(self):
print(self.total_area.text)
Your wrote #classmethod so you got a class method, whose first argument is not an instance of the class (usually called self) but rather the class itself (usually called cls).
Your self.total_area is therefore equivalently RootWidget.total_area which is, as the error says, an ObjectProperty.
You need to access the total_area as an attribute of an instance of your class. In this case, remove the #classmethod.
Related
I completed my code for main window which is the login page and I'm writing my code for the second page. Then, I tried to use the text field on my login screen and I can't type in it. However, before making my second window, the text field worked as expected and I can type in it. What could cause this issue?
Furthermore, I recieve an error (AttributeError: 'super' object has no attribute 'getattr'
) when I tried to press the clear button in main window.
Also, if you tried to run this code and see the second window, you will notice 3 MD cards not aligned properly if you don't expand the window to full size. if you have any idea how to fix it, please let me know.
SUMMARY OF WHAT I'M TRYING TO ACHIEVE HERE:
Figure out what why I can't use the textfield in main window
Fix the error when press the clear button
Align MD Cards properly
.py file
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class BaseApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "Green"
return Builder.load_file("base.kv")
def clear(self):
self.root.ids.user.text = ""
self.root.ids.passw.text = ""
if __name__ == "__main__":
BaseApp().run()```
.kv file
# Filename: base.kv
#:kivy 2.1.0
#:include main.py
WindowManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
Screen:
MDCard:
size_hint: None, None
size: 400, 500
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 25
spacing: 25
orientation: "vertical"
MDLabel:
id: login_label
text: "WELCOME TO JAMES MADISON/n HIGH SCHOOL STUDENTS APP"
font_size: 22
font_name: "Raleway-Regular"
halign: "center"
size_hint_y: None
height: self.texture_size[1]
padding_y: 15
MDBoxLayout:
Image:
source: "jmhs.png"
size_hint_x: 1.15
halign: "center"
size_hint_y: 1.15
height: self.texture_size[1]
padding_y: 10
MDTextFieldRound:
id: user
hint_text: "Username"
icon_right: "account"
size_hint_x: None
width: 200
font_size: 18
font_name: "Roboto-Regular"
pos_hint: {"center_x": 0.5}
MDTextFieldRound:
id: passw
hint_text: "Password"
icon_right: "eye-off"
size_hint_x: None
width: 200
font_size: 18
font_name: "Roboto-Regular"
pos_hint: {"center_x": 0.5}
password: True
MDRoundFlatButton:
text: "LOG IN"
font_size: 12
font_name: "Raleway-Regular"
pos_hint: {"center_x": 0.5}
on_press:
app.root.current = "second"
root.manager.transition.direction = "left"
MDRoundFlatButton:
text: "CLEAR"
font_size: 12
font_name: "Raleway-Regular"
pos_hint: {"center_x": 0.5}
on_press: app.clear()
<SecondWindow>:
name: "second"
MDBoxLayout:
orientation: "horizontal"
MDBoxLayout:
size_hint: .225, 1
canvas.before:
Color:
rgba: 0, 0, 0, 0
Rectangle:
pos: self.pos
size: self.size
MDBoxLayout:
orientation: "vertical"
spacing: "10dp"
size_hint: 1, 1
canvas.before:
Color:
rgba: 227/255, 255/255, 226/255, 0.8
Rectangle:
pos: self.pos
size: self.size
MDBoxLayout:
spacing: "10dp"
size_hint: 1, .1
MDBoxLayout:
spacing: "10dp"
size_hint: 1, .9
canvas.before:
Color:
rgba: 227/255, 255/255, 226/255, 0.8
Rectangle:
pos: self.pos
size: self.size
MDGridLayout:
cols: 3
padding: 18
spacing: "20dp"
MDCard:
padding: 16
elevation: 0
border_radius: 20
radius: [15]
size_hint: None, None
size: "370dp", "140dp"
pos_hint: {"center_x": .5, "center_y": .5}
MDCard:
padding: 16
elevation: 0
border_radius: 20
radius: [15]
size_hint: None, None
size: "370dp", "140dp"
pos_hint: {"center_x": .5, "center_y": .5}
MDCard:
padding: 16
elevation: 0
border_radius: 20
radius: [15]
size_hint: None, None
size: "200dp", "140dp"
pos_hint: {"center_x": .5, "center_y": .5}
MDBoxLayout:
size_hint: .29, 1
canvas.before:
Color:
rgba: 0, 0, 0, 0
Rectangle:
pos: self.pos
size: self.size
I need all the elements of my FirsWindow, put them in a recycleview. I'm thinking to do it from RecycleGridLayout instead of GridLayout, but I don't how to really apply it in the code. It's necesary that the GridLayout or RecycleGridLayout have cols:4, I think it's not necessary upload my py.file but I'm not sure. please I need help on this. thanks in advance.
kv.file
<FirstWindow>:
name: "Catalogue"
BoxLayout:
id: Boxmain
orientation: "vertical"
#size: root.width, root.height
#size_hint: 1, None
#height: 300
#padding: 50
spacing: 50
size: root.height, root.width
GridLayout:
cols: 4
padding: 4
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
#size_hint: (1, .15)
Label:
text: "0.0"
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
#------------------------
Button:
text:"lala"
Button:
text:"lal2"
Button:
text:"lale"
Button:
text:"lal5"
Button:
text:"lala"
Button:
text:"lal54"
Button:
text:"lala"
#------------------------------------------------------------
How can I change the mouse pointer to 'hand' when hovering over a button present in a screen along with a different widget in kivy python?
I tried creating two functions in a python file. But it changes the mouse cursor for the entire page. How can I change the cursor for just the button present on the page?
#Kivy design file
<Loginpage>:
name: "login"
Image:
source: 'background.jpg'
allow_stretch: True
keep_ratio: False
Screen:
MDCard:
size_hint: None,None
size: 400, 550
pos_hint: {"center_x":0.5,"center_y":0.5}
elevation: 10
padding: 25
spacing: 25
orientation: 'vertical'
MDLabel:
id: welcome_label
text: "Welcome"
font_size: 40
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
padding_y:15
font_style: 'H2'
MDTextField:
id: uid
hint_text: "Username"
icon_right: "account"
size_hint_x: None
width: 200
font_size: 20
pos_hint: {"center_x": 0.5}
MDTextField:
id: pwd
hint_text: "Password"
icon_right: "lock"
size_hint_x: None
width: 200
font_size: 20
pos_hint: {"center_x": 0.5}
password: True
MDRoundFlatButton:
text: "LOG IN"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: root.logger()
MDRoundFlatButton:
text: "CLEAR"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: root.clear()
MDLabel:
id: login_label
text: "Invalid Login"
font_size: 14
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
pos_hint: {"center_x": 0.5}
padding_y:15
theme_text_color:'Custom'
text_color: 1,0,0,1
Widget:
size_hint_y: None
height: 10
Python File:
class Loginpage(Screen):
def enter(self, *args):
Window.set_system_cursor('hand')
class Loginpage(Screen):
def enter(self, *args):
Window.set_system_cursor('hand')
def leave(self, *args):
Window.set_system_cursor('arrow')
def logger(self):
'''code to login'''
I have managed to get the size of a box layout to change based on the corresponding Label height, however, it doesn't line up perfectly and I cannot figure out why.
I have looked at adding offsets but have had no luck since it ended up making the issue worse
Thank you for any help
MainInterface:
<MainInterface#BoxLayout>:
orientation: "vertical"
Label:
#font_name: "Nunito-Black.ttf"
text: "T R U T H"
size_hint: 1, 0.1
GridLayout:
size_hint: 1, 0.12
cols: 4
Button:
text: "Menu1"
Button:
text: "Menu2"
Button:
text: "Menu3"
Button:
text: "Menu4"
PageLayout:
border: "20dp"
swipe_threshold: 0.2
RecycleView:
viewclass: 'PostGrid'
scroll_y: 1
id: rv
data: app.posts
RecycleBoxLayout:
id: box
default_size: None, None
default_size_hint: 1, None
size_hint_y: None
padding: ["10dp", "16dp"]
spacing: "8dp"
height: self.minimum_height
orientation: 'vertical'
key_size: '_size'
BoxLayout:
orientation: "vertical"
Button:
text: "peni"
Button:
text: "tag # will J"
Button:
text: "Input"
<PostGrid#BoxLayout>:
message_id: -1
orientation: "horizontal"
text: ''
spacing: "10dp"
#size_hint: self.width, None
_size: 0, 74
size: 0, 74
text_size: None, None
BoxLayout:
orientation: "vertical"
spacing: "10dp"
size_hint: .1, 1
size: self.size
Button:
text: "UP"
font_size: "5dp"
size_hint: 1, 0.2
Button:
text: "DOWN"
font_size: "5dp"
size_hint: 1, 0.2
Label:
text: "test"
font_size: "5dp"
size_hint: 1, 0.6
Label:
text: root.text
padding: 5, 5
size_hint: .9, 1
#size: self.texture_size
height: self.texture_size[1]
text_size: self.width, None
color: 0,0,0,1
#text_size: self.width, None
#size_hint: None, 1
#size: self.texture_size
#font_name: "Nunito-Bold.ttf"
#font_size: "12dp"
multiline: True
#size: 1, root.min_height
on_texture_size:
app.update_message_size(
root.message_id,
self.texture_size,
root.width)
pos: self.pos
canvas.before:
Color:
rgba: (0.8, 0.8, 0.8, 1)
RoundedRectangle:
size: self.texture_size
radius: [5, 5, 5, 5]
pos: self.x, self.y
canvas:
Color:
rgba:0,0.9,0.9,1
Line:
width:0.8
rounded_rectangle:(self.x,self.y,self.width,self.height, 5)
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.properties import ListProperty
from kivy.animation import Animation
from kivy.metrics import dp
class TruthApp(App):
posts = ListProperty([{'message_id':0, 'text':"testskjhfjksdhfkjshfjshfjkhskjdfhskjdhfkjshfdkjhsjkdfhskjhdfkjshdfjkhzxczxczxczxcxzczxcxsjkdfhjksdhfkjshkjdfhksjdhfjkshdfjkhsdkjhfkjshdfjkshkjfhskjhfkjshfjkshdkjfhskjhfjshfkjshdfjkshdjkfhskjhfkjshfjksdhjfhsjkdhfjkhsdkjfhskjhfjk\ngdgdgd\ndgdgdg\ndgdgdg\ndggdgd",'_size':[0,0] }, {'message_id':1, 'text':"testskjhfjksdhfkjshfjshfjkhskjdfhskjdhfkjshfdkjhsjkdfhskjhjfhskjhfjk,'_size':[0,0]"}])
def update_message_size(self, message_id, texture_size, max_width):
#print(self.posts)
#print("Here")
self.posts[message_id] = {**self.posts[message_id], '_size':[0, texture_size[1]]}
if __name__ == '__main__':
TruthApp().run()
^ Image showing how the code above runs
The problem is in the viewclass of RecycleView (i.e. PostGrid). You set its heightas such it could not accomodate its children which is supposed to be the minimum height that will place all its children.
Now that's exactly the attr. minimum_height does. With this being applied you also don't need default_size: None, None (especially the height attr.)
With this and other modifications your .kv file should now look like,
MainInterface:
<MainInterface#BoxLayout>:
orientation: "vertical"
Label:
#font_name: "Nunito-Black.ttf"
text: "T R U T H"
size_hint: 1, 0.1
GridLayout:
size_hint: 1, 0.12
cols: 4
Button:
text: "Menu1"
Button:
text: "Menu2"
Button:
text: "Menu3"
Button:
text: "Menu4"
PageLayout:
border: "20dp"
swipe_threshold: 0.2
RecycleView:
viewclass: 'PostGrid'
scroll_y: 1
id: rv
data: app.posts
RecycleBoxLayout:
id: box
default_size_hint: 1, None
size_hint_y: None
padding: ["10dp", "16dp"]
spacing: "8dp"
height: self.minimum_height
orientation: 'vertical'
key_size: '_size'
BoxLayout:
orientation: "vertical"
Button:
text: "peni"
Button:
text: "tag # will J"
Button:
text: "Input"
<PostGrid#BoxLayout>:
message_id: -1
orientation: "horizontal"
text: ''
spacing: "10dp"
size_hint_y: None
#_size: 0, 74
height: self.minimum_height
text_size: None, None
BoxLayout:
orientation: "vertical"
spacing: "10dp"
size_hint: .1, 1
Button:
text: "UP"
font_size: "5dp"
size_hint: 1, 0.2
Button:
text: "DOWN"
font_size: "5dp"
size_hint: 1, 0.2
Label:
text: "test"
font_size: "5dp"
size_hint: 1, 0.6
Label:
text: root.text
padding: 5, 5
#size_hint: .9, 1
#size: self.texture_size
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
color: 0,0,0,1
#text_size: self.width, None
#size_hint: None, 1
#size: self.texture_size
#font_name: "Nunito-Bold.ttf"
#font_size: "12dp"
multiline: True
#size: 1, root.min_height
on_texture_size:
app.update_message_size(
root.message_id,
self.texture_size,
root.width)
pos: self.pos
canvas.before:
Color:
rgba: (0.8, 0.8, 0.8, 1)
RoundedRectangle:
size: self.texture_size
radius: [5, 5, 5, 5]
pos: self.x, self.y
canvas:
Color:
rgba:0,0.9,0.9,1
Line:
width:0.8
rounded_rectangle:(self.x,self.y,self.width,self.height, 5)
I have been trying to make an app that have many functions associate to one buttons each. This way, if I have 70 buttons I have 70 different functions. I want to add, the respective value, when I click respective button, to a respective variable label (I am using numericproperty). As the change between this functions is only in this numeric value, I would like to make this in a more inteligent way than I did. Have anybody one suggestion of better way to do it without I have to repeat my code? Very thanks everybody, and part of my code is bellow.
.py
class SegundoScreen(Screen):
def __init__(self, **kwargs):
self.name = 'dois'
super(Screen,self).__init__(**kwargs)
def mucarela(self, *args):
screen4 = self.manager.get_screen('carrinho')
screen4.btn1 = BubbleButton(text="Muçarela", font_size='20dp', size_hint=(1,None), background_normal='1.png', background_down='2.png')
screen4.lb1 = Label(text="25,00", font_size='20dp', size_hint=(1,None))
screen4.ids.lb5.value += 25
screen4.ids.grid.add_widget(screen4.btn1)
screen4.ids.grid.add_widget(screen4.lb1)
def catupiry(self, *args):
screen4 = self.manager.get_screen('carrinho')
screen4.btn2 = BubbleButton(text="Catupiry",font_size='20dp', size_hint=(1,None), background_normal='2.png', background_down='1.png')
screen4.lb2 = Label(text="25,00",font_size='20dp', size_hint=(1,None))
screen4.ids.lb5.value += 25
screen4.ids.grid.add_widget(screen4.btn2)
screen4.ids.grid.add_widget(screen4.lb2)
def peru(self, *args):
screen4 = self.manager.get_screen('carrinho')
screen4.btn2 = BubbleButton(text="Peito de peru",font_size='20dp', size_hint=(1,None), background_normal='1.png', background_down='2.png')
screen4.lb2 = Label(text="95,00",font_size='20dp', size_hint=(1,None))
screen4.ids.lb5.value += 35
screen4.ids.grid.add_widget(screen4.btn2)
screen4.ids.grid.add_widget(screen4.lb2)
[...]
and .kv
StackLayout:
orientation: 'tb-lr'
ScrollView:
size_hint: (1, .9)
pos_hint:{'x': .0, 'y': .0}
GridLayout:
cols: 2
padding: 45, 50
spacing: 25, 50
size_hint: (1, 1)
size_hint_y: None
height: self.minimum_height
width: 500
Label:
text: "[b]Escolha[/b]\n[i]Sabor[/i]"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
Label:
text: "[b]Preço\n[/b] [i](R$)[/i]"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
Button:
text: "[b]Muçarela[/b]\n[i]Muçarela, tomate\n e orégano[/i]"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
background_normal:'1.png'
background_down:'2.png'
on_press: root.mucarela()
Label:
text: "25,00"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
Button:
text: "[b]Catupiry[/b]\n[i]Catupiry, azeitona\n e tomate[/i]"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
background_normal:'2.png'
background_down:'1.png'
on_press: root.catupiry()
Label:
text: "25,00"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
Button:
text: "[b]Peito de peru[/b]\n[i]Muçarela, peito de peru\n e tomate[/i]"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
background_normal:'1.png'
background_down:'2.png'
on_press: root.peru()
Label:
text: "35,00"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
Button:
text: "[b]Portuguesa[/b]\n[i]Muçarela, presunto,\n cebola e ovo[/i]"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
background_normal:'2.png'
background_down:'1.png'
on_press: root.portuguesa()
Label:
text: "27,00"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
Button:
text: "[b]Toscana[/b]\n[i]Calabresa moída, muçarela\ne parmesão[/i]"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
background_normal:'1.png'
background_down:'2.png'
on_press: root.toscana()
Label:
text: "35,00"
markup: True
font_size: '20dp'
size_hint_y: None
height: self.texture_size[1]
and I have more one class form that is just one label numericpropertie whose change the value when respective button has clicked. As the change is only in this label, I am locking for how can I take the price value from the text label, and flavor name from the text button. Very thanks.
for your .kv file I recommend using Classes like
<MyButton#Button>:
markup: True
font_size: '20dp'
size_hint_y: None
then in your code you could use instances of MyButton which could minify your code a little bit.