mardi 15 mars 2016

How to create a dynamic kivy app with checkboxes?

I'm doing a proyect for my university and i'm doing using the kivy module of python. I want to do a mainscreen where you could select some field, and then the next screen will depend on the fields you have selected in the mainscreen. Doing dynamic with kv language is difficult to me because i don't know too much about programming with kivy, so i have decided to do like python programming but it doesn't work. Here you have the code of my program:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

Builder.load_string('''
<Root>:
    MainScreen:
        name: 'main'
    AnotherScreen:
        name: 'another'

<MainScreen>:
    GridLayout:
        cols: 2
        Label:
            text: "Select Subjects"
            font_size: 15
        Label:
            text: " "
        CheckBox:
            on_active:root.ping('ch1')
        Label:
            text: "Termotecnia"
        CheckBox:
            on_active:root.ping('ch2')
        Label:
            text: "Control"
        CheckBox:
            on_active:root.ping('ch3')
        Label:
            text: "Termotcnia"
        CheckBox:
            on_active:root.ping('ch4')
        Label:
            text: "Control"
        Button:
            text: "Exit"
            background_color: .7, .7, 6, 1
            on_release:root.parent.current='another'
        Button:
            text: "Run"
            font_size: 24
            background_color: .7, .7, 1, 1
            on_release: root.parent.current='another'


''')


class MainScreen(Screen):
    def __init__(self, **kw):
        super(MainScreen, self).__init__(**kw)
        self.a = App.get_running_app()
        self.e={}
    def ping(self,n):
        if self.a.big_dict[n]=='False':
            self.a.big_dict[n]='True'
        else: 
            self.a.big_dict[n]='False'
        print self.a.big_dict
        self.e=self.a.big_dict

class AnotherScreen(GridLayout):
    def __init__(self, **kw):
        super(AnotherScreen, self).__init__(**kw)
        t=[] 
        self.i=MainScreen()
        self.cols=2
        self.add_widget(Button(text='Assignatura',background_color=[0,1,1,1]))
        self.add_widget(Button(background_color=[0,1,1,1],text='Escriu Grup'))
        for k in self.i.e:
            if self.i.e[k]=='True':
                self.add_widget(Label(text=k))
                self.k=TextInput(multiline=False)
                t.append(self.k.text)
                self.add_widget(self.k)
            else:
                pass
        b1=Button(text='Exit',background_color=[0,1,0,1])
        self.add_widget(b1)
        b2=Button(text='Run',background_color=[0,1,0,1])
        self.add_widget(b2)
        b1.bind(on_press=exit)


class Root(ScreenManager):
    pass
class SimpleKivy(App):
    big_dict={'ch1':'False','ch2':'False','ch3':'False','ch4':'False'}
    def build(self):
        return Root()
SimpleKivy().run()

This code doesn't work because ScreenManager only works with a screen, no with a GridLayout (first problem).the big_dict save the data of the checkboxes, but when i try to import to another class, it doesn't work (another problem). If anyone knows the answer to this problems it would be very helpful, because i know how to do with e.g 13 fields always, but i want to do with a preselection, because there are to many fields (in this program only 4, but there are 14).Thanks a lot.




Aucun commentaire:

Enregistrer un commentaire