Skip to content Skip to sidebar Skip to footer

How To Change A Space When A Button Is Pressed With Kivy?

I am trying create a GUI by implementing the template of the ComicCreator GUI sample as a template for my own project. The code is easy to follow, but I would like to be able to r

Solution 1:

A neat way to do this is to use screen.

Since I allready have an example of this app from you earlier question, it was easy to implement the screens, and rewrite the classes a bit.

When a button is pressed, you set the screenmanager's current to whatever the name you named the screen you want.

Then you just edit the layouts as you want inside of each screen, in the kv file, or python file.

I choose to make most of the layout stuff in kv language here. Because I find it easier to develop a layout the way I want it this way. Later I could rewrite it to python if I want that.

So my python file looks like this now:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.clock import Clock
from kivy.uix.screenmanager import Screen,ScreenManager,NoTransition
from kivy.lang import Builder
import time


Builder.load_file("kv.kv")


classMyLayout(BoxLayout):

    def__init__(self,**kwargs):
        super(MyLayout,self).__init__(**kwargs)
        self.orientation = "vertical"
        self.padding = 10classMainScreen(Screen):
    passclassRemoveScreen(Screen):
    passclassGroupScreen(Screen):
    passclassMyLogo(BoxLayout):

    your_time = StringProperty()
    def__init__(self,**kwargs):
        super(MyLogo,self).__init__(**kwargs)
        Clock.schedule_interval(self.set_time, 0.1)

    defset_time(self,dt):
        self.your_time = time.strftime("%m/%d/%Y %H:%M")




classMyApp(App):
    def__init__(self,**kwargs):
        super(MyApp,self).__init__(**kwargs)
        self.sm = ScreenManager(transition=NoTransition())

        self.sm.add_widget(MainScreen(name = "main"))
        self.sm.add_widget(RemoveScreen(name = "remove"))
        self.sm.add_widget(GroupScreen(name = "group"))

        self.sm.current = "main"defbuild(self):
        return self.sm


if __name__ == "__main__":
    MyApp().run()

And kv.kv file looks like this:

#:kivy 1.9.1<MyButtons@BoxLayout>:padding:10,10,10,0spacing:10size_hint:1,0.3orientation:"horizontal"Button:text:"Clear"on_press:app.sm.current="main"Button:text:"Remove"on_press:app.sm.current="remove"Button:text:"Group"on_press:app.sm.current="group"Button:text:"Color"Button:text:"Gestures"<MyLogo>:spacing:10padding:10,10,10,0orientation:"horizontal"BoxLayout:orientation:"vertical"size_hint:0.3,1canvas:Rectangle:pos:self.possize:self.sizeAsyncImagesource:'http://lmsotfy.com/so.png'Label:size_hint:1,0.3text:root.your_timecolor: [0,0,0,1]
        Label:size_hint:1,0.3text:"NYC, New York, USA"color: [0,0,0,1]


<MainScreen>:MyLayout:MyLogo:#Button:#    text: "main"MyButtons:#buttonsBoxLayout:padding:10,10,10,10size_hint:1,0.3Button:text:"Total figures: 1          Kivy Started"<RemoveScreen>:MyLayout:MyLogo:BoxLayout:orientation:"horizontal"Label:font_size:"40sp"text:"Remove"Button:font_size:"20sp"text:"Remove this or something"MyButtons:#buttonsBoxLayout:padding:10,10,10,10size_hint:1,0.3Button:text:"Total figures: 1          Kivy Started"<GroupScreen>:MyLayout:MyLogo:BoxLayout:orientation:"vertical"Label:font_size:"40sp"text:"Group"Button:font_size:"20sp"text:"Something groups stuff"MyButtons:#buttonsBoxLayout:padding:10,10,10,10size_hint:1,0.3Button:text:"Total figures: 1          Kivy Started"

Solution 2:

The layout frame should be a screen manager, and each layout a screen. Screen transitions would be then triggered by pressing the buttons. You can also watch a tutorial here if you don't know how to do this, but the documentation should be enough.

Post a Comment for "How To Change A Space When A Button Is Pressed With Kivy?"