How to get data from wordpress and show in kivymd? - kivy

I want to fetch and show post data on my kivymd app from wordpress blog:
Here is blog : Binghow.com
When i try to run this code, it shows data in the console, what I want is to show my data in kivymd app.
Note: I am new in programming and trying KivyMD
from kivy.network.urlrequest import UrlRequest
from kivymd.app import MDApp
from kivymd.uix.label import Label
from kivymd.uix.screen import Screen
from kivy.core.window import Window
Window.size = (350, 600)
class MyApp(MDApp):
def build(self):
screen = Screen()
label = Label(text="Kisan Mitra", color="red", font_size="40px", pos_hint={
"center_x": 0.5, "center_y": 0.6})
screen.add_widget(label)
return screen
def got_json(req, result):
for key, value in req.resp_headers.items():
print('{}: {}'.format(key, value))
req = UrlRequest("https://binghow.com/wp-json/wp/v2/posts", got_json)
MyApp().run()

Related

I want write codes for counting numbers app. My app can count the numbers of pages. I want to add button for cleaning screen

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class TestScreen(Screen):
def __init__(self, **kwargs):
Screen.__init__(self, **kwargs)
layout = BoxLayout(orientation="vertical")
self.add_widget(layout)
layout.add_widget(Label(text=self.name, font_size="150sp"))
button = Button(text="Count",font_size='30sp')
layout.add_widget(button)
button.bind(on_press=self.add_screen)
def add_screen(self, *args):
n = len(self.manager.screen_names)
screen = TestScreen(name="{}".format(n))
self.manager.add_widget(screen)
self.manager.current = screen.name
# Create the screen manager
sm = ScreenManager()
sm.add_widget(TestScreen(name=''))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
I can't add button with a view to cleaning screen. I know this app not for counting. But i only need add button for cleaning numbers.

Design where image is centered in the left half of the screen with scale and move possibility

I have this design of the UI:
The hardest part is to center the image in the left half of the screen, with scale and move possibility. I'm trying to do it with FloatLayout and somehow combine the behavior of Scatter and Image.
I have this code sofar:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.config import Config
from kivy.uix.button import Button
from kivy.uix.scatter import Scatter
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
class Tedegraph(App):
def build(self):
mainbox = FloatLayout()
mainbox.add_widget(Button(text="Prev",
font_size="17dp",
size_hint=(.15, .15),
pos_hint={"left":1,
"center_y":0.5},
))
#sp = Scatter(scale=1, do_scale = True, do_rotation = False, pos_hint={"center_x":0.5, "center_y":0.5})
self.img = Image(source='img.png')
#sp.add_widget(self.img)
mainbox.add_widget(self.img) # images will change during execution
self.text_label = Label(text="HELLO", size_hint=(1, None), pos_hint={"center_x":0.5, "center_y":0.5}) # text will change during execution
self.text_label.bind(
width=lambda *x: self.text_label.setter('text_size')(self.text_label, (self.text_label.width, None))) # only wrapping functionality
mainbox.add_widget(self.text_label)
mainbox.add_widget(Button(text="Next",
font_size="17dp",
size_hint=(.15, .15),
pos_hint={"right":1,
"center_y":0.5},
))
return mainbox
if __name__ == "__main__":
Tedegraph().run()
I hope it is possible with keeping the ratio of the image. Thanks for suggestions
You can set the size and position of the Image widget when you create it:
self.img = Image(source='img.png', size_hint=(0.33,0.33), pos_hint={'center_x':0.33, 'center_y':0.5}, allow_stretch=True, keep_ratio=True)
And similarly, with the Label:
self.text_label = Label(text="HELLO\nThis is a Test", halign='center', size_hint=(0.33, None), pos_hint={"center_x":0.67, "center_y":0.5})

How to make a Kivy App that interprets python code?

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class MyApp(App):
def build(self):
layout = BoxLayout(padding=10, orientation='vertical')
btn1 = Button(text="Run")
btn1.bind(on_press=self.buttonClicked)
layout.add_widget(btn1)
self.lbl1 = Label(text="After Running.. ")
layout.add_widget(self.lbl1)
self.txt1 = TextInput(text='', multiline=True)
layout.add_widget(self.txt1)
return layout
# button click function
def buttonClicked(self, btn):
self.lbl1.text = self.txt1.text
# run app
if __name__ == "__main__":
MyApp().run()
I want to run python code in my Kivy app. That is, when I press "Run" it runs the program. Any ideas on how to do it?
codeInString = """
def main(x):
print(x)
main("Mo")
"""
codeObejct = compile(codeInString, 'function', 'exec')
exec(codeObejct)
I think this is one possible way to do it.

How can I implement a scrolling label in Kivy without using Builder (or a .kv file)?

I am trying to implement a scrolling label in a Kivy program, and found this example (slightly modified) that works:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.lang import Builder
long_text = "".join(["this is a long line "+str(n)+"\n" for n in range(1,101)])
Builder.load_string('''
<ScrollableLabel>:
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
text: root.text
''')
class ScrollableLabel(ScrollView):
text = StringProperty('')
class ScrollApp(App):
def build(self):
return ScrollableLabel(text=long_text)
if __name__ == "__main__":
ScrollApp().run()
Partly for my own education, I am trying to convert this sample to not use Builder (and not resort to a .kv file). I have modified the above example to:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
long_text = "".join(["this is a long line "+str(n)+"\n" for n in range(1,101)])
class ScrollableLabel(ScrollView):
text = StringProperty('')
def __init__(self, **kwargs):
super(ScrollableLabel, self).__init__(**kwargs)
self.label = Label(size_hint_y=None, text=self.text)
self.label.height = self.label.texture_size[1]
self.label.text_size = (self.label.width, None)
self.add_widget(self.label)
class ScrollApp(App):
def build(self):
return ScrollableLabel(text=long_text)
if __name__ == "__main__":
ScrollApp().run()
To my obviously untutored eye, these programs look like they should be equivalent. However, my (second) version doesn't work correctly (on several fronts).
So my question is two-fold: why doesn't the second version work the same as the first, and (if the answer isn't obvious from the first), how can I make it do so?
Thanks! -David
Try this:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.clock import Clock
long_text = "".join(["this is a long line "+str(n)+"\n" for n in range(1,101)])
class ScrollableLabel(ScrollView):
text = StringProperty('')
def __init__(self, **kwargs):
super(ScrollableLabel, self).__init__(**kwargs)
self.label = Label(size_hint_y=None, text=self.text)
self.add_widget(self.label)
Clock.schedule_once(self.update, 1)
def update(self, *args):
self.label.text_size = (self.label.width, None)
self.label.height = self.label.texture_size[1]
class ScrollApp(App):
def build(self):
return ScrollableLabel(text=long_text)
if __name__ == "__main__":
ScrollApp().run()
the output now is the same as your first

Kivy property observer objects left behind after ModalView is dismissed

I display in a popup (ModalView) a dynamically changing value. I use a method in my main widget class to open/dismiss the popup, and bind a Kivy StringProperty to a Label in the popup. There is a problem - each time the popup is dismissed, something is left behind. Listing all observers of the StringProperty shows how with each cycle of open/dismiss the number of objects accumulates. See the example code below. When I run this on Raspberry Pi 2 under Raspbian Jessie (Pixel) with 128M allocated for VRAM, within about a minute the progam stops functioning correctly - popup starts to show a black screen. Am I doing something silly in my code?
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.modalview import ModalView
from kivy.clock import Clock
from kivy.properties import StringProperty
from random import randint
Builder.load_string('''
#:kivy 1.9.2
<MainWidget>:
BoxLayout:
Button:
''')
class MainWidget(BoxLayout):
value_str = StringProperty()
def show_popup(self, even=True):
if even:
popup = ModalView(size_hint=(None, None), auto_dismiss=False, size=(700,480))
popup_label = Label(font_size = 200, text_size=self.size, halign='center', valign='center')
self.bind(value_str=popup_label.setter('text')) # value_str must be a Kivy StringProperty
popup.add_widget(popup_label)
self.value_str = str(randint(0,100))
popup.open()
else: # find all instances of ModalView and dismiss them
for widget in App.get_running_app().root_window.children:
if isinstance(widget, ModalView):
print "observers of value_str property:"
observers = self.get_property_observers('value_str')
for observer in observers:
print observer
widget.dismiss(force=True, animation=False)
Clock.schedule_once(lambda dt: self.show_popup(not even), 0.25)
class MyApp(App):
def build(self):
mw=MainWidget()
Clock.schedule_once(lambda dt: mw.show_popup(),0)
return mw
if __name__ == '__main__':
MyApp().run()
I found a workaround, inspired by this How to unbind a property automatically binded in Kivy language?
I now preserve the Label child by removing it from the ModalView and adding it to the MainWidget before ModalView is dismissed, then reversing this for the next popup. This way the property binding takes place only once so no new observers are created. The label can be made invisible by assigning an empty string to the bound property.
I think this may be a bug - ModalView dismiss() method should not leave behind observers, but cannot test with latest Kivy version (1.10.1.dev0).
Here's the code:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.modalview import ModalView
from kivy.clock import Clock
from kivy.properties import StringProperty
from random import randint
Builder.load_string('''
#:kivy 1.9.2
<MyLabel>:
font_size: 100
text_size: self.size
halign: 'center'
valign: 'center'
<MainWidget>:
Button:
background_color: 0.5, 0.5, 1, 1
''')
class MyLabel(Label):
pass
class MainWidget(FloatLayout):
value_str = StringProperty()
popup_label = MyLabel()
def __init__(self, **kwargs):
super(MainWidget, self).__init__(**kwargs)
self.bind(value_str=self.popup_label.setter('text')) # value_str must be a Kivy StringProperty
self.add_widget(self.popup_label)
def show_popup(self, even=True):
if even:
popup = ModalView(size_hint=(None, None), auto_dismiss=False, size=(500,380))
self.remove_widget(self.popup_label)
popup.add_widget(self.popup_label)
self.value_str = str(randint(0,100))
popup.open()
else: # find all instances of ModalView and dismiss them
for widget in App.get_running_app().root_window.children:
if isinstance(widget, ModalView):
print "observers of value_str property:"
observers = self.get_property_observers('value_str')
for observer in observers:
print observer
widget.remove_widget(self.popup_label)
self.add_widget(self.popup_label)
self.value_str =''
widget.dismiss(force=True, animation=False)
Clock.schedule_once(lambda dt: self.show_popup(not even), 0.25)
class MyApp(App):
def build(self):
mw=MainWidget()
Clock.schedule_once(lambda dt: mw.show_popup(),0)
return mw
if __name__ == '__main__':
MyApp().run()

Resources