3

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 reconfigure the drawingspace.kv, each time a button is pushed, say for example something like this:

enter image description here

Q: How could I configure the drawingspace.kv to have a different layout with different widgets for each button that is pressed?

3kstc
  • 1,871
  • 3
  • 29
  • 53

2 Answers2

4

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")


class MyLayout(BoxLayout):

    def __init__(self,**kwargs):
        super(MyLayout,self).__init__(**kwargs)
        self.orientation = "vertical"
        self.padding = 10


class MainScreen(Screen):
    pass


class RemoveScreen(Screen):
    pass


class GroupScreen(Screen):
    pass


class MyLogo(BoxLayout):

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

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




class MyApp(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"

    def build(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,0
    spacing: 10
    size_hint: 1,0.3
    orientation: "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: 10
    padding: 10,10,10,0
    orientation: "horizontal"
    BoxLayout:
        orientation: "vertical"
        size_hint: 0.3,1
        canvas:
            Rectangle:
                pos: self.pos
                size: self.size
        AsyncImage
            source: 'http://lmsotfy.com/so.png'
        Label:
            size_hint: 1,0.3
            text: root.your_time
            color: [0,0,0,1]
        Label:
            size_hint: 1,0.3
            text: "NYC, New York, USA"
            color: [0,0,0,1]


<MainScreen>:
    MyLayout:
        MyLogo:
            #Button:
            #    text: "main"

        MyButtons:
            #buttons

        BoxLayout:
            padding: 10,10,10,10
            size_hint: 1,0.3
            Button:
                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:
            #buttons

        BoxLayout:
            padding: 10,10,10,10
            size_hint: 1,0.3
            Button:
                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:
            #buttons

        BoxLayout:
            padding: 10,10,10,10
            size_hint: 1,0.3
            Button:
                text: "Total figures: 1          Kivy Started"
el3ien
  • 5,362
  • 1
  • 17
  • 33
  • Is `kv.kv` a new file in addition to files in the [example](https://www.packtpub.com/packtlib/book/Application-Development/9781785286926/1/ch01lvl1sec13/Our%20project%20%20Comic%20Creator)? – 3kstc Jul 21 '16 at 05:48
  • @3kstc yes and it gets loaded by python on line 10 in the .py file `Builder.load_file("kv.kv")` – el3ien Jul 21 '16 at 12:07
  • you are a godsend! – 3kstc Jul 22 '16 at 00:27
  • I am having difficulty implementing "tables", like the one depicted in the question - a 6 row by 2 col? - Any suggestions? Column 1 will have standard text, and column 2 the text will be retrieved from a MySQL table. – 3kstc Jul 28 '16 at 05:55
  • @3kstc you could place a gridlayout inside of there – el3ien Jul 28 '16 at 09:20
  • Last 2 questions; **1** Is there a way to merge two cells into one?, or have a 1x1 grid on top of 6x2 grid? **2** I can't seem to [`halign: 'right'`](https://kivy.org/docs/api-kivy.uix.label.html) How can I `right` align? – 3kstc Jul 29 '16 at 05:20
  • @3kstc you can merge cells, and you can nest as many layouts you want. grid inside a greid inside a grid. Inception – el3ien Jul 29 '16 at 11:54
  • I am so sorry to bother you, but I have tried for past week different thigns but wasn't successful, could you please help me with my [question](http://stackoverflow.com/questions/38779737/kivy-how-to-merge-cells-and-interact-with-mysql-database)? – 3kstc Aug 05 '16 at 01:25
  • @3kstc may I suggest that you place a new question, with a minimal example, and I will help you. – el3ien Aug 05 '16 at 01:41
  • Thanks! I have already placed a new question. It was linked to the word `question` above or [here it is](http://stackoverflow.com/questions/38779737/kivy-how-to-merge-cells-and-interact-with-mysql-database) – 3kstc Aug 05 '16 at 01:43
0

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.

jligeza
  • 4,544
  • 6
  • 23
  • 31
  • Thanks, I actually had a read through it before posting.... but I didn't understand the *how to* implement it in the [code](https://www.packtpub.com/packtlib/book/Application-Development/9781785286926/1/ch01lvl1sec13/Our%20project%20%20Comic%20Creator). – 3kstc Jul 18 '16 at 00:48