I have problems with giving the text of my button when clicking on the label in list from MDDropdownMenu. In my testing main.py and main.kv it has been working, but when it was implemented in main code - it has an error AttributeError: 'super' object has no attribute '__getattr__'
There is my main.py:
# encoding=utf8
import sys
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.popup import Popup
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.core.text import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.screenmanager import SlideTransition
from kivy.properties import ObjectProperty, NumericProperty, StringProperty, BooleanProperty, ListProperty
from kivy.utils import get_hex_from_color
from kivy.metrics import dp, sp, pt
from kivy.clock import Clock
from kivy.uix.textinput import TextInput
from kivymd.theming import ThemeManager
from kivymd.dialog import MDDialog
from kivymd.label import MDLabel
from kivymd.button import MDRoundFlatButton
from kivymd.button import MDRaisedButton
from kivymd.list import ILeftBodyTouch
from kivymd.popupscreen import MDPopupScreen
from newpickers import MDDatePicker
from kivymd.menus import MDDropdownMenu
year1 = 0
monthlist = ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь',]
monthnum = 0
selected_date = ''
class Manager(ScreenManager):
def __init__(self, **kwargs):
super(Manager, self).__init__(**kwargs)
class MainMenu(Screen):
pass
class InfoMenu(Screen):
pass
class ListButton(MDRaisedButton):
pass
class DateSetupMenu(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.menu_items = [
{
"viewclass": "MDMenuItem",
"text": "%d" % i,
"callback": self.callbackforlist,
}
for i in range(1900, 2100)
]
def callbackforlist(self, *args):
pass
class Year_layout_popup1(GridLayout):
pass
class Year_layout_scrollview1(ScrollView):
pass
class YearButton1(MDRaisedButton):
pass
class YearSelectionButton(MDRoundFlatButton):
def on_release(self):
global year1
year1 = int(self.text)
class DatePickPopup(Popup):
pass
class Month_layout_popup(GridLayout):
pass
class Month_layout_scrollview(ScrollView):
pass
class MonthButton(MDRaisedButton):
pass
class DatePickerWidget(MDDatePicker):
pass
class MonthSelectionButton(MDRoundFlatButton):
def on_release(self):
global monthlist
global monthnum
monthnum = int(monthlist.index(str(self.text)))
monthnum += 1
class MonthPickPopup(Popup):
pass
class yearselectbtn(Button):
pass
class monthselectbtn(Button):
pass
class rt_android(App):
theme_cls = ThemeManager()
theme_cls.device_orientation == 'portrait'
title = 'Rectif Tattva Android Edition'
yearselectlabeltext = StringProperty('Выберите год рождения')
monthselectlabeltext = StringProperty('Выберите месяц рождения')
dateselectlabeltext = StringProperty('Выберите день рождения')
mlist = ['Месяц','Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь']
VARIABLE = ""
def build(self):
self.theme_cls.theme_style = 'Light'
Window.size = (480, 854)
return Manager()
def CloseExitPopup(self, *args):
from kivymd.toast.kivytoast import toast
if args[0] == 'Да':
App.get_running_app().stop()
else:
pass
def ExitDialog(self):
self.dialog = MDDialog(
title='Выход из приложения', size_hint=(.8, .25), text_button_ok='Нет',
text="Вы точно хотите выйти?",
text_button_cancel='Да',
events_callback=self.CloseExitPopup)
self.dialog.open()
def OpenDatePicker(self, *args):
DatePickerWidget(self.set_date, year1, monthnum, 1).open()
def set_date(self, date_obj):
global selected_date
global year1
global monthnum
global daynum
self.setupdate = date_obj
selected_date = str(self.setupdate)
year1 = int(selected_date[0:4])
monthnum = int(selected_date[5:7])
daynum = int(selected_date[8:10])
month_id = monthnum-1
def year_select_clicked1(self):
self.ylp = Year_layout_popup1()
self.ylp.bind(minimum_height=self.ylp.setter('height'))
# for i in range(1950, 2019):
# self.ysb = YearSelectionButton()
# self.ysb.text = str(i)
# self.ylp.add_widget(self.ysb)
for i in range(1950, 2019):
self.ysb = yearselectbtn()
self.ysb.text = str(i)
self.ylp.add_widget(self.ysb)
root = Year_layout_scrollview1()
root.add_widget(self.ylp)
self.popup = DatePickPopup()
self.popup.content = root
self.popup.open()
def month_select_clicked(self):
global monthlist
self.mlp = Month_layout_popup()
self.mlp.bind(minimum_height=self.mlp.setter('height'))
# for i in range(0, 12):
# self.msb = MonthSelectionButton()
# self.msb.text = str(monthlist[i])
# self.msb.id = str(i)
# self.mlp.add_widget(self.msb)
for i in range(0, 12):
self.msb = monthselectbtn()
self.msb.text = str(monthlist[i])
self.msb.id = str(i)
self.mlp.add_widget(self.msb)
root = Month_layout_scrollview()
root.add_widget(self.mlp)
self.popup_m = DatePickPopup()
self.popup_m.content = root
self.popup_m.open()
def closeitpls(self):
self.popup.dismiss()
def closeitpls_m(self):
self.popup_m.dismiss()
def change_variable(self, value):
print("\nvalue=", value)
self.VARIABLE = value
print("\tself.VARIABLE=", self.VARIABLE)
class CreditsImage(Image):
pass
with open("main_interface.kv", encoding='utf8') as f:
main_interface = Builder.load_string(f.read())
if __name__ == '__main__':
rt_android().run()
And there is my main_interface.kv:
# encoding=utf8
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
#:import Factory kivy.factory.Factory
#:import MDLabel kivymd.label.MDLabel
#:import MDRaisedButton kivymd.button.MDRaisedButton
#:import MDRectangleFlatButton kivymd.button.MDRectangleFlatButton
#:import MDToolbar kivymd.toolbar.MDToolbar
#:import MDRoundFlatButton kivymd.button.MDRoundFlatButton
#:import MDDropdownMenu kivymd.menus.MDDropdownMenu
#:import MDMenuItem kivymd.menus.MDMenuItem
#:import hex kivy.utils.get_color_from_hex
#:set white hex('#00a86b')
<MenuButton#MDRaisedButton>:
font_size: dp(8)
elevation_normal: 3
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint_x: 0.5
height: dp(50)
<InfoMenuButton#MDRaisedButton>:
font_size: dp(8)
elevation_normal: 3
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: None, None
height: dp(50)
<DateSetupMenuButton#MDRaisedButton>:
font_size: dp(8)
elevation_normal: 3
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: None, None
height: dp(50)
<Year_layout_popup1>:
cols: 1
spacing: 15
padding: [10,10,10,10]
size_hint_y: None
<Year_layout_scrollview1>:
size_hint: (1, None)
size: Window.width*0.8, Window.height*0.7
<YearButton1>:
font_size: dp(8)
elevation_normal: 3
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: None, None
height: dp(50)
<DatePickPopup>:
title: 'Выбор года рождения'
size_hint: 0.8, 0.8
auto_dismiss: False
separator_color: white
title_color: white
background: 'assets/whiteback.png'
<YearSelectionButton>:
font_size: dp(8)
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: 1, None
height: dp(50)
on_press:
app.yearselectlabeltext = self.text
on_release:
app.closeitpls()
<Month_layout_popup>:
cols: 1
spacing: 15
padding: [10,10,10,10]
size_hint_y: None
<Month_layout_scrollview>:
size_hint: (1, None)
size: Window.width*0.8, Window.height*0.7
<MonthButton>:
font_size: dp(8)
elevation_normal: 3
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: None, None
height: dp(50)
<MonthPickPopup>:
title: 'Выбор месяца рождения'
size_hint: 0.8, 0.8
auto_dismiss: False
separator_color: white
title_color: white
background: 'assets/whiteback.png'
<MonthSelectionButton>:
id: ''
font_size: dp(8)
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: 1, None
height: dp(50)
on_press:
app.monthselectlabeltext = self.text
on_release:
app.closeitpls_m()
<yearselectbtn>:
font_size: dp(16)
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: 1, None
height: dp(50)
background_color: (255, 255, 255, 1)
color: (0,0,0,1)
on_press:
app.yearselectlabeltext = self.text
on_release:
app.closeitpls()
<monthselectbtn>:
font_size: dp(16)
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: 1, None
height: dp(50)
background_color: (255, 255, 255, 1)
color: (0,0,0,1)
on_press:
app.monthselectlabeltext = self.text
on_release:
app.closeitpls_m()
<MDMenuItem>:
on_release:
app.root.ids.buttonoflist.text = self.text
<ListButton>:
id: buttonoflist
font_size: dp(8)
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint: None, None
height: dp(50)
<Manager>:
MainMenu:
name: 'main_menu'
InfoMenu:
name: 'info_menu'
DateSetupMenu:
name: 'date_setup_menu'
<MainMenu>:
AnchorLayout:
anchor_y: 'top'
MDToolbar:
title: app.title
elevation: 10
md_bg_color: white
AnchorLayout:
anchor_y: 'bottom'
BoxLayout:
orientation: 'vertical'
size_hint: 0.95, 0.25
spacing: dp(10)
padding: [0, 0, 0, dp(10)]
MenuButton:
text: 'Приступить'
on_press:
app.root.transition = SlideTransition(direction='left', duration = .17)
on_release:
root.manager.current = 'date_setup_menu'
MenuButton:
text: 'Инфо'
on_press:
app.root.transition = SlideTransition(direction='left', duration = .17)
on_release:
root.manager.current = 'info_menu'
MenuButton:
text: 'Выход'
on_release:
app.ExitDialog()
<InfoMenu>:
AnchorLayout:
anchor_y: 'top'
MDToolbar:
title: 'Инфо'
elevation: 10
md_bg_color: white
BoxLayout:
size_hint_y: 0.4
orientation: 'vertical'
spacing: 5
padding: [0, dp(90), 0, 0]
pos_hint: {'center_x': .5, 'center_y': 1}
CreditsImage:
source: 'assets/info_credits_table.png'
size_hint_y: 0.8
MDLabel:
text: 'версия программы: 1.0.0'
font_name: 'assets/Ponter.ttf'
color: (255,255,255,1)
size_hint_y: 0.2
font_size: dp(20)
halign: 'center'
valign: 'top'
text_size: self.size
AnchorLayout:
anchor_y: 'bottom'
BoxLayout:
orientation: 'vertical'
size_hint: 0.95, 0.25
padding: [0, 0, 0, dp(15)]
InfoMenuButton:
text: 'Назад'
on_press:
app.root.transition = SlideTransition(direction='right', duration = .17)
on_release:
root.manager.current = 'main_menu'
<DateSetupMenu>:
AnchorLayout:
anchor_y: 'top'
MDToolbar:
title: 'Выбор параметров'
elevation: 10
md_bg_color: white
AnchorLayout:
anchor_y: 'center'
BoxLayout:
orientation: 'vertical'
size_hint: 0.95, 0.25
padding: [0, dp(15), 0, 0]
spacing: dp(5)
ListButton:
id: buttonoflist
text: 'Выбор'
on_release:
MDDropdownMenu(items=root.menu_items, width_mult=4).open(self)
YearButton1:
text: app.yearselectlabeltext
on_release:
app.year_select_clicked1()
MonthButton:
text: app.monthselectlabeltext
on_release:
app.month_select_clicked()
DateSetupMenuButton:
text: app.dateselectlabeltext
on_release:
app.OpenDatePicker()
AnchorLayout:
anchor_y: 'bottom'
BoxLayout:
orientation: 'vertical'
size_hint: 0.95, 0.25
padding: [0, 0, 0, dp(15)]
DateSetupMenuButton:
text: 'Назад'
on_press:
app.root.transition = SlideTransition(direction='right', duration = .17)
on_release:
root.manager.current = 'main_menu'
I need to change the text of «ListButton» object after clicking MDMenuItem proreply. I dont know, why it is working in standalone code, but in my main code it has that horrible problem...
Errors - KeyError & AttributeError
Traceback (most recent call last):
File "kivy/properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
KeyError: 'buttonoflist'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
...
File ".../main.kv", line 132, in <module>
app.root.ids.buttonoflist.text = self.text
File "kivy/properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
Root Cause
The first error encountered was KeyError because the id, 'buttonoflist' does not exist in Kivy self.ids dictionary type property.
The second error encountered was AttributeError because the id, 'buttonoflist' does not exist in the root (which is a ScreenManager).
Solution
Since id: buttonoflist is declared in the Screen,
DateSetupMenu:, you have to add an id to DateSetupMenu object so
that you can access / reference it.
Replace app.root.ids.buttonoflist.text with app.root.ids.date_setup_menu.ids.buttonoflist.text
Snippets - kv file
<MDMenuItem>:
on_release:
app.root.ids.date_setup_menu.ids.buttonoflist.text = self.text
...
<Manager>:
...
DateSetupMenu:
id: date_setup_menu
name: 'date_setup_menu'
...
Output
Related
I want to be able to click on MDFlatButton that says Štart and i want it to let's say for an example to call get_user_input(self) which would print whatever is inside text field. And I have been struggling with this for 2 whole days and i have no idea what to do, I am just a beginner and I have no clue what am I doing so sorry if it's messy. Ty for help, those are my files:
main.py file:
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.menu import MDDropdownMenu
from kivymd.uix.textfield import MDTextField
from kivy.properties import StringProperty
class testy(Screen):
novy_test = ObjectProperty()
class Test(MDApp):
Window.size = (1170 / 3, 2532 / 3)
# input_number = ObjectProperty()
def build(self):
self.theme_cls.material_style = "M3"
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = 'Gray'
return testy()
def get_user_input(self):
print(self.root.ids.my_textfield_1.ids.user.text)
def callback(self, button):
pass
class CustomOverFlowMenu(MDDropdownMenu):
# In this class you can set custom properties for the overflow menu.
pass
Test().run()
test.kv file:
#:import Factory kivy.factory.Factory
#:import CustomOverFlowMenu __main__.CustomOverFlowMenu
<testy>:
name:'testy'
id: testy
MDBoxLayout:
orientation: "vertical"
MDTopAppBar:
title: "MDTopAppBar"
use_overflow: True
overflow_cls: CustomOverFlowMenu()
specific_text_color: .51,.51,.51,1
md_bg_color: 0, 0, 0, 1
left_action_items: [["car", lambda x: Factory.novy_test().open(), '',"Nový test"]]
MDBottomNavigation:
panel_color: "black"
selected_color_background: "white"
text_color_active: "lightgray"
selected_color_background: 1, 1, 1, .4
MDBottomNavigationItem:
name: 'screen 1'
text: 'Testy'
icon: 'road-variant'
MDLabel:
text: 'Test'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 2'
text: 'chyby'
icon: 'alert-circle'
MDLabel:
text: 'Chyby'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 3'
text: 'Settings'
icon: 'cog'
MDLabel:
text: 'LinkedIN'
halign: 'center'
<novy_test#Popup>:
id:my_textfield_1
size_hint: .8, .45
title: 'Nový test'
separator_color: 'black'
title_align: 'center'
BoxLayout:
id: layout
spacing: 10
orientation:'vertical'
MDTextField:
id: user
hint_text: "Číslo testu"
mode: "round"
pos_hint: {"top": 1}
MDFlatButton:
text: 'Štart'
pos_hint: {'center_x': .5}
on_press: app.get_user_input()
MDFlatButton:
pos_hint: {'center_x': .5}
text:'test z nesprávnych'
MDFlatButton:
text:'test z neurobených'
pos_hint: {'center_x': .5}
MDFlatButton:
text:'test z neurobených'
pos_hint: {'center_x': .5}
MDFlatButton:
text:'test z neurobených'
pos_hint: {'center_x': .5}
I didn't perfectly understand all that you wanted to do, but in this example, the text a person types into the box will print. I moved the .kv language into a string just for convenience of creating a one-file runnable project. I tested it.
The main point is this code, inside the .kv file/kivy language you can use the id property to get a reference to the objects, and then the .text property of that object is sent as an argument to the function.
on_press: app.get_user_input(user.text)
runnable:
from kivy.core.window import Window
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.menu import MDDropdownMenu
from kivy.uix.popup import Popup
from kivymd.uix.textfield import MDTextField
from kivy.properties import StringProperty
Builder.load_string('''
#:import Factory kivy.factory.Factory
<testy>:
name:'testy'
id: testy
MDBoxLayout:
orientation: "vertical"
MDTopAppBar:
title: "MDTopAppBar"
use_overflow: True
# overflow_cls: CustomOverFlowMenu()
specific_text_color: .51,.51,.51,1
md_bg_color: 0, 0, 0, 1
left_action_items: [["car", lambda x: Factory.NovyTest().open(), '',"Nový test"]]
MDBottomNavigation:
panel_color: "black"
selected_color_background: "white"
text_color_active: "lightgray"
selected_color_background: 1, 1, 1, .4
MDBottomNavigationItem:
name: 'screen 1'
text: 'Testy'
icon: 'road-variant'
MDLabel:
text: 'Test'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 2'
text: 'chyby'
icon: 'alert-circle'
MDLabel:
text: 'Chyby'
halign: 'center'
MDBottomNavigationItem:
name: 'screen 3'
text: 'Settings'
icon: 'cog'
MDLabel:
text: 'LinkedIN'
halign: 'center'
<NovyTest#Popup>:
id:my_textfield_1
size_hint: .8, .45
title: 'Nový test'
separator_color: 'black'
title_align: 'center'
BoxLayout:
id: layout
spacing: 10
orientation:'vertical'
MDTextField:
id: user
hint_text: "Číslo testu"
mode: "round"
pos_hint: {"top": 1}
MDFlatButton:
text: 'Štart'
pos_hint: {'center_x': .5}
# inside .kv/kivy language you can use the id property
on_press: app.get_user_input(user.text)
MDFlatButton:
pos_hint: {'center_x': .5}
text:'test z nesprávnych'
MDFlatButton:
text:'test z neurobených'
pos_hint: {'center_x': .5}
MDFlatButton:
text:'test z neurobených'
pos_hint: {'center_x': .5}
MDFlatButton:
text:'test z neurobených'
pos_hint: {'center_x': .5}
'''
)
class NovyTest(Popup):
# just an example, not used in this code
def __init__(self, **kw):
super().__init__(**kw)
class testy(Screen):
# can list objects defined in .kv file and give them a type hint corresponding to object type
novy_test: NovyTest
def __init__(self, **kw):
super().__init__(**kw)
print("creating screen testy")
# novy_test = ObjectProperty()
class Test(MDApp):
Window.size = (1170 / 3, 2532 / 3)
# input_number = ObjectProperty()
def build(self) -> testy:
self.theme_cls.material_style = "M3"
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = 'Gray'
return testy()
def get_user_input(self, input_text, *args):
print(f"{self} user input: {input_text}")
# print(self.root.ids.my_textfield_1.ids.user.text)
def callback(self, button):
pass
class CustomOverFlowMenu(MDDropdownMenu):
# In this class you can set custom properties for the overflow menu.
pass
Test().run()
end
Im trying to add a MDDropDownMenu in my databaseWindow, but each time I try to access the menu, the app crashes with an error of "'databaseWindow' object has no attribute 'menu'". Im planning to use the dropdown menu as a list of options for the user to choose from so they can run a query. Im new to KivyMD and any assistance would be appreciated.
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from kivymd.uix.datatables import MDDataTable
from kivy.metrics import dp
from kivy.uix.anchorlayout import AnchorLayout
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import ObjectProperty
import mysql.connector
class myLoginScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class databaseWindow(Screen):
def dbtable_menu(self):
layout = AnchorLayout()
menu_items = [
{
"text": f"Item {i}",
"right_text": f"R+{i}",
"right_icon": "apple-keyboard-command",
"left_icon": "git",
"viewclass": "Item",
"height": dp(54),
"on_release": lambda x=f"Item {i}": self.menu_callback(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
caller = self.manager.get_screen('database1').ids['button'],
items = menu_items,
width_mult=4,
)
self.add_widget(self.menu)
def menu_callback(self, text_item):
print(text_item)
self.manager.get_screen('database1').ids['button'].text = str(text_item)
class MainApp(MDApp):
def build(self):
self.screen = Builder.load_file('login3.kv')
sm = ScreenManager()
sm.add_widget(myLoginScreen(name='main'))
sm.add_widget(databaseWindow(name='database1'))
return sm
if __name__ == "__main__":
MainApp().run()
Here is the kivy file:
#:import Window kivy.core.window.Window
#:import utils kivy.utils
ScreenManager:
myLoginScreen:
databaseWindow:
<myLoginScreen>:
id:main
name:'main'
bg_color: (1,1,1,.3)
highlight_color1jbsidis: [0,0,0,0]
highlight_color2jbsidis: (0,0,0,.3)
canvas.before:
Color:
rgba: utils.get_color_from_hex('#0A609E')
Rectangle:
size: self.size
pos: self.pos
FloatLayout:
#Image:
# source: '/Users/logo.png'
# pos_hint: {'right':.99, 'top':.85}
# size_hint_y: None
# height: dp(40)
# size: self.size
# pos: self.pos
# allow_stretch: False
# keep_ratio: True
TextInput:
id: username
hint_text: "Username"
foreground_color: [1,1,1,1]
background_color: [0,0,0,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
font_size: 40
size_hint: .7 ,.1 #.06
pos_hint: {'center_x':.5, 'center_y':.65} #'center_y':.8
canvas.before:
Color:
rgba: [1,1,1,.5]
RoundedRectangle:
size: self.size
pos: self.pos
radius: 10,
FloatLayout:
MDIconButton:
text: "[u]Reset password[/u]"
icon: 'account'
markup: True
color: Window.clearcolor
opposite_colors: True
pos_hint: {'center_x':.8, 'center_y':.65}
TextInput:
id: password
hint_text: "Password"
foreground_color: [1,1,1,1]
background_color: [0,0,0,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
password: True
size_hint: .7 ,.1 #.06
font_size: 40
pos_hint: {'center_x':.5, 'center_y':.45}
canvas.before:
Color:
rgba: [1,1,1,.5]
RoundedRectangle:
size: self.size
pos: self.pos
radius: 10,
FloatLayout:
MDIconButton:
icon: 'lock-outline'
markup: True
color: Window.clearcolor
opposite_colors: True
pos_hint: {'center_x':.8, 'center_y':.45}
MDRaisedButton:
id: sign_button
text: "Sign In"
pos_hint: {"center_x": .5, "top": .3}
on_release:
app.root.current = "database1"
MDRaisedButton:
id: button
text: "Sign up"
size_hint: .2 ,.05
pos_hint: {"right": .85, "center_y": .1}
opacity: 1
disabled: False
bg_color: (0,0,0,1)
color: [0,0,0,1]
<databaseWindow>:
id: database1
name: 'database1'
canvas.before:
Color:
rgba: utils.get_color_from_hex('#0A609E')
Rectangle:
size: self.size
pos: self.pos
MDRaisedButton:
id: button
text: "PRESS ME"
pos_hint:{"center_x":.5, "center_y":0.5}
on_release: root.menu.open()
MDTopAppBar:
title: "Select ONLY 1 Row for Query"
pos_hint: {'top':1}
elevation: 5
MDBoxLayout:
orientation: 'vertical'
padding: 10
MDFloatingActionButton:
icon: 'logout-variant'
on_press:
app.root.current = 'main'
root.log_out()
# GUI for pop up window
<P>:
Label:
text : "Please enter valid information"
size_hint : 0.2, 0.1
pos_hint : {"x" : 0.3, "top" : 0.8}
I'm trying KivyMD to build an app and everything was working great until I ran into an issue which I can't seem to fix even after spending an entire day on it yesterday. I'm building an app in which I can store my workouts and ran into an issue when addeding two MDTextFields. First they wouldn't align properly and they moved all the way outside the boxlayout, then after some resizing (self.minimum_height in the KV file) they are now clickable (you can see the hint text changing), but they won't allow for any input. Can you anyone help me why the MDTextField aren't working? My guess it has something to do with the same issue which caused the fields to be moved outside the boxlayout, but I don't understand what is causing that issue.
Here is the modified KV:
NavigationLayout:
id:nav_layout
MainAppScreenManager:
id: screen_manager
AddWorkoutMenu:
<MainAppScreenManager>
<AddWorkoutMenu>
name: "addworkoutmenu"
id: "addworkoutmenu"
workoutname:workoutname
setlist:setlist
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Add Workout"
elevation: 10
MDTextField:
id: workoutname
hint_text: "Workout name"
ScrollView:
MDList:
id: setlist
Sets:
id: workoutboxlist
MDRoundFlatIconButton:
id:addset
icon: "plus"
text: "Add set"
on_release:
root.add_set()
<Sets>
id:workoutbox
workoutexerciselist:workoutexerciselist
addbutton:addbutton
reps:reps
rest:rest
orientation: "vertical"
md_bg_color: app.theme_cls.primary_color
radius: 15, 15, 15, 15
padding: "16dp"
spacing: "4dp"
size_hint_y: None
height: self.minimum_height
BoxLayout:
pos_hint: {"center_x": .5, "center_y": 0}
orientation: "horizontal"
MDLabel:
pos_hint: {"center_x": .5, "center_y": .2}
theme_text_color: "Custom"
text_color: [1,1,1,1]
text: root.id
MDIconButton:
pos_hint: {"center_x": .5, "center_y": 0}
icon: "trash-can"
theme_text_color: "Custom"
text_color: [1,1,1,1]
on_release:
self.parent.parent.remove_set()
MDBoxLayout:
size_hint_y: None
height: self.minimum_height / 4
pos_hint: {"center_x": .5, "y": 0}
orientation: "horizontal"
MDLabel:
size_hint: (0.15,None)
pos_hint: {"center_x": .5, "center_y": 0}
theme_text_color: "Custom"
text_color: [1,1,1,1]
text: "Reps:"
MDTextField:
id: reps
size_hint: (0.4,None)
pos_hint: {"center_x": .5, "center_y": 0.1}
spacing: "4dp"
current_hint_text_color: [1,1,1,1]
line_color_normal: [1,1,1,1]
hint_text: "# reps"
MDLabel:
size_hint: (0.1,None)
pos_hint: {"center_x": .5, "center_y": 0}
spacing: "4dp"
theme_text_color: "Custom"
text_color: [1,1,1,1]
text: "Rest:"
MDTextField:
id: rest
size_hint: (0.4,None)
pos_hint: {"center_x": .5, "center_y": 0.1}
spacing: "4dp"
current_hint_text_color: [1,1,1,1]
line_color_normal: [1,1,1,1]
hint_text: "in seconds"
MDList:
id:workoutexerciselist
MDRoundFlatIconButton:
id:addbutton
icon: "plus"
theme_text_color: "Custom"
text_color: [1,1,1,1]
text: "Add exercise"
on_release:
root.open_the_menu()
<WorkoutExerciseListItem>
id: listitem
theme_text_color: "Custom"
text_color: [1,1,1,1]
DeleteItem:
<DeleteItem>
name: "deleteicon"
icon: "close"
theme_text_color: "Custom"
text_color: [1,1,1,1]
on_release:
root.remove()
Here is the modified Python:
from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior
from kivymd.toast import toast
from kivymd.uix.list import OneLineIconListItem, MDList, IRightBodyTouch, OneLineRightIconListItem
from kivymd.uix.menu import MDDropdownMenu, RightContent
from kivymd.uix.button import MDIconButton
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.widget import Widget
from kivy.properties import StringProperty, ObjectProperty, NumericProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from database import DataBaseExercises, DataBaseWorkouts
class AddWorkoutMenu(Screen):
def __init__(self, **kwargs):
super(AddWorkoutMenu, self).__init__(**kwargs)
def add_set(self):
self.ids.setlist.add_widget(Sets(id = ""))
for i in self.ids.setlist.children:
if not hasattr(i, 'menu_1'):
i.create_self()
def save(self):
workout = self.workoutname.text
sets = self.ids.workoutboxlist._sets
if workout != "":
db_workouts.add(workout, sets)
self.parent.current = "workouts"
self.clear_all()
toast("Exercise saved")
else:
toast("Please fill in all information")
def clear_all(self):
self.workoutname.text = ""
for i in list(self.setlist.children):
self.setlist.remove_widget(i)
Sets._ids = 0
self.add_set()
def update_ids(self, removed, total):
if removed != total:
for i in self.ids.setlist.children:
current_number = int(i.id[-1])
if current_number > removed:
current_number -= 1
i.id = "Set " + str(current_number)
class Sets(MDBoxLayout):
_ids = 0
_sets = {}
def __init__(self, **kwargs):
super(Sets, self).__init__(**kwargs)
Sets._ids += 1
self.id = "Set " + str(Sets._ids)
self._sets[self.id] = {}
self._sets[self.id]["Exercise"] = []
self._sets[self.id]["Reps"] = []
self._sets[self.id]["Rest"] = []
def add_selection(self, name):
self.workoutexerciselist.add_widget(WorkoutExerciseListItem(text=name))
self._sets[self.id]["Exercise"].append(name)
print(self._sets[self.id]["Exercise"])
self.update_height()
def update_height(self):
if self.workoutexerciselist.height == 16.0:
self.workoutexerciselist.height = 0.0
self.height = self.minimum_height + self.workoutexerciselist.height
def create_menu(self, text, instance):
menu_items = [{"text": i} for i in text]
return MDDropdownMenu(position = "auto", caller=instance, items=menu_items, width_mult=5, callback=self.set_item)
def create_self(self):
exerciselist = db_exercise.get_all()
self.menu_1 = self.create_menu(exerciselist, self.addbutton)
def update_self(self, value):
new_list = []
new_list.append(value)
for i in self.menu_1.items:
for u in i.values():
new_list.append(u)
self.menu_1 = self.create_menu(new_list, self.addbutton)
def set_item(self, instance):
self.menu_1.on_dismiss()
self.add_selection(instance.text)
self.remove_item(instance.text)
def remove_item(self, item):
new_list = []
for i in self.menu_1.items:
for u in i.values():
if u != item:
new_list.append(u)
self.menu_1 = self.create_menu(new_list, self.addbutton)
def open_the_menu(self):
self.menu_1.open()
def remove_set(self):
number = int(self.id[-1])
self._sets.pop("Set "+str(number))
self.update_set(number)
self.id = "Set 0"
self.parent.parent.parent.parent.parent.update_ids(number, Sets._ids)
Sets._ids -= 1
self.parent.remove_widget(self)
def update_set(self, number):
for i in range(number, Sets._ids):
self._sets["Set " + str(i)] = self._sets["Set " + str(i+1)]
self._sets.pop("Set " + str(i + 1))
def save_reps_and_rest(self):
self._sets[self.id]["Reps"] = self.reps
self._sets[self.id]["Rest"] = self.rest
class WorkoutExerciseListItem(OneLineRightIconListItem):
pass
class DeleteItem(IRightBodyTouch, MDIconButton):
pass
class MainAppScreenManager(ScreenManager):
pass
class MainApp(MDApp):
pass
if __name__ == "__main__":
MainApp().run()
Not sure what is happening, but adding:
color_mode: 'custom'
line_color_focus: 1, 1, 1, 1
to each MDTextField seems to wok.
I got a popup filechooser from the below link:
Kivy popup Filechooser pass variable (selection)
I cannot get the file path string value to pass between my main Tab() class instance of the app and the class FileChoosePopup popup instance. I know this question has been asked before, but I cannot seem to figure it out. When I run the app and click on the file I want to pass to a TextInput on my main class I get the following error:
AttributeError: 'super' object has no attribute '__getattr__'
I have tried passing a reference to the popup class instance in the main class by using the super init method, but then the app does not even initialize.
Here is my popup in python:
class FileChoosePopup(Popup):
file_path = StringProperty("No file chosen")
def __init__(self, **kwargs):
super(FileChoosePopup, self).__init__(**kwargs)
self.Tab = Tab()
def load(self, selection):
self.file_path = str(selection[0])
path_file = self.file_path
the_file = self.ids.get_file #is the textinput id
if path_file != "No file chosen":
the_file.text = path_file
self.dismiss()
else:
self.dismiss()
class Tab(StackLayout):
def open_popup(self):
the_popup = FileChoosePopup()
the_popup.open()
Here the kivy code:
<FileChoosePopup>:
title: "Choose a .CSV File"
size_hint: .9, .9
auto_dismiss: False
BoxLayout:
orientation: "vertical"
FileChooser:
id: filechooser
FileChooserIconLayout
BoxLayout:
size_hint: (1, 0.1)
pos_hint: {'center_x': .5, 'center_y': .5}
spacing: 20
RoundedCancelButton:
text: "Cancel"
on_release: root.dismiss()
RoundedAcceptButton:
text: "Load"
on_release: root.load(filechooser.selection)
id: ldbtn
disabled: True if filechooser.selection==[] else False
<Tab>:
TabbedPanel:
do_defualt_tab: False
background_color: (.87, .87, .87, 1)
border: [0, 0, 0, 0]
background_image: 'path/to/background/image'
TabbedPanelItem:
text: 'Import'
background_color: (1, .5, 0, 1)
background_normal: ''
StackLayout:
orientation: 'lr-tb'
size_hint_y: None
height: 30
spacing: 5
Label:
text: ''
size_hint_x: 1
Label:
text: ''
size_hint_x: 0.2
RoundedButton:
text: 'Choose File'
size_hint_x: 0.2
on_press: root.open_popup()
TextInput:
id: get_file
readonly: True
size_hint_x: 0.4
Label:
text: ''
size_hint_x: 0.2
Can someone please give me some pointers on how to get the value to pass from the popup to the textinput?
Reference TextInput
Populate TextInput by using the following:
self.ids.get_file.text = self.file_path
Example
main.py
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.properties import ObjectProperty, StringProperty
from kivy.lang import Builder
class FileChoosePopup(Popup):
load = ObjectProperty()
class Tab(TabbedPanel):
file_path = StringProperty("No file chosen")
the_popup = ObjectProperty(None)
def open_popup(self):
self.the_popup = FileChoosePopup(load=self.load)
self.the_popup.open()
def load(self, selection):
self.file_path = str(selection[0])
self.the_popup.dismiss()
print(self.file_path)
# check for non-empty list i.e. file selected
if self.file_path:
self.ids.get_file.text = self.file_path
Builder.load_file('main.kv')
class TestApp(App):
def build(self):
return Tab()
if __name__ == "__main__":
TestApp().run()
main.kv
<FileChoosePopup>:
title: "Choose a .CSV File"
size_hint: .9, .9
auto_dismiss: False
BoxLayout:
orientation: "vertical"
FileChooser:
id: filechooser
FileChooserIconLayout
BoxLayout:
size_hint: (1, 0.1)
pos_hint: {'center_x': .5, 'center_y': .5}
spacing: 20
Button:
text: "Cancel"
on_release: root.dismiss()
Button:
text: "Load"
on_release: root.load(filechooser.selection)
id: ldbtn
disabled: True if filechooser.selection==[] else False
<Tab>:
do_default_tab: False
TabbedPanelItem:
text: 'Import'
background_color: (1, .5, 0, 1)
background_normal: ''
StackLayout:
orientation: 'lr-tb'
size_hint_y: None
height: 30
spacing: 5
Label:
text: ''
size_hint_x: 1
Label:
text: ''
size_hint_x: 0.2
Button:
text: 'Choose File'
size_hint_x: 0.2
on_press: root.open_popup()
TextInput:
id: get_file
readonly: True
size_hint_x: 0.4
Label:
text: ''
size_hint_x: 0.2
Output
Can anyone tell me?
I have two file test.py and test.kv.
I have a textBox which id : state . How to pass state.text value in root.get_value(state.text) and get value in function of def get_value
test.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
Window.size = (450, 525)
class display(Screen):
state = ObjectProperty(None)
def add_more(self):
self.ids.rows.add_row()
def get_value(self,arg1):
print(arg1)
class Row(BoxLayout):
button_text = StringProperty("")
state = ObjectProperty(None)
class Rows(BoxLayout):
orientation = "vertical"
row_count = 0
def __init__(self, **kwargs):
super(Rows, self).__init__(**kwargs)
self.add_row()
def add_row(self):
self.row_count += 1
self.add_widget(Row(button_text=str(self.row_count)))
class test(App):
def build(self):
self.root = Builder.load_file('test.kv')
return self.root
if __name__ == '__main__':
test().run()
test.kv
<Row>:
state : state
orientation: "horizontal"
spacing: 0, 5
Button:
text: root.button_text
size_hint_x: .2
TextInput:
text : "Test1"
size_hint_x: .8
id : state
display:
BoxLayout:
orientation: "vertical"
padding : 20, 20
BoxLayout:
orientation: "horizontal"
Button:
size_hint_x: .2
text: "+Add More"
valign: 'bottom'
on_press: root.add_more()
BoxLayout:
orientation: "horizontal"
Label:
size_hint_x: .2
text: "SN"
valign: 'bottom'
Label:
size_hint_x: .8
text: "Value"
valign: 'bottom'
Rows:
id: rows
BoxLayout:
orientation: "horizontal"
padding : 10, 0
spacing: 10, 10
size_hint: .5, .7
pos_hint: {'x': .25, 'y':.25}
Button:
text: 'Ok'
on_release:
root.get_value(state.text)
Button:
text: 'Cancel'
on_release: root.dismiss()