return a BoxLayout inside another one in Kivy? - python-3.x

I'm trying to make a simple application in Python 3.5 and kivy that starts with a simple screen and when you click on it, goes to another one which shows 3 lists that let you chose the data:
The Python file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListItemButton
from kivy.properties import ListProperty
from dataTopy import rlists
# Transition des ecrans:
class MainScreen(Screen):
pass
class AnotherScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("ex44.kv")
#
class FirstListItemButton(ListItemButton):
pass
class SecondListItemButton(ListItemButton):
pass
class ThirdListItemButton(ListItemButton):
pass
class Ex44(BoxLayout):
d1 = ListProperty([str(i) for i in range(1990,2014)] )
d2 = ListProperty(['']*100)
d3 = ListProperty(['']*100)
def change(self,c):
try: self.d2,self.d3 = rlists(int(c.text))
except:
import os
CurDir = os.getcwd()
print('Can not find data in ' + CurDir)
def change1(self,c):
print('M => '+c.text)
def change2(self,c):
print('F => '+c.text)
class Ex44App(App):
def build(self):
return presentation
if __name__ == '__main__':
Ex44App().run()
The kivy file:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ex44 ex44
ScreenManagement:
transition: FadeTransition()
MainScreen:
AnotherScreen:
<MainScreen>:
name: "main"
Button:
on_release: app.root.current = "other"
text: "Next Screen"
font_size: 50
<AnotherScreen>:
name: "other"
BoxLayout:
Ex44
Button:
color: 0,1,0,1
font_size: 25
size_hint: 0.3,0.2
text: "Back Home"
on_release: app.root.current = "main"
pos_hint: {"right":1, "top":1}
<FirstListItemButton>:
on_press: app.root.change(*args)
<SecondListItemButton>:
on_press: app.root.change1(*args)
<ThirdListItemButton>:
on_press: app.root.change2(*args)
<Ex44>:
ListView:
adapter:
ListAdapter(data=root.d1,
selection_mode='single',
cls=ex44.FirstListItemButton)
ListView:
adapter:
ListAdapter(data=root.d2,
selection_mode='single',
cls=ex44.SecondListItemButton)
ListView:
adapter:
ListAdapter(data=root.d3,
selection_mode='single',
cls=ex44.ThirdListItemButton)
When I try to run the app, it tells me: "Unknown class "
It is weird because the class Ex44 works alone but not when I'm trying to add it into the main application logic.
I've tried to return a widget instead of a BoxLayout for the class, to return Ex44 alone in the kivy file, etc. but I always get the same error in return.
Is it possible to return a BoxLayout inside another one in Kivy?

You are building the kv file too soon (before the class is defined). move the Builder.from_file call to the build method
...
def build(self):
return Builder.load_file("ex44.kv")

Related

How to use Splitters in kivy with multiple screens?

This is a simple example but essentially I need to use Splitter to separate and resize two individual Text input boxes horizontally, however when I'm trying to use the Splitter widget in a Screen its creating a double behind the original content and the Splitter is not functioning properly. Any help will be appreciated. Thank You.
main file
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager , Screen
from kivy.core.window import Window
from kivy.factory import Factory
class TestScreen1(Screen):
pass
class TestScreen2(Screen):
pass
class Manager(ScreenManager):
pass
class MyApp(App):
def build(self):
return Builder.load_file("D:\MainProject\TestFiles\my.kv")
if __name__ == "__main__":
MyApp().run()
KV file
Manager:
TestScreen1:
TestScreen2:
<TestScreen1>:
name:"abc1"
BoxLayout:
orientation: 'horizontal'
Label:
text:"Next"
Splitter:
sizable_from:"left"
Button:
text: "next"
on_press: root.manager.current="abc2"
<TestScreen2>:
name:"abc2"
BoxLayout:
orientation: 'horizontal'
Label:
text:"pre"
Splitter:
sizable_from:"left"
Button:
text: "pre"
on_press: root.manager.current="abc1"
Your kv file is being loaded twice. Once by you Builder.load_file() and once by the App (see documentation). Just remove that Builder.load_file() line.

A non-existent line

I'm learning Kivy and I'm on the MDExpansionPanel widget.
Data source from a JSON I use the keys to assemble my panels and the values to compose my Contents.
It happens that I'm able to do it but an extra line always appears in my contents.
I would like your help to delete this line.
I will post my code below:
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelOneLine
from kivy.properties import StringProperty, ObjectProperty
json_data = {'01001BA476': {'price': '74.73',
'product_code': '000003',
'quantity': '100'},
'0100251633': {'price': '92.07',
'product_code': '000156',
'quantity': '1000'}}
KV = '''
<ClassDetails>
orientation: 'vertical'
adaptive_height: True
OneLineIconListItem:
id: info_line
text: root.text
on_press: root.action()
IconLeftWidget:
icon: 'star'
on_press: print(f'star pressed on line: {info_line.text}')
ScrollView:
MDGridLayout:
id: box
cols: 1
adaptive_height: True
'''
class ClassDetails(MDBoxLayout):
text = StringProperty()
action = ObjectProperty()
class InvoicePanel(MDExpansionPanel):
pass
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
def fechar_pedido(self):
print('You clicked on the information line')
def on_start(self):
for class_title, class_details in json_data.items():
cd = ClassDetails()
expansion_panel = InvoicePanel(panel_cls=MDExpansionPanelOneLine(text=f'Invoice #: {class_title}'), content=cd)
self.root.ids.box.add_widget(expansion_panel)
for item in class_details.items():
cd.add_widget(ClassDetails(
text=str(class_details.values()), action=self.fechar_pedido))
Test().run()
I think the extra line is coming from setting your initial content to a ClassDetails instance, which has a OneLineIconListItem in its definition. Try replacing the content with a simple MDBoxLayout instead:
def on_start(self):
for class_title, class_details in json_data.items():
print(class_title, class_details)
# cd = ClassDetails()
cd = MDBoxLayout(orientation='vertical', adaptive_height=True)
expansion_panel = InvoicePanel(panel_cls=MDExpansionPanelOneLine(text=f'Invoice #: {class_title}'), content=cd)
self.root.ids.box.add_widget(expansion_panel)
for item in class_details.items():
print('\titem:', item)
cd.add_widget(ClassDetails(
text=str(class_details.values()), action=self.fechar_pedido))

Python - Kivy: Label does not update during a function excution

I saw that there are other topics related to this one but I could make this simple project to work.
I need a label to be updated with the function status while it is running just to let the user know that the program still working while the function is processing, but the label is only updated when the function is completed
Here are the files
my.kv
WindowManager:
ChangeLabel:
<ChangeLabel>:
BoxLayout:
orientation: 'vertical'
padding: 200, 20
Label:
id: labeltxt
text: 'Text to change'
font_size: 30
height: 50
Button:
text:'Run Function'
size_hint_y: None
height: 30
on_release:
root.changeLabel()
Label:
test.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.core.window import Window
import time
from kivy.clock import Clock
class ChangeLabel(Screen):
def changeLabel(self):
Clock.schedule_once(self.test)
def test(self, dt):
self.ids.labeltxt.text = 'Function is running...'#I wanna this in lablel while function is running
time.sleep(2)# function to run
self.ids.labeltxt.text = 'Completed'
class WindowManager (ScreenManager):
pass
kv = Builder.load_file('my.kv')
class MyApp(App):
def build(self):
return kv
MyApp().run()
I did this solution, now it is working the way I want, but not sure if this is the best way.
test.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.core.window import Window
import time
from kivy.clock import Clock
class ChangeLabel(Screen):
i = 0
def changeLabel(self):
Clock.schedule_interval(self.test, 0.05)
def test(self, dt):
if self.i == 0:
self.ids.labeltxt.text = 'Function is running...'
self.i = 1
else:
time.sleep(2)# function to run
self.ids.labeltxt.text = 'Completed'
self.i = 0
Clock.unschedule(self.test)
class WindowManager (ScreenManager):
pass
kv = Builder.load_file('my.kv')
class MyApp(App):
def build(self):
return kv
MyApp().run()

Changing button text with popup text input kivy

So, I'm new to kivy and can't get this code to work. I'm trying to update a button text with a popup text input. The popup shows when the button is pressed, and when it's dismissed it should update the text on the button with whatever text was typed in it.
I've tried many variations of this code, but no one have worked. Either nothing happens or I get this error:
AttributeError: 'super' object has no attribute '__getattr__'
Here it is:
main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.popup import Popup
class MainWidget(GridLayout):
pass
class PopText(Popup):
def textChange(self):
MyButton().change()
def getText(self):
text = self.display
return text
class MyButton(AnchorLayout):
def change(self):
self.ids.equip_bt.text = PopText().getText()
print(self.ids.my_bt.text)
class MyApp(App):
def build(self):
return MainWidget()
if __name__ == "__main__":
MyApp().run()
my.kv
#:kivy 1.10.0
#:import F kivy.factory.Factory
<PopText>:
size_hint: .7, .3
title: "Input Text"
on_dismiss: self.textChange()
display: pop_text.text
TextInput:
id: pop_text
focus: True
multiline: False
on_text_validate: root.dismiss()
<MyButton>:
anchor_y: "top"
anchor_x: "right"
Button:
id: my_bt
text: "Input Text"
on_release: F.PopText().open()
<MainWidget>:
cols: 1
rows: 2
MyButton:
MyButton:
Any ideas on how to solve this?
Here is a minimum example of what you are trying to achieve. The hard part is connection the button from the Popup to the Button which opened it. I am going through the app class to achieve that. I got to admit it is not a pretty solution.
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
class PopupBttn(Button):
def openPopup(self):
Pop = TextPopup().open(self.text)
class TextPopup(Popup):
def open(self, text, **kwargs):
super(TextPopup, self).open(**kwargs)
self.ids.txtipt.text = text
class MyApp(App):
pass
if __name__ == "__main__":
MyApp().run()
kv file:
BoxLayout:
PopupBttn:
id: bttn
text: 'open Popup'
on_press: self.openPopup()
<TextPopup>:
BoxLayout:
orientation: "vertical"
TextInput:
id: txtipt
Button:
text: "OK"
on_release: app.root.ids.bttn.text=root.ids.txtipt.text
on_release: root.dismiss()
Here is an updated version to use multiple buttons. Unfortunately, you will need to set ids and name to the string of id per Button.
python file
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.properties import StringProperty
class PopupBttn(Button):
def openPopup(self):
print(self.name)
Pop = TextPopup().open(self.text, self.name)
class TextPopup(Popup):
bttnid = StringProperty()
text = StringProperty()
def open(self, text, id, **kwargs):
super(TextPopup, self).open(**kwargs)
self.ids.txtipt.text = text
self.bttnid = id
def setText(self):
App.get_running_app().root.ids[self.bttnid].text = self.text
class MyApp(App):
pass
if __name__ == "__main__":
MyApp().run()
kv file
BoxLayout:
orientation: 'vertical'
PopupBttn:
name: 'one'
id: one
text: 'I am the first Button'
PopupBttn:
name: 'two'
id: two
PopupBttn:
name: 'three'
id: three
PopupBttn:
name: 'four'
id: four
text: 'I am the fourth button'
<PopupBttn>:
text: 'open Popup'
on_press: self.openPopup()
<TextPopup>:
text: txtipt.text
BoxLayout:
orientation: "vertical"
TextInput:
id: txtipt
Button:
text: "OK"
on_release: root.setText()
on_release: root.dismiss()
In order to update both buttons, you need to assign unique id to each of them. Please refer to the example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
class MainWidget(GridLayout):
btn_top = ObjectProperty()
btn_bottom = ObjectProperty()
class PopText(Popup):
pass
class MyButton(AnchorLayout):
my_btn = ObjectProperty()
class TestApp(App):
title = "Changing button text with popup text input Kivy"
def build(self):
return MainWidget()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.10.0
#:import F kivy.factory.Factory
<PopText>:
size_hint: .7, .3
title: "Input Text"
TextInput:
focus: True
multiline: False
on_text_validate:
app.root.btn_top.my_btn.text = self.text
app.root.btn_bottom.my_btn.text = self.text
root.dismiss()
<MyButton>:
my_btn: my_bt
anchor_y: "top"
anchor_x: "right"
Button:
id: my_bt
text: "Input Text"
on_release: F.PopText().open()
<MainWidget>:
btn_top: btn_top
btn_bottom: btn_bottom
cols: 1
rows: 2
MyButton:
id: btn_top
MyButton:
id: btn_bottom
Output

How to load string from text file to kivy label, Python 3.5

I've been searching around for answers on stackoverflow for a couple of days now to be honest but I could not find the thing for me, lets say I have a text file named bind.txt with a couple lines of text, how can i load that text file to a kivy label? be it directly or indirectly. I've been trying to teach myself python and this is kind of in the way of me building my first app. Thank you in advance, and heres the code.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class MainScreen(Screen):
pass
class MainLabel(ScrollView):
text = StringProperty("")
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("bind.kv")
class MainApp(App):
def build(self):
return presentation
if __name__ == "__main__":
MainApp().run()
And the kv file.
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
<MainLabel>:
text: #bind.txt here, somehow..
Label:
text: root.text
font_size: 15
text_size: self.width, None
size_hint_y: None
height: self.texture_size[1]
<MainScreen>:
name: "main"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'img/Fundal.png'
MainLabel
You can open file in reading mode, store contents in a variable and assign it to the text property.
For an example
with open("bind.txt") as f:
contents = f.read()
main_label.text = contents # main_label is an instance of kivy's Label class.

Resources