Python中Kivy小部件之间的交互

时间:2016-03-14 16:56:03

标签: python checkbox kivy

我正在使用kivy做一个proyect,但我的复选框有问题。起初我正在尝试像python编码这样的程序(我知道它是干净的,但我了解更多)我有一个第一个屏幕,有这个编码:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.checkbox import CheckBox




class MainScreen(GridLayout):
    def __init__(self,**kwargs):
        e=[]
        super(MainScreen, self).__init__(**kwargs)
        self.cols=2
        def on_checkbox_active(checkbox, value):
            if value:
                e.append(value)
                print e
            else:
                print('The checkbox', checkbox, 'is inactive')

        self.add_widget(Label(text='Inserta assignatures desitjades',font_size=35))
        self.add_widget(Label(text=''))
        ch1 = CheckBox()
        self.add_widget(ch1)
        self.add_widget(Label(text='Termotecnia'))
        ch2 = CheckBox()
        self.add_widget(ch2)
        self.add_widget(Label(text='Termotecnia'))
        ch3 = CheckBox()
        self.add_widget(ch3)
        self.add_widget(Label(text='Termotecnia'))
        ch4 = CheckBox()
        self.add_widget(ch4)
        self.add_widget(Label(text='Termotecnia'))
        b1=Button(text='Exit',background_color=[0.7,0.7,1,1],font_size=24)  
        self.add_widget(b1)
        b2=Button(text='Next',font_size=24,font_color=[1,3,4,0],background_color=[1,2,3,6]) 
        self.add_widget(b2)
        ch1.bind(active=on_checkbox_active)
        ch2.bind(active=on_checkbox_active)
        b1.bind(on_press=exit)
        b2.bind(on_press=reloaded)
...

class SimpleKivy(App):
    def build(self):
        return MainScreen()


if __name__=='__main__':
    SimpleKivy().run()

我想选择两个或三个选项,并将其保存到下一个屏幕,就像一种选择。如果有人知道怎么做并保存下一个屏幕的信息它会帮助我很多,因为我有所有选项的下一个屏幕的代码,但我想在第一个屏幕预选然后只使用哪个我选择了。此外,如果有人可以帮助我,我想知道当按钮"下一步"时,可以转换到另一个班级(屏幕)。被压了。我知道这个问题很简单,但我在kivy编程方面很新,而且一些概念非常困难。谢谢。

1 个答案:

答案 0 :(得分:0)

您想要的是访问其他类中的变量。有时这可能很烦人,你可以用所有__init__()和东西来做这件事,或者......一种更简单的方法:get_running_app()

您可以创建字典或其他内容,您可以在其中存储其他类需要访问的任何值。它与使用globals类似,但代码行数较少。例如,在您的情况下,您可以使用字典(或嵌套字典,json,...)来存储例如'checkboxes':'<names of checked ones>',并且在每个初始化中,您可以遍历这些值以使复选框active

基本上你需要的是a = App.get_running_app()某个地方以及可以在主要应用程序类中访问的内容。

示例:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string('''
<Root>:
    MainScreen:
        name: 'main'
    AnotherScreen:
        name: 'another'
<MainScreen>:
    BoxLayout:
        Button:
            text: 'next screen'
            on_release: root.parent.current='another'
        Button:
            text: 'ping!'
            on_release: root.ping()
<AnotherScreen>:
    BoxLayout:
        Button:
            text: 'previous screen'
            on_release: root.parent.current='main'
        Button:
            text: 'ping!'
            on_release: root.ping()
''')

class MainScreen(Screen):
    def __init__(self, **kw):
        super(MainScreen, self).__init__(**kw)
        self.a = App.get_running_app()
    def ping(self):
        print self.a.big_dict['hi']

class AnotherScreen(Screen):
    def ping(self):
        b = App.get_running_app()
        print b.big_dict['hi']

class Root(ScreenManager):
    pass
class SimpleKivy(App):
    big_dict={'hi':'hi there!'}
    def build(self):
        return Root()
SimpleKivy().run()

您可以看到,无需拨打__init__(),如果您真的不需要,则无需编写更多代码。

相关问题