I have a Class level boolean variable which speaks with all the classes and widget in each class change depending on weather or not this value is true. Opacity is throwing the below error when I try to execute. I've tried AsyncImages but it given a loading logo which I am trying to avoid.
Below is the snippet of the code
Python File:
class MainScreen(Screen):
pass
class SecondScreen(Screen):
pass
class TestApp(App):
ABS_OPACITY=BooleanProperty(True)
def test(self):
print "Hello"
def build(self):
return presentation
Kivy file -
ScreenManagement:
MainScreen:
SecondScreen:
<MainScreen>:
name: "Main"
FloatLayout:
Button:
font_size:12
size_hint: 0.07, 0.05
text: "AC"
on_click:app.ABS_OPACITY=not app.ABS_OPACITY
pos_hint: {"right":0.93,"left":0.30, "bottom":0.1, "top": 0.93}
Image:
source: "xyz/image.png"
pos_hint:{"top":0.955}
opacity:1 if app.ABS_OPACITY else 0
Image:
source: "abc/image.png"
pos_hint:{"top":0.955}
opacity:0 if app.ABS_OPACITY else 1
<SecondScreen>:
name: "Second"
FloatLayout:
Button:
font_size:12
size_hint: 0.07, 0.05
text: "AC"
on_click:on_click:app.ABS_OPACITY=not app.ABS_OPACITY
pos_hint: {"right":0.93,"left":0.30, "bottom":0.1, "top": 0.93}
Image:
source: "xyz/image"
pos_hint:{"top":0.955}
opacity:1 if app.ABS_OPACITY else 0
Image:
source: "abc/image.png"
pos_hint:{"top":0.955}
opacity:0 if app.ABS_OPACITY else 1
The above is the error I am facing when I do the same..
Traceback (most recent call last):
File "testapp.py", line 412, in <module>
presentation = Builder.load_file ("main.kv")
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 301, in load_file
return self.load_string(data, **kwargs)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 405, in load_string
rule_children=rule_children)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 657, in _apply_rule
root=rctx['ids']['root'], rule_children=rule_children)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/uix/widget.py", line 469, in apply_class_lang_rules
rule_children=rule_children)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 538, in apply
rule_children=rule_children)
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 707, in _apply_rule
e), cause=tb)
kivy.lang.builder.BuilderException: Parser: File "/home/pi/Downloads/motormind/main.kv", line 161:
...
159: source: "UI_UX/blue/mainpage/abs.png"
160: pos_hint:{"top":0.955}
>> 161: opacity:0 if app.ABS_OPACITY else 1
162: #Park Lights On
163: Image:
...
BuilderException: Parser: File "/home/pi/Downloads/motormind/main.kv", line 161:
...
159: source: "UI_UX/blue/mainpage/abs.png"
160: pos_hint:{"top":0.955}
>> 161: opacity:0 if app.ABS_OPACITY else 1
162: #Park Lights On
163: Image:
...
AttributeError: 'NoneType' object has no attribute 'bind'
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 249, in create_handler
return eval(value, idmap), bound_list
File "/home/pi/Downloads/motormind/main.kv", line 161, in <module>
opacity:0 if app.ABS_OPACITY else 1
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/parser.py", line 75, in __getattribute__
object.__getattribute__(self, '_ensure_app')()
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/parser.py", line 70, in _ensure_app
app.bind(on_stop=lambda instance:
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 692, in _apply_rule
rctx['ids'])
File "/home/pi/.local/lib/python2.7/site-packages/kivy/lang/builder.py", line 254, in create_handler
cause=tb
This error is typical of calling Builder.load_file() for a kv file that is creating Widgets and referencing the app before an App is defined. Try calling Builder.load_file() in the build() method of the App.
Another way to fix this is to use a <ScreenManagerment> rule in the kv file and then do
return ScreenManagement()
in your build() method.
Related
I'm using Kivy in python trying to make the button "Save" run the "Save_number" function but I keep getting a "TypeError: 'str' object is not callable"-error. Below is relevant code.
class DBScreen(Screen):
def save_number(self):
conn = sqlite3.connect('database.db')
c = conn.cursor()
name = self.ids.save_name.text
number = self.ids.save_number.text
name = str(name).replace("(","").replace(")","").replace(",","").replace("'","")
number = str(number).replace("(", "").replace(")", "").replace(",", "").replace("'", "")
number = int(number)
c.execute("INSERT INTO database VALUES (?,?)", (name, number))
c.execute("SELECT * FROM database WHERE name=:name", {'name': name})
conn.commit()
conn.close()
pass
def save_process(self):
self.save_name = self.ids.save_name.text
self.save_number = self.ids.save_number.text
if self.save_number == "" and self.save_name == "":
self.disable_save()
elif self.save_number != "" and self.save_name != "":
self.enable_save()
pass
def disable_save(self):
self.ids.save_button.disabled = True
def enable_save(self):
self.ids.save_button.disabled = False
def load_number(self):
global number_list
global initial_length
conn = sqlite3.connect('database.db')
c = conn.cursor()
name = self.ids.load_name.text
if self.ids.load_name.text == "":
self.disable_submit()
else:
self.enable_submit()
c.execute("SELECT * FROM database WHERE name=:name", {'name': name})
name, number = c.fetchone()
conn.commit()
conn.close()
number_list = []
for i in number:
number_list.append(int(i))
initial_length = len(number_list)
def load_process(self):
self.load_text = self.ids.load_name.text
print(self.load_text)
if self.load_text == "":
self.disable_load()
else:
self.enable_load()
def disable_load(self):
self.ids.load_button.disabled = True
def enable_load(self):
self.ids.load_button.disabled = False
Below is the relevant part of the Kivy file
<DBScreen>:
GridLayout:
padding: 20
cols: 1
rows: 10
TextInput:
id: save_name
hint_text:'Name'
pos_hint: {'center_x': 0.5, 'center_y': 0.705}
size_hint: 0.95, 0.5
multiline: False
on_text: root.save_process()
TextInput:
id: save_number
hint_text:'Number'
pos_hint: {'center_x': 0.5, 'center_y': 0.705}
size_hint: 0.95, 0.5
multiline: False
input_filter: 'int'
on_text: root.save_process()
Button:
id: save_button
text: 'Save'
disabled: True
on_press: root.save_number()
Error code
Traceback (most recent call last):
File "C:\Users\aronf\PycharmProjects\rtn\main.py", line 184, in <module>
gui.run()
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\app.py", line 955, in run
runTouchApp()
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 574, in runTouchApp
EventLoop.mainloop()
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 339, in mainloop
self.idle()
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 383, in idle
self.dispatch_input()
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 334, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\base.py", line 263, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\core\window\__init__.py", line 1660, in on_motion
self.dispatch('on_touch_down', me)
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\core\window\__init__.py", line 1677, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\screenmanager.py", line 1210, in on_touch_down
return super(ScreenManager, self).on_touch_down(touch)
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\relativelayout.py", line 306, in on_touch_down
ret = super(RelativeLayout, self).on_touch_down(touch)
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\widget.py", line 589, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy\_event.pyx", line 727, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1191, in kivy._event.EventObservers._dispatch
File "C:\Users\aronf\AppData\Local\Programs\Python\Python39\lib\site-packages\kivy\lang\builder.py", line 55, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:\Users\aronf\PycharmProjects\rtn\gui.kv", line 124, in <module>
on_press: root.save_number()
TypeError: 'str' object is not callable
Process finished with exit code 1
You have a function and a TextInput with the same name number and both of them are in root (DBScreen), so the interpreter doesn't understand which name you call and calls a TextInput, not a function as you expected. To fix the problem give a TextInput another name
I'm trying to get the data from a simple textinput that is in my kv file and use the information in my python file.
but i keep getting the same error.
this error happens when i press a button and the function try to run looks like its trying to get the information from the text_location widget
thank you in advance.
class ScatterTextWidget(BoxLayout):
def initialize_request(self):
''' Initial analysis and control flow of the class '''
location = self.ids.input_location
date = S.ids.input_date
time_raw = ScatterTextWidget.ids.input_time
print(f'{location} - {date} - {time_raw}')
class HistoricalApp(App):
def build(self):
return ScatterTextWidget()
if __name__ == "__main__":
HistoricalApp().run()
the kivy file called "historical.kv"
i reduce it because is kind of long
<ScatterTextWidget>:
BoxLayout:
canvas.before:
Color:
rgb: utils.get_color_from_hex('#01183f')
Rectangle:
pos: self.pos
size: self.size
size_hint_y: None
height: 40
valign: 'middle'
TextInput:
id:text_location
width: 200
id: input2
font_size: 15
size_hint_y: None
height:40
TextInput:
id:input_date
width: 200
id: input1
font_size: 15
size_hint_y: None
height: 40
TextInput:
id:input_time
width: 200
id: input1
font_size: 15
size_hint_y: None
height: 40
the error i receive is
File "kivy\properties.pyx", line 838, in
kivy.properties.ObservableDict.__getattr__
KeyError: 'text_location'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/IBKCo/Desktop/Git Programs/TimeStationHistoricalRecords/MainGui.py", line 77, in <module>
HistoricalApp().run()
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\app.py", line 826, in run
runTouchApp()
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\base.py", line 502, in runTouchApp
EventLoop.window.mainloop()
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\core\window\window_sdl2.py", line 727, in mainloop
self._mainloop()
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\core\window\window_sdl2.py", line 460, in _mainloop
EventLoop.idle()
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\base.py", line 340, in idle
self.dispatch_input()
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\base.py", line 325, in dispatch_input
post_dispatch_input(*pop(0))
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\base.py", line 291, in post_dispatch_input
wid.dispatch('on_touch_up', me)
File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
self.dispatch('on_release')
File "kivy\_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
File "kivy\_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
File "kivy\_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\historical.kv", line 127, in <module>
on_release: root.initialize_request()
File "C:/Users/IBKCo/Desktop/Git Programs/TimeStationHistoricalRecords/MainGui.py", line 31, in initialize_request
location = self.ids.text_location
File "kivy\properties.pyx", line 841, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
thank you in advance
best,
First, you need to remove both of the id:input1 lines and the id:input2 line. Then change your initialize_request method to:
def initialize_request(self):
''' Initial analysis and control flow of the class '''
location = self.ids.text_location.text
date = self.ids.input_date.text
time_raw = self.ids.input_time.text
print(f'{location} - {date} - {time_raw}')
Note that self.ids.text_location is a reference to the TextInput widget, so you have to add .text to get its text. Same for the other TextInput widgets.
GridLayout:
cols:
rows: root.numberoflist
for i in range(root.businfolist):
Label:
font_name: 'C:/Windows/Fonts/HYWULM'
text_size: cm(2), cm(2)
pos: self.pos
id: my_custom_label
text: root.businfolist[0]
color: [255,255,255,1]
.kv file
i want use FOR STATEMENT to .kv file and i saw the
https://kivy.org/docs/api-kivy.lang.html?highlight=statement
this page, but i don't understand.
i think indentation seems to be wrong.
what is wrong with these code?
traceback is here thank you for read my question.
Traceback (most recent call last):
File "C:/Users/mg/Desktop/Tubuc/TubucTest.py", line 58, in <module>
class Bus_Information(Screen):
File "C:/Users/mg/Desktop/Tubuc/TubucTest.py", line 60, in Bus_Information
Builder.load_string(f.read())
File "C:\Users\mg\Anaconda3\lib\site-packages\kivy\lang\builder.py", line 368, in load_string
parser = Parser(content=string, filename=fn)
File "C:\Users\mg\Anaconda3\lib\site-packages\kivy\lang\parser.py", line 392, in __init__
self.parse(content)
File "C:\Users\mg\Anaconda3\lib\site-packages\kivy\lang\parser.py", line 501, in parse
objects, remaining_lines = self.parse_level(0, lines)
File "C:\Users\mg\Anaconda3\lib\site-packages\kivy\lang\parser.py", line 605, in parse_level
level + 1, lines[i:], spaces)
File "C:\Users\mg\Anaconda3\lib\site-packages\kivy\lang\parser.py", line 615, in parse_level
'Invalid property name')
kivy.lang.parser.ParserException: Parser: File "<inline>", line 28:
...
26: rows: root.numberoflist
27:
>> 28: for i in range(root.businfolist):
29: Label:
30: font_name: 'C:/Windows/Fonts/HYWULM'
...
Invalid property name
Add import statement, #:import Label kivy.uix.label.Label
Use on_parent event to execute the for loop in kv file
Valid expressions
There are two places that accept python statements in a kv file:
after a property, which assigns to the property the result of the expression (such as the text of a button as shown above) and
after a on_property, which executes the statement when the property is updated (such as on_state).
main.py
from kivy.lang import Builder
from kivy.base import runTouchApp
runTouchApp(Builder.load_string('''
#:kivy 1.11.0
#:import Label kivy.uix.label.Label
GridLayout:
cols: 3
on_parent:
for i in range(10): txt = "Label {0}".format(i); self.add_widget(Label(text = txt, text_size=(cm(2), cm(2)), pos=self.pos,
id=txt, color=(1,1,1,1)))
'''))
Output
I've been banging my head off this particular wall for days now. None of the relevant articles here have worked, so I really need to know where I'm going wrong. The relevant sections of code are:
def print_scb(myscb):
print(myscb)
scb_string = ''
scb_list = [ 'Combat' , 'Communication' , 'Manipulation' , 'Mental' , 'Perception', 'Physical' ]
for s in scb_list:
zz = str(myscb[s])
scb_string = scb_string + s + " " + zz + "% "
return scb_string
And the inline KV section is:
<CharacterSheet>:
BoxLayout:
orientation: 'vertical'
Label:
text:'Stats'
Label:
text: app.print_stats()
Label:
text:'Rolls'
Label:
text: app.print_rolls()
Label:
text:'SCM'
Label:
text: app.print_scm()
Label:
text:'SCB'
Label:
text: app.print_scb()
Button:
text:'Quit'
on_press: sys.exit()
Button:
text:'Back to Start'
on_press: root.manager.current = 'welcome'
""")
The program crashes with:
kivy.lang.BuilderException: Parser: File "<inline>", line 114:
...
112: text:'SCB'
113: Label:
>> 114: text: app.print_scb()
115: Button:
116: text:'Quit'
...
BuilderException: Parser: File "<inline>", line 114:
...
112: text:'SCB'
113: Label:
>> 114: text: app.print_scb()
115: Button:
116: text:'Quit'
...
AttributeError: 'NoneType' object has no attribute 'bind'
File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 1742, in create_handler
return eval(value, idmap), bound_list
File "<string>", line 114, in <module>
File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 935, in __getattribute__
object.__getattribute__(self, '_ensure_app')()
File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 930, in _ensure_app
app.bind(on_stop=lambda instance:
File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 2115, in _apply_rule
rctx['ids'])
File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 1747, in create_handler
cause=tb)
The whole code has been posted to dpaste at BrpGui.py
The print_scb() functions returns a string.
Any ideas where I'm going wrong?
Colin
As you mentioned in the comments.
print_scb is not a global function. It is defined in the CharacterSheet(Screen): class.
When you defined the function you forgor self.
So instead of def print_scb(myscb): it shall be def print_scb(self, myscb):
Also in your kv file, dont use app.print_scb() but root.print_scb()
Since it belongs to that class.
But since your function wants a parameter, it will throw an error. So you need to call it with a parameter. like this root.print_scb(some_variable)
From the docs https://kivy.org/docs/guide/lang.html -
There are three keywords specific to Kv language:
app: always refers to the instance of your application.
root: refers to the base widget/template in the current rule
self: always refer to the current widget
I want to edit a text in a TextInput in kivy but don't know how to, I've tried this code as Ive search in the net but still no luck.
class MainApp(Screen):
def __init__(self,**kwargs):
super(MainApp,self).__init__(**kwargs)
pass
class Forms(BoxLayout):
def __init__(self, **kwargs):
super(Main,self).__init__(**kwargs)
self.ids.txtSearch.text = "new text"
class Main(App):
def build(self):
return root_widget
if __name__ == "__main__":
Main().run()
------kivy-------
<Main>:
TextInput:
id: txtSearch
this is not my whole code but I think those are what matters in the issue
this is the error:
File "C:\Users\Guest\Documents\Python\Kivy\Project\main.py", line 295, in <module>
''')
File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1828, in load_string
self._apply_rule(widget, parser.root, parser.root)
File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1985, in _apply_rule
self.apply(child)
File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1872, in apply
self._apply_rule(widget, rule, rule)
File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1986, in _apply_rule
self._apply_rule(child, crule, rootrule)
File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1986, in _apply_rule
self._apply_rule(child, crule, rootrule)
File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1983, in _apply_rule
child = cls(__no_builder=True)
File "C:\Users\Guest\Documents\Python\Kivy\Project\main.py", line 40, in __init__
self.ids.txtSearch.text = "new text"
File "kivy\properties.pyx", line 720, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:10911)
AttributeError: 'super' object has no attribute '__getattr__'
To change Widgets properties in kivy you need to 'link' the widget between .py and .kv file, first in .py file:
txt_Search = ObjectProperty()
then in .kv file, in your root widget:
txt_Search: txtSearch
then assign the id to a widget (as you already did):
<Main>:
TextInput:
id: txtSearch
text: ' '
then in your .py file, you can change attributes of the widget by doing this:
self.txt_Search.text = 'New Text'
or any other attribute:
self.txt_Search.height = '30dp'
Are you sure self.ids.txtSearch exists when you try to assign a text to it? You're calling super(Main,self) one line above so I guess txtSearch is never instantiated.
Btw, it's better to init widgets in *.kv files:
<Main>:
TextInput:
id: txtSearch
text: "new text"