I wrote this to display a button with given image
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class DatesPage(FloatLayout):
def __init__(self, **kwargs):
super(DatesPage, self).__init__(**kwargs)
self.add_butt = Button(background_normal='plus_butt.png', size_hint=(0.2, 0.1))
self.add_widget(self.add_butt)
class DiaryApp(App):
def build(self):
return DatesPage()
if __name__=='__main__':
DiaryApp().run()
How can i make sure that the image is displayed on the button
You can't get a window with this code. Because you are not calling DiaryApp class correctly.Add brackets after class name.
if __name__=='__main__':
DiaryApp().run()
Related
I add some TextInputs in a Python3/kivy app, using a for cycle. I'd need to get all updated values after clicking a Button:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class app(App):
def build(self):
self.box=BoxLayout(orientation='vertical')
for n in range(5):
self.box.add_widget(TextInput())
def doet(instance):
print('values: ')
#print all TextInputs values
self.box.add_widget(Button(text='DOET',on_press=doet))
return self.box
app().run()
def doet(instance):
print('values: ')
# for loop:
for t in instance.parent.children:
if isinstance(t, TextInput):
print(t.text)
# using list comprehension
print('\n'.join([t.text for t in instance.parent.children if isinstance(t, TextInput)]))
In the kivy.FocusBehavior documentation (https://kivy.org/doc/stable/api-kivy.uix.behaviors.focus.html), an example with a FocusButton(FocusBehavior, Button) is given. But using the tab key on Windows 10 to cycle between the buttons added to the GridLayout does not work. What is wrong in the code below ?
from kivy.app import App
from kivy.uix.behaviors.focus import FocusBehavior
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class FocusButton(FocusBehavior, Button):
def _on_focus(self, instance, value, *largs):
print(self.text)
class FocusBehaviorGUI(GridLayout):
def __init__(self, **kwargs):
super().__init__( **kwargs)
self.cols = 4
self.rows = 2
for i in range(8):
self.add_widget(FocusButton(text=str(i)))
# clicking on a widget will activate focus, and tab can now be used
# to cycle through
class FocusBehaviorApp(App):
def build(self):
return FocusBehaviorGUI()
if __name__ == '__main__':
FocusBehaviorApp().run()
It is working. try changing:
def _on_focus(self, instance, value, *largs):
to:
def on_focus(self, instance, value, *largs):
Why am I getting black screen for the below code in kivy?
MyGrid class is not instantiating.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class MyGrid(GridLayout):
def __int__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text="First name="))
self.firstName = TextInput(multiline=False)
self.add_widget(self.firstName)
class Myapp(App):
def build(self):
return MyGrid()
if __name__ == '__main__':
Myapp().run()
You've written __int__ instead of __init__
I have written a python code in python file(main.py) to create a kivy app that contains dynamically created labels which works fine.
Here is main.py file
main.py
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
class createLabels(GridLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.cols=1
labels=[Label(text='Label '+str(i)) for i in range(5)]
[self.add_widget(label) for label in labels]
class DocApp(App):
def build(self):
return createLabels()
if __name__=="__main__":
DocApp().run()
However i would like to dynamically create labels similar to the one above using a kivy language file (.kv). I am not sure whether we can use lists and for statements in .kv file. I tried the solution mentioned in one of the similar type question but it didn't work.
You could use on_kv_post which runs when the kv class which you use on_kv_post is ready. And dynamically, like in push of buttons, is basically the same as in the example below.
from kivy.app import App
from kivy.lang import Builder
KV = '''
#: import Label kivy.uix.label.Label
GridLayout:
cols: 1
on_kv_post:
[self.add_widget(Label(text="Label " + str(i))) for i in range(9)]
'''
class TestApp(App):
def build(self):
return Builder.load_string(KV)
if __name__ == '__main__':
TestApp().run()
I am trying to create a simple TabbedPanel with 4 tabs. I want each tab to render the widgets from 4 classes. Kivy seems to not be rendering this simple Label. I want to do this with the libraries and not by creating a .kv file and importing it with Builder.load_file(file). In the code I provided, I only show the default_tab. Thanks in advance.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader
kivy.require('1.10.1')
class DeployScreen(GridLayout):
def __init__(self, **kwargs):
super(DeployScreen, self).__init__(**kwargs)
self.cols = 1
self.add_widget(Label(text='Deploy'))
return(None)
class NucleusPanel(TabbedPanel):
tab_pos = "top_left"
nuc_panel = TabbedPanel()
nuc_panel.default_tab_text = "Deploy"
nuc_panel.default_tab_content = DeployScreen()
class NucleusApp(App):
def build(self):
return(NucleusPanel())
if __name__ == "__main__":
NucleusApp().run()
Add a constructor for class NucleusPanel()
main.py
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.tabbedpanel import TabbedPanel
class DeployScreen(GridLayout):
def __init__(self, **kwargs):
super(DeployScreen, self).__init__(**kwargs)
self.cols = 1
self.add_widget(Label(text='Deploy'))
class NucleusPanel(TabbedPanel):
def __init__(self, **kwargs):
super(NucleusPanel, self).__init__(**kwargs)
self.tab_pos = "top_left"
self.default_tab_text = "Deploy"
self.default_tab_content = DeployScreen()
class NucleusApp(App):
def build(self):
return NucleusPanel()
if __name__ == "__main__":
NucleusApp().run()
Output