Here is my kivyfile.kv
WindowManager:
FirstWindow:
SecondWindow:
<FirstWindow>:
name: "first"
AnchorLayout:
anchor_x: "left"
anchor_y: "top"
BoxLayout:
cols:2
orientation: 'vertical'
size_hint: None, None
size: self.minimum_size
Button:
text: "Button 1"
size_hint: (None,None)
width:200
height:30
Button:
text: "Button2"
size_hint: (None,None)
width:200
height:30
TextInput:
id: url
multiline: False
size_hint: (None,None)
width:200
height:30
TextInput:
id: url2
multiline: False
size_hint: (None,None)
width:200
height:30
Here is my .py file:
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class FirstWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv=Builder.load_file('kivyfile.kv')
class Downloader(App):
def build(self):
return kv
if __name__ == '__main__':
Downloader().run()
What I am trying to achive, is to get a BoxLayout at the top left corner, which has two columns - Button and TextInput NEXT TO it. However, it only does all components one under each other. I have also a future question on how to add another BoxLayout NEXT TO the previous BoxLayout that I have. As in - how to make it be on the right side of existing BoxLayout, how to position it.
Related
I want to remove a Boxlayout in my widget, but I cant.
There is a Boxlayout at the begining of the app called "Speca". After my sellections it must be removed.
When you run the app, sellect something in "Home" Spinner and click any of the new Toggle buttons.
Wtih the press of any of the Toggle buttons "Speca" must disappear.
Please help.
Here is main.py:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.spinner import Spinner
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty
from kivy.properties import ListProperty
from collections import OrderedDict
from kivy.uix.togglebutton import ToggleButton
data1=["mother","father","son"]
data2=["uncle","aunt","grandfather"]
data3=["jack","mike","simon"]
data4=["1898","1975","1985","1885"]
amd="0dp"
class MainWidget(Widget):
def remove_layout(self, *ignore):
self.remove_widget(self.layout)
global amd
an0=tuple(list(OrderedDict.fromkeys(data1)))
cal5= ObjectProperty()
cal6= ObjectProperty()
def btn10(self,text):
if self.cal5:
self.cal5.parent.remove_widget(self.cal5)
self.cal5 =ModelSpecifications()
a=data2
b=data3
c=data4
mi=[]
n=0
while n < len(a):
aba=n
mi.append(aba)
n+=1
for i in mi:
self.b1=MyTButton(text=str(i),size_hint=(1,None),height="100dp",group="selections")
self.cal5.add_widget(self.b1)
self.ids.scd.add_widget(self.cal5, index=3)
def on_state(self, togglebutton): #<----THIS IS THE TUGGLEBUTTON I WANT USE
global amd
tb = togglebutton
text1=tb.text
if text1=="0":
amd="50dp"
elif text1=="1":
amd="100dp"
else:
amd="200dp"
if self.cal6:
self.cal6.parent.remove_widget(self.cal6)
self.cal6 =Spec()
self.ids.scd.add_widget(self.cal6, index=2)
class SecondPage(ScrollView):
pass
class Speca(BoxLayout): #<------ THIS IS THE BOXLAYOUT I WANT TO REMOVE
pass
class Spec(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.size_hint=(1,None)
self.height=amd
class MyTButton(ToggleButton):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class ModelSpecifications(BoxLayout): #this is the class I want add after my spinner selection
pass
class Calculation(GridLayout):
pass
class MyApp(App):
pass
MyApp().run()
here is my.kv :
MainWidget:
<MainWidget>:
hideable: hideable
ScreenManager:
id: scmanager
size: root.width, root.height
Screen:
id: scndpage
name: "second"
SecondPage:
Calculation:
id:scd
cols:1
height: self.minimum_height
row_default_height: "70dp"
size_hint_y: None
spacing:"10dp"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
size_hint: 1, None
height: "50dp"
pading:"10dp"
spacing:"10dp"
orientation: "vertical"
BoxLayout:
orientation: "horizontal"
Label:
text:"Name:"
color: 0,0,0,1
TextInput:
text:"---"
color: 0,0,0,1
Label:
text:"Surname:"
color: 0,0,0,1
TextInput:
text:"-----"
color: 0,0,0,1
BoxLayout:
id:scdd
size_hint: 1, 1
height: "100dp"
orientation: "vertical"
BoxLayout:
size_hint: 1, None
height: "50dp"
orientation: "horizontal"
Label:
text: " Sellection:"
color: 0,0,0,1
Spinner:
text: 'Home'
values: root.an0
on_text: app.root.btn10(self.text)
Speca: #<------ THIS IS THE BOXLAYOUT I WANT TO REMOVE
Button:
text:" Calculate"
Button:
text:"Sellect"
Button:
text:"Back"
<ModelSpecifications>:
id:anss
pading:"10dp"
spacing:"10dp"
size_hint: 1, None
height: "100dp"
orientation: "horizontal"
<MyTButton#ToggleButton>: #<----THIS IS THE TUGGLEBUTTON I WANT USE
on_state:
app.root.on_state(self)
<Speca>: #<------ THIS IS THE BOXLAYOUT I WANT TO REMOVE
orientation:"vertical"
Label:
color: (1,1,0,1)
text:"I WANT TO REMOVE THIS PART WHEN I PRESS ANY OF TOGGLE BUTTONS"
Please run the app and see it.
Looks to me like speca is a child of the calculation widget with ID scd. So give speca an ID and then Remove speca via ids in on_srate python function.
Kv
SecondPage:
Calculation:
id:scd
speca:
id: speca
py
def on_state():
self.root.ids.scd.remove_widget(self.root.ids.speca)
YES.
When I make those changes;
Kv
SecondPage:
Calculation:
id:scd
speca:
id: speca
py
def on_state():
self.ids.scd.remove_widget(self.ids.speca)
It works.
I have a kind of major problem here. I want to create a list at the bottom of my app. I have a dictionary which works on this guy's solution: How to put multiple columns into a kivy RecycleView?
HOWEVER, I want to add that widget with button. Everything looks fine but I have problem with ids.
Please take a look at it, "FinalList" class cant find variables.
When you run it click "Calculate selections" it adds another class which has another button called "Confirm". Click "Confirm", it should add list at the bottom but it does not.
here is my main.py code;
from kivy.animation import Parallel
from kivy.app import App
from kivymd.app import MDApp
from kivy.uix.gridlayout import GridLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.uix.checkbox import CheckBox
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.lang import Builder
from functools import partial
from kivy.properties import ObjectProperty, StringProperty, BooleanProperty
#Window.size=((640/1.5),(960/1.5))
items = [{'SP1': 'Artikelnummer', 'SP2': 'Name', 'SP3': 'Groesse'},
{'SP1': '510001', 'SP2': 'Big Pump', 'SP3': '1.50 L'},
{'SP1': '523001', 'SP2': 'Leonie Still', 'SP3': '1.50 L'},
{'SP1': '641301', 'SP2': 'Cola Mix', 'SP3': '1.50 L'}
]
class MainWidget(Widget):
cal2= ObjectProperty()
cal4= ObjectProperty()
def btn4(self):
self.cal2 = MaterialS() #MaterialS is my class with different widget
self.ids.scd.add_widget(self.cal2, index=0) #scd is the id of my main widget, index =0 help me to insert to the bottom
def btn8(self):
self.cal4 = RV() **#HERE I CALL THE RV CLASS**
self.ids.scd.add_widget(self.cal4, index=0)
class SecondPage(ScrollView):
pass
class Calculation(GridLayout):
pass
class MaterialS(BoxLayout):
def btn7(self,x):
self.ids[x].disabled= True
def btn9(self,x):
self.ids[x].disabled= True
class FinalList(BoxLayout):
pass
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.data = [{'spalte1_SP': str(x['SP1']), 'spalte2_SP': str(x['SP2']), 'spalte3_SP': str(x['SP3'])} for x in items]
class MyApp(MDApp):
pass
MyApp().run()
here is my.kv file;
MainWidget:
<MainWidget>:
ScreenManager:
id: scmanager
size: root.width, root.height
Screen:
id: scndpage
name: "second"
SecondPage:
Calculation:
id:scd
cols:1
height: self.minimum_height
row_default_height: "70dp"
size_hint_y: None
spacing:"10dp"
canvas.before:
Rectangle:
pos: self.pos
size: self.size
Button:
id: cslect
text:"Calculate Selections"
on_press: app.root.btn4()
<MaterialS>:
pading:"10dp"
spacing:"10dp"
size_hint: 1, None
height: "250dp"
orientation:"vertical"
BoxLayout:
size_hint: 1, 1
orientation:"horizontal"
Label:
text:"cutter head material"
color: 0,0,0,1
Button:
id: btnStd
text:"Confirm"
on_press: app.root.btn8()
on_release: root.btn9("btnStd")
<Optionals>:
BoxLayout:
orientation: "vertical"
Button:
id: btnStd
text:"Confirm"
on_press: app.root.btn8()
on_release: root.btn9("btnStd")
<FinalList>:
orientation: 'horizontal'
spalte1_SP: 'spalte1'
spalte2_SP: 'spalte2'
spalte3_SP: 'spalte3'
Label:
id: SP1
text: app.root.spalte1_SP **#HERE IS THE MAIN PROBLEM**
Label:
id: SP2
text: app.root.spalte2_SP **#HERE IS THE MAIN PROBLEM**
Label:
id: SP3
text: app.root.spalte3_SP **#HERE IS THE MAIN PROBLEM**
<RV>:
viewclass: 'FinalList'
RecycleBoxLayout:
default_size: None, dp(20)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
You just need to correct your references to the properties of the FinalList. Like this:
<FinalList>:
orientation: 'horizontal'
spalte1_SP: 'spalte1'
spalte2_SP: 'spalte2'
spalte3_SP: 'spalte3'
Label:
id: SP1
text: root.spalte1_SP #**#HERE IS THE MAIN PROBLEM**
Label:
id: SP2
text: root.spalte2_SP #**#HERE IS THE MAIN PROBLEM**
Label:
id: SP3
text: root.spalte3_SP #**#HERE IS THE MAIN PROBLEM**
this code returns me a black screen, what am I doing wrong?
python file:
from kivy.app import App
from kivy.metrics import dp
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.pagelayout import PageLayout
class myPageLayout(PageLayout):
pass
class myScrollView(ScrollView):
pass
class myStackLayout(StackLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
for i in range(1,101):
size=dp(100)
b= Button(text=str(i),size_hint=(None,None),size=(size,size))
self.add_widget(b)
class myGridLayout(GridLayout):
pass
class myAnchorLayout(AnchorLayout):
pass
class myBoxLayout(BoxLayout):
pass
class myWidget(Widget):
pass
class gameApp(App):
pass
gameApp().run()
kivy file:
myPageLayout:
<myBoxLayout>:
orientation: "horizontal"
spacing: "10dp"
Button:
text: "ciao"
size_hint: 1,.5
size: "10dp","10dp"
pos_hint: { "y": .5 }
BoxLayout:
orientation: "vertical"
spacing: "10dp"
Button:
text: "b1"
Button:
text: "b2"
Button:
text: "b3"
Button:
text: "hi2"
<myWidget>:
Button:
text: "hi"
size: "100dp","20dp"
pos: 300, 100
<myAnchorLayout>:
#anchor_x: "right"
anchor_y: "bottom"
BoxLayout:
size_hint: .1,.1
Button:
text: "hi"
Button:
text: "hi2"
<myGridLayout>:
cols: 3
rows: 3
Button:
text:"hi"
size_hint:.5,1
Button:
text:"hi"
Button:
text:"hi"
myAnchorLayout:
size_hint:.5,1
Button:
text:"hi"
myBoxLayout:
Button:
text:"hi"
size_hint:.5,1
myWidget:
Button:
text:"hi"
<myStackLayout>:
#orientation:"tb-lr"
#padding:("20dp","20dp","20dp","20dp")
#spacing: "10dp","10dp"
<myScrollView>:
myStackLayout:
size_hint:1,None
height: self.minimum_height
<myPageLayout>:
myBoxLayout:
myAnchorLayout:
myWidget:
myGridLayout:
singularly layouts work and even if embedded like in the case of "myAnchorLayout" and "myGridLayouts".
I also tried to create a new StackLayout in "myScrollView" this way:
<myScrollView>:
StackLayout:
size_hint:1,None
height: self.minimum_height
Button:
text:"hi"
size_hint:.7,None
height:"500dp"
Button:
text:"hi"
size_hint:.7,None
height:"500dp"
and it worked, but now it doesn't work anymore, this appened with "myPageLayout" too.
The problem is that the kv language insists that class names start with a capital letter. The documentation says:
Keep class names capitalized to avoid syntax errors
I think your code will work if you change all your class names to meet that requirement.
.
Im trying to save data from one page of my app into a .json file. There are 2 checkboxes and one text input area. I only wish to save one or the other as not all of them need to be saved. Just the selected one.
Ive tried reading the kivy docs, and ive tried various examples from here and on the web, and i still cannot save my data as intended into a json file.
from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.properties import ListProperty, ObjectProperty
from kivy.uix.checkbox import CheckBox
from kivy.uix.button import Button
from kivy.storage.jsonstore import JsonStore
from kivy.clock import Clock
class MyScreenManager(ScreenManager):
pass
class FirstScreen(Screen):
pass
class Who(Screen):
def __init__(self, **kwargs):
super(Who, self).__init__(**kwargs)
self.store = JsonStore("who.json")
Clock.schedule_once(lambda *args: self.load())
def save(self):
self.store.put('who',
mum = self.mum.checkbox,
dad = self.dad.checkbox,
other = self.other.text)
def load(self):
if self.store.exists('who'):
who = self.store.get('who')
w = [("mum", self.mum), ("dad", self.dad), ("other", self.other)]
for key, ti in v:
val = who.get(key)
if val:
ti.text = val
root_widget = Builder.load_file("test.kv")
runTouchApp(root_widget)
and my kv doc looks like this:
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import StringProperty kivy.properties.StringProperty
#:import ObjectProperty kivy.properties.ObjectProperty
#:import Label kivy.uix.label.Label
#:import Window kivy.core.window.Window
#:import ListProperty kivy.properties.ListProperty
#:import CheckBox kivy.uix.checkbox.CheckBox
#:import TextInput kivy.uix.textinput.TextInput
#:import Button kivy.uix.button.Button
#:import BoxLayout kivy.uix.boxlayout.BoxLayout
#:import GridLayout kivy.uix.gridlayout.GridLayout
#:import CheckBox kivy.uix.checkbox.CheckBox
MyScreenManager:
transition: FadeTransition()
Who:
FirstScreen:
<Who>:
name: 'who'
mum: mum
dad: dad
other: other
GridLayout:
cols: 2
row_force_default: True
row_default_height: '50dp'
Label:
text: 'Mum'
font_size: 30
CheckBox:
id: mum
on_press: root.save(mum.active, mum.text)
Label:
text: 'Dad'
font_size: 30
CheckBox:
id: dad
on_press: root.save(dad.active, dad.text)
Label:
text: 'Someone Else'
font_size: 30
TextInput:
id: other
multiline: False
font_size: 30
Button:
text: 'Book'
font_size: 30
valign: 'middle'
on_press: root.save()
Button:
text: 'Cancel'
font_size: 30
valign: 'middle'
on_press: root.manager.current = 'first'
<FirstScreen>:
name: 'first'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Go Back'
font_size: 40
BoxLayout:
Button:
text: 'Back To Who'
font_size: 40
on_press: root.manager.current = 'who'
The outcome usually revolves around this:
File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.getattr
AttributeError: 'CheckBox' object has no attribute 'text'
Expected outcome is obviously saved json data. I need to be able to select one or the other or even all at once. I have tried all I can find. My cat is scared of me. I need help. Thank you.
I am trying to create a simple Chatbot application UI using Kivy and Python but I'm stuck at the very first stage.
How do I access the TextInput widget inside the BoxLayout to fetch it's contents?
I read that the function on_text_validate() is called whenever you press 'Enter' on the TextInput. However this code down below doesn't seem to be functioning.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.lang import Builder
Builder.load_string('''
#:import AnchorLayout kivy.uix.anchorlayout
#:import Layout kivy.uix.layout
<ChatBotScreen>:
BoxLayout:
orientation: 'vertical'
ScrollView:
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
botOutput: root.botOutput
ScrollView:
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
userInput: root.userInput
TextInput:
id: ti_userinput
multiline: False
''')
class ChatBotScreen(Screen):
userInput = StringProperty()
botOutput = StringProperty()
def on_text_validate(self):
text_input_userInput = self.ids['ti_userinput'].text
self.ids['ti_userinput'].text = ''
print(text_input_userInput)
def UserInput(self):
pass
def BotOutput(self):
pass
sm = ScreenManager(transition=FadeTransition())
sm.add_widget(ChatBotScreen(name='mainchat'))
class MyApp(App):
def build(self):
return sm
Please guide me.
The problem in your case is that on_text_validate is not a method of the Screen class, but of TextInput, so you will never call on_text_validate of the ChatBotScreen class, what you can do is invoke that method from the on_text_validate event of the TextInput:
Builder.load_string('''
#:import AnchorLayout kivy.uix.anchorlayout
#:import Layout kivy.uix.layout
<ChatBotScreen>:
BoxLayout:
orientation: 'vertical'
ScrollView:
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
botOutput: root.botOutput
ScrollView:
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
userInput: root.userInput
TextInput:
id: ti_userinput
multiline: False
on_text_validate: root.on_text_validate()
''')
class ChatBotScreen(Screen):
userInput = StringProperty()
botOutput = StringProperty()
def on_text_validate(self):
text_input_userInput = self.ids['ti_userinput'].text
self.ids['ti_userinput'].text = ''
print(text_input_userInput)
sm = ScreenManager(transition=FadeTransition())
sm.add_widget(ChatBotScreen(name='mainchat'))
class MyApp(App):
def build(self):
return sm