I'm making a kivy app to find the rhyming words for a word entered by the user. It displays all the rhyming words as OneLineListItems in an MDList which is inside a kivy RecycleView. On clicking on one of these OneLineListItems it displays the definition of the word on the right-hand side of the screen. However, when I click on a OneLineListItem its definition takes very long to appear and sometimes it lags so badly that the app closes. Am I doing something wrong or is it just my computer? Code below:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.label import MDLabel
from kivymd.uix.list import OneLineListItem
import pronouncing
import enchant
from PyDictionary import PyDictionary
dictionary = PyDictionary()
d = enchant.Dict("en_US")
kv = """
Screen:
input:input
scroll:scroll
word:word
defs:defs
MDGridLayout:
rows: 1
cols: 2
MDGridLayout:
rows: 2
cols: 1
MDFloatLayout:
MDTextField:
id:input
size_hint: (.4, None)
height: 26
multiline: False
on_text_validate: app.rhyme()
hint_text: "Search"
mode: "rectangle"
pos_hint: {"center_x": .25, "center_y": .85}
font_name: "DejaVuSans.ttf"
text_size: self.size
MDFloatLayout:
RecycleView:
size_hint: 0.85,1.5
bar_width: dp(15)
bar_height: dp(40)
scroll_type: ["content"]
pos_hint: {"center_x": 0.45, "center_y": 0.93}
MDList:
id: scroll
MDBoxLayout:
id:defs
orientation: "vertical"
md_bg_color: 0, 1, 0.2, 1
MDLabel:
id: word
text: ""
text_size: self.size
"""
class RhymeApp(MDApp):
played = []
x_turn = True
def build(self):
self.screen = Builder.load_string(kv)
return self.screen
def rhyme(self):
raw_rhymes = pronouncing.rhymes(self.screen.input.text)
rhymes = []
[rhymes.append(x) for x in raw_rhymes if x not in rhymes and x[-1] != "." and d.check(x)]
self.screen.scroll.clear_widgets()
for i in rhymes:
self.screen.scroll.add_widget(
OneLineListItem(text=f"{i}".capitalize(), on_release=self.dictionary)
)
def dictionary(self, btn):
nl = '\n'
self.screen.defs.clear_widgets()
self.screen.word.text = btn.text.capitalize()
meaning = dictionary.meaning(btn.text, disable_errors=True)
if meaning is None:
self.screen.defs.add_widget(
MDLabel(text=f"Sorry, no meaning for that word.",
text_size="self.size")
)
else:
for key in meaning:
self.screen.defs.add_widget(
MDLabel(text=f"Part of speech: {key} {nl}Meaning: {nl}{nl}{meaning[key][0].capitalize()}.",
text_size="self.size")
)
if __name__ == "__main__":
RhymeApp().run()
Can someone please help?
First create a custom class for the data-class like following:
class ListItem(OneLineListItem):
# Here define all the necessary attrs., methods apart from the defaults (if you need any).
Now in your .kv initialize RecycleView as,
RecycleView:
id: scroll
#size_hint: 0.85,1.5
bar_width: dp(15)
bar_height: dp(40)
scroll_type: ["content"]
#pos_hint: {"center_x": 0.45, "center_y": 0.93}
viewclass: "ListItem"
RecycleBoxLayout:
default_size: None, dp(48)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Now you are ready to feed RecycleView with you data as,
def rhyme(self):
...
self.screen.ids.scroll.data = [
{"text" : f"{i}".capitalize()}
for i in rhymes]
Related
I'm a beginner and would like to insert an MDSpinner while executing a loop, or a mysql query, or some process that takes time. I made this example below to illustrate my problem:
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.clock import Clock
import threading
KV = '''
MDScreen:
ScreenManager:
id: screen_manager
MDScreen:
name: 'a'
BoxLayout:
orientation: 'vertical'
MDLabel:
text: 'test - screen a'
pos_hint: {"center_x": .5, "center_y": .5}
Button:
id: btn
text: 'screen a'
size_hint: 0.5, 0.1
pos_hint: {"center_x": .5, "center_y": .5}
on_press:
load.active = True
app.aperta()
MDScreen:
name: 'b'
BoxLayout:
orientation: 'vertical'
MDLabel:
text: 'test - screen b'
pos_hint: {"center_x": .5}
Button:
id: btn2
text: 'screen b'
size_hint: 0.5, 0.5
pos_hint: {"center_x": .5, "center_y": .5}
MDSpinner:
id: load
active: False
size: dp(64), dp(64)
size_hint: None, None
pos_hint: {'center_x': 0.5, "center_y": .5}
'''
class Example(MDApp):
def build(self):
self.tela = Builder.load_string(KV)
return self.tela
def aperta(self):
threading.Thread(target=self.op_1).start()
def op_1(self, *args):
cont = 0
while cont < 1000000:
cont += 1
print(cont)
self.tela.ids.load.active = False
self.tela.ids.screen_manager.current = 'b'
Example().run()
however, when I change the screen, the following error appears and all the elements of the two screens overlap...
TypeError: Cannot change graphics instruction outside the main Kivy thread
You can also use the mainthread decorator, which is the equivalent to what #ApuCoder suggested:
class Example(MDApp):
def build(self):
self.tela = Builder.load_string(KV)
return self.tela
def aperta(self):
threading.Thread(target=self.op_1).start()
def op_1(self, *args):
cont = 0
while cont < 1000000:
cont += 1
print(cont)
self.go_to_b()
#mainthread
def go_to_b(self):
self.tela.ids.load.active = False
self.tela.ids.screen_manager.current = 'b'
from kivy.app import App
from kivy.graphics import Line
from kivy.metrics import dp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivymd.app import MDApp
class ScrollViewExample(ScrollView):
pass
class FirstWindow(Screen):
def set_text(self):
my_title_input = self.ids.note_title
my_details = self.ids.note_details
fo = open("notes.txt", "a")
fo.write('\n'+'\n'+my_title_input.text)
fo.write('\n'+my_details.text)
fo.close()
class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
def on_toggle_button_state(self):
dark_mode = self.ids.dark_mode
dark_mode.text = 'Feature not available'
pass
class FourthWindow(Screen):
#This class displays notes from a file in buttons
#works but only the notes showup
def build(self):
root = FourthWindow()
root.add_widget(FourthWindow(name='StackLayout'))
class StackLayoutExample(StackLayout):
class ScrollView(ScrollView):
pass
def __init__(self, **kwargs):
super().__init__(**kwargs)
layout = StackLayout(orientation='lr-tb')
with open('notes.txt', 'r') as fp:
data = fp.read()
splat = data.split("\n\n")
notes = []
for i, note in enumerate(splat, 0):
notes.append(note)
for i in range(0, len(notes)):
b = Button(text=notes[i], size_hint=(.25, .4))
self.add_widget(b)
class WindowManager(ScreenManager):
pass
class RelativeLayoutExample(RelativeLayout):
pass
class ScreeManager(ScreenManager):
pass
class LineMaker(Widget):
pass
class Noteit(App):
pass
Noteit().run()
Noteit.kv <---The kv file begins here
WindowManager:
FirstWindow:
SecondWindow:
ThirdWindow:
FourthWindow:
<FirstWindow>:
id: first_window
name: "Notes"
RelativeLayout:
Label:
text: "Notes"
size_hint: None,None
pos_hint: {'top':1,'center_x':.5}
font_size: 35
Button:
text: 'Account'
size_hint: None,None
pos_hint: {'top':1,'right':1}
on_release:
app.root.current = "Account"
root.manager.transition.direction= "left"
Button:
text: 'Preferences'
size_hint: None,None
pos_hint: {'top':1,'left':1}
on_release:
app.root.current = "Preferences"
root.manager.transition.direction= "right"
Label:
text: "Title:"
size_hint: None,None
pos_hint: {'top':.85,'center_x':.5}
font_size: 20
TextInput:
id: note_title
size_hint: .45,.08
pos_hint: {'top':.75,'center_x':.5}
multiline: False
Label:
text: "Details:"
size_hint: None,None
pos_hint: {'top':.65,'center_x':.5}
font_size: 20
TextInput:
id: note_details
size_hint: .45,.3
pos_hint: {'top':.55,'center_x':.5}
multiline: True
Button:
text: "Submit"
size_hint: .25,.1
length: "100dp"
pos_hint: {'top':.25,'center_x':.5}
on_release:
root.set_text()
LineMaker:
canvas:
Color:
rgba: 1, 1, 1, 1
Line:
width: 2.
points: (self.width,self.height*.83,0,self.height*.83)
Button:
text: 'View All Notes'
size_hint:.25,.15
pos_hint: {'bottom':1,'center_x':.5}
on_release:
app.root.current = "View"
root.manager.transition.direction= "up"
<SecondWindow>:
name: "Account"
RelativeLayout:
Label:
text: "Account"
size_hint: None,None
pos_hint: {'top':1,'center_x':.5}
font_size: 35
Button:
text: 'Notes'
size_hint: None,None
pos_hint: {'top':1,'left':1}
on_release:
app.root.current = "Notes"
root.manager.transition.direction= "right"
Button:
text: 'Preferences'
size_hint: None,None
pos_hint: {'top':1,'right':1}
on_release:
app.root.current = "Preferences"
root.manager.transition.direction= "left"
Label:
text: "Username:"
size_hint: None,None
pos_hint: {'top':.85}
font_size: 20
TextInput:
size_hint: .5,.1
pos_hint: {'top':.55,'left':.3}
multiline: False
Label:
text: "Password:"
size_hint: None,None
pos_hint: {'top':.65}
font_size: 20
TextInput:
size_hint: .5,.1
pos_hint: {'top':.75,'left':.3}
multiline: False
Button:
text: "Log In"
size_hint: .25,.1
length: "100dp"
pos_hint: {'top':.45}
Button:
text: "Sign Up"
size_hint: .25,.1
length: "100dp"
pos_hint: {'top':.45,'center_x':.375}
LineMaker:
canvas:
Color:
rgba: 1, 1, 1, 1
Line:
width: 2.
points: (self.width,self.height*.83,0,self.height*.83)
<ThirdWindow>:
name: "Preferences"
RelativeLayout:
Label:
text: "Preferences"
size_hint: None,None
pos_hint: {'top':1,'center_x':.5}
font_size: 35
Button:
text: 'Notes'
size_hint: None,None
pos_hint: {'top':1,'right':1}
on_release:
app.root.current = "Notes"
root.manager.transition.direction= "left"
Button:
text: 'Account'
size_hint: None,None
pos_hint: {'top':1,'left':1}
on_release:
app.root.current = "Account"
root.manager.transition.direction= "right"
ToggleButton:
text: 'Dark Mode'
size_hint: .25,.1
pos_hint: {'top':.75,'center_x':.25}
on_state: root.on_toggle_button_state()
Label:
id: dark_mode
text: ''
LineMaker:
canvas:
Color:
rgba: 1, 1, 1, 1
Line:
width: 2.
points: (self.width,self.height*.83,0,self.height*.83)
<FourthWindow>:
name: "View"
RelativeLayout:
Button:
text: 'Note Submission'
size_hint:.25,.15
pos_hint: {'top':1,'center_x':.5}
on_release:
app.root.current = "Notes"
root.manager.transition.direction= "down"
Label:
text: 'All Notes'
size_hint: None,None
pos_hint: {'top':1,'right':.9}
font_size: 35
How do I make the stacklayout code showup with the kv file code? Or at least make the stacklayout part of the screen.
Right now the only option I have is either take the screen as output or the stack layout as output.
Replacing the def build(self) inside the class FourthWindow(Screen) will show the whole application. The code inside the def build(self) also works but they do not work together.
I am new to kivy and object oriented programming so I do not know much about this topic so I could be missing something really basic.
So, I've been working on a very simple kivy app and am having trouble with the backend. How would you go about getting the values from text_input and changing label values? I've watched a few tutorials but their methods vary and I get error messages.
Here is my python code:
import kivy
from kivy.app import App
kivy.require('1.9.1')
class MyWindowApp(App):
pass
window = MyWindowApp()
window.run()
And here is the .kv file:
Screen:
side: side
FloatLayout:
Label:
text: "Side:"
pos_hint: {"x": 0.1, "y": 0.7}
text_size: self.size
Label:
text: "Volume:"
pos_hint: {"x": 0.1, "y": 0.65}
text_size: self.size
Label:
text: "Surface Area:"
pos_hint: {"x": 0.1, "y": 0.6}
text_size: self.size
TextInput:
size_hint: (.4, None)
height: 26
multiline: False
pos_hint: {"x": 0.24, "y": 0.7}
id: side
Label:
text: "0cm"
id: volume
pos_hint: {"x": 0.27, "y": 0.65}
text_size: self.size
Label:
text: "0cm"
id: surface_area
pos_hint: {"x": 0.355, "y": 0.6}
text_size: self.size
As mentioned in the other answer, place Builder.load_file or Builder.load_string in your build method.
To handle changes e.g. if the user hits enter in the side input use on_text_validate: app.on_side_change(self) or place a button to trigger the calculation and use on_press method.
The on_side_change method in your class MyApp will handle the change. It will be called once the user hits enter. See the example code below, for a basic calculation.
For getting/setting values from the labels/inputs you can use ObjectProperty or StringProperty from kivy.properties. In the below code I'm using StringProperty.
Important you need the StringProperty in your app class and use it in the kv file.
For the mode of your calculation I've added a property, so you can use it in your calculation method.
The mode is also used in the kv file so it's displaying the current mode.
The switcher dictionary is the Python way to do a switch/case statement.
Example code
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
kv = """
Screen:
side: side
GridLayout:
rows: 1
cols:2
spacing:0
GridLayout:
rows: 5
cols:1
Button:
text: "Cube"
# on_press: app.mode = self.text
on_press: app.setMode(self)
Button:
text: "Cuboid"
on_press: app.setMode(self)
Button:
text: "Cylinder"
on_press: app.setMode(self)
Button:
text: "Cone"
on_press: app.setMode(self)
Button:
text: "Sphere"
on_press: app.setMode(self)
FloatLayout:
Label:
text: "The Volume and surface area of a {}:".format(app.mode)
pos_hint: {"x":0.1, "y":0.8}
text_size: self.size
Label:
text:"Side:"
pos_hint: {"x":0.1, "y":0.7}
text_size: self.size
Label:
text:"Volume:"
pos_hint: {"x":0.1, "y":0.65}
text_size: self.size
Label:
text:"Surface Area:"
pos_hint: {"x":0.1, "y":0.6}
text_size: self.size
TextInput:
size_hint: (.4, None)
height: 26
multiline: False
pos_hint: {"x":0.24, "y":0.7}
id: side
text: app.sideText
on_text_validate: app.on_side_change(self)
Label:
text: app.volume
pos_hint: {"x":0.27, "y":0.65}
text_size: self.size
Label:
text: app.area
pos_hint: {"x":0.355, "y":0.6}
text_size: self.size
"""
class MyApp(App):
sideText = StringProperty("")
area = StringProperty("0 cm²")
volume = StringProperty("0 cm³")
mode = StringProperty("Cube")
def build(self):
return Builder.load_string(kv)
def setMode(self, btn):
self.mode = btn.text
def on_side_change(self, instance):
print(instance.text)
result = 0
try:
value = float(instance.text)
except:
# failed to convert
return
def cubeCalc(val):
return {
"volume": val * val * val,
"area": val * val
}
switcher = {
"Cube": cubeCalc
}
method = switcher.get(self.mode, "Unknown mode")
if method is not "Unknown mode":
result = method(value) # result is a dictionary with volume & area
#print(result)
print(self.volume)
self.volume = "{:.2f} cm³".format(result["volume"])
self.area = "{:.2f} cm²".format(result["area"])
if __name__ == "__main__":
MyApp().run()
why you don't have a build method in your App class?
try this:
import kivy
from kivy.app import App
from kivy.lang.builder import Builder
kivy.require('1.9.1')
class MyWindowApp(App):
def build(self):
self.root = Builder.load_file("YOUR_KV_FILE.kv")
return self.root
window = MyWindowApp()
window.run()
Don't forget to change YOUR_KV_FILE.kv to your own kv filename
since you are not using Builder.load_file method to load your kv file you your kv file name must be something like mywindow.kv but im recommended to use Builder.load_file because you can save the whole window in a variable and access it later easy and it also make you free to change your kv design filename
I have used KivyMD to develop a Screen which displays the Parameter values (In a DialogBox) of specific Item (which I listed them as OnelinelistItem). I also want to make provision for the user to change the parameter values from the DialogBox. But apparently I cant update the parameter settings from the DialogBox. The DialogBox contains the Textfield. Can anyone help me to figure out the issue, by going through the code, and let me know, where I am doing it wrong?
TIA! :)
testingsetpointpage.py
'''
from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'systemanddock')
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivymd.uix.dialog import MDDialog
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivymd.uix.button import MDFlatButton
from kivy.properties import StringProperty
Window.size = (1024, 600)
The Builder is shown here:
KV = """
#:kivy 1.11.0
#:import MDDropdownMenu kivymd.uix.menu.MDDropdownMenu
#:import MDRaisedButton kivymd.uix.button.MDRaisedButton
ScreenManager:
MainScreen:
<MainScreen>:
name: 'mainscreen'
NavigationLayout:
ScreenManager:
Screen:
name: 'homemain'
BoxLayout:
orientation:"vertical"
halign:"center"
#DOWN TAB
MDBottomNavigation:
MDBottomNavigationItem:
name: 'setpoint'
text: 'Setpoints'
icon: 'network'
BoxLayout:
orientation: "vertical"
MDToolbar:
title: 'Setpoints'
pos_hint: {'center_x':0.5,'center_y':0.95}
right_action_items: [["wifi", lambda x: app.navigation_draw()]]
left_action_items: [["menu", lambda x: nav_drawer.toggle_nav_drawer()]]
elevation: 10
BoxLayout:
orientation:"vertical"
padding: 5
spacing: 5
MDLabel:
text: " Functions: "
halign:"left"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
canvas.before:
Color:
rgba: (211/255.0,211/255.0,211/255.0,1)
Rectangle:
size: self.size
pos: self.pos
SetpointContent:
#MAKE THE PARAMETER LIST
<SetpointContent>:
BoxLayout:
orientation: "vertical"
padding: 5
spacing: 5
ScrollView:
MDList:
OneLineListItem:
text: "51P: Phase Time Overcurrent"
on_press: root.show_51Pdata()
<Content51P>
alarmpick51p: alp51p
alarmdelay51p: ald51p
trippick51p: trp51p
invcurve51p: inp51p
orientation: "vertical"
size_hint_y: None
height: "200dp"
GridLayout:
rows: 4
cols: 2
spacing: 10
MDLabel:
text: "Alarm Pickup: "
halign: "center"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
size_hint_x: 0.4
width:100
MDTextFieldRect:
id: alp51p
text: root.text1
multiline: False
MDLabel:
text: "Alarm Delay: "
halign: "center"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
size_hint_x: 0.4
width:100
MDTextFieldRect:
id: ald51p
text: str(app.Aldelay51P)
multiline: False
MDLabel:
text: "Trip Pickup: "
halign: "center"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
size_hint_x: 0.4
width:100
MDTextFieldRect:
id: trp51p
text: str(app.Trpick51P)
multiline: False
MDLabel:
text: "Inverse Curve: "
halign: "center"
theme_text_color: "Custom"
text_color: 0, 0, 1, 1
size_hint_y: 0.15
size_hint_x: 0.4
width:100
MDTextFieldRect:
id: inp51p
text: str(app.InverseCurve)
multiline: False
#####
"""
and the Classes are defined here:
class Content51P(BoxLayout):
app=MDApp.get_running_app()
text1 = StringProperty("1")
alarmpick51p = ObjectProperty()
alarmdelay51p = ObjectProperty()
trippick51p = ObjectProperty()
invcurve51p = ObjectProperty()
class MainScreen(Screen):
class SetpointContent(Screen):
def show_51Pdata(self):
self.dialog = MDDialog(title="51P Parameters:",
type="custom",
content_cls=Content51P(),
buttons=[MDFlatButton(text='Close', on_release=self.close_dialog),
MDFlatButton(text='Update', on_release=self.update51P)]
)
self.dialog.auto_dismiss = False
self.dialog.open()
def update51P(self, obj):
duc = Content51P()
app = MDApp.get_running_app()
duc.text1 = duc.ids.alp51p.text
print(duc.text1)
app.Alpick51P = float(duc.text1)
print(app.Alpick51P)
def close_dialog(self, obj):
self.dialog.auto_dismiss = True
self.dialog.dismiss()
class MainApp(MDApp):
Alpick51P = ObjectProperty("5")
Aldelay51P = ObjectProperty("5")
Trpick51P = ObjectProperty("5")
InverseCurve = ObjectProperty("Very Inverse")
def build(self):
self.theme_cls.primary_palette = "Blue"
screen = Builder.load_string(KV)
return screen
def navigation_draw(self):
print("Navigation")
def on_start(self):
pass
if __name__ == '__main__':
MainApp().run()
'''
The outlook looks something like this. I want to update the four parameters as the user clicks on the Update button and be able to view the value, the next time I open the DialogBx.
In the kv rule, under <Content51P> for MDTextFieldRect textinputs add:
on_text: app.Alpick51P = self.text
on_text: app.Aldelay51P = self.text
on_text: app.Trpick51P = self.text
on_text: app.InverseCurve = self.text
The on_text method should go to respective MDTextFieldRect.
Update the update51P() function should be like below now:
def update51P(self, obj):
app = MDApp.get_running_app()
print(app.Alpick51P)
print(app.Aldelay51P)
print(app.Trpick51P)
print(app.InverseCurve)
This would now print the updated inputs from textinput fields.
I've some problems with multiple MDLabels in BoxLayout (that is contains by AnchorLayout), so all the MDLabel objects are stacked in one place on the screen!
I dont know how to make them centered and grouped like a list (with spacing and e.g.)
Please, help me with solving that problem!
Thanks a lot and sorry for bad english.
There is my main.py
from kivy.app import App
from kivymd.theming import ThemeManager
from kivymd.label import MDLabel
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.metrics import dp, sp, pt
def toast(text):
from kivymd.toast.kivytoast import toast
toast(text)
class MyScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.menu_items = [
{
"viewclass": "MDMenuItem",
"text": "text%d" % i,
"callback": self.callback,
}
for i in range(1, 3)
]
self.menu_button = None
def change_variable(self, value):
print("\nvalue=", value)
self.VARIABLE = value
print("\tself.VARIABLE=", self.VARIABLE)
def callback(self, *args):
toast(args[0])
class MainApp(App):
title = "KivyMD MDDropdownMenu Demo"
theme_cls = ThemeManager()
def build(self):
return MyScreen()
if __name__ == "__main__":
MainApp().run()
And there is my main.kv file contains:
#:import MDDropdownMenu kivymd.menus.MDDropdownMenu
#:import MDRaisedButton kivymd.button.MDRaisedButton
#:import MDLabel kivymd.label.MDLabel
<MDMenuItem>:
on_release:
app.root.change_variable(self.text)
app.root.menu_button.text = self.text
<MyScreen>:
name: 'myscrn'
AnchorLayout:
anchor_y: 'center'
BoxLayout:
orientation: 'vertical'
size_hint: 0.1, 0.5
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
spacing: dp(10)
MDRaisedButton:
id: mainbutton
size_hint: None, None
size: 3 * dp(48), dp(48)
text: 'MDButton1'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
opposite_colors: True
on_release:
root.menu_button = mainbutton
MDDropdownMenu(items=root.menu_items, width_mult=4).open(self)
MDRaisedButton:
id: secondbutton
size_hint: None, None
size: 3 * dp(48), dp(48)
text: 'MDButton2'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
opposite_colors: True
on_release:
root.menu_button = secondbutton
MDDropdownMenu(items=root.menu_items, width_mult=4).open(self)
AnchorLayout:
anchor_y: 'top'
BoxLayout:
orientation: 'vertical'
size_hint: 0.95, 0.5
padding: [0, 0, 0, 0]
spacing: dp(5)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDLabel:
font_size: dp(12)
text: '123'
MDLabel:
font_size: dp(22)
text: '456'
Woops, looks like a simple mistake. Your indentation on KV Lang is incorrect. You didn't nest your labels into BoxLayout correctly.
AnchorLayout:
anchor_y: 'top'
BoxLayout:
orientation: 'vertical'
size_hint: 0.95, 0.5
padding: [0, 0, 0, 0]
spacing: dp(5)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDLabel:
font_size: dp(12)
text: '123'
MDLabel:
font_size: dp(22)
text: '456'"""