Python中Kivy小部件之间的交互 [英] Interaction between Kivy widgets in Python

查看:207
本文介绍了Python中Kivy小部件之间的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing a proyect using kivy but i have a problem with the checkboxes. At first I'm trying to do the program like python coding (I know it is'nt clean, but I understand more) And i have a first screen with this coding:

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

例如,我想选择两个或三个选项,并将其保存为下一个屏幕,如选择类型。如果任何人知道如何做和保存信息为下一个屏幕它woul帮助我很多,因为我有下一个屏幕的所有选项的代码,但我想在第一个屏幕中预选,然后只使用哪个已选择。如果任何人可以帮助我,我想知道,当按下下一步按钮时,转换到另一个类(屏幕)。我知道这个问题很简单,但我是新的kivy编程和一些概念是相当困难。谢谢。

I want to select two or three options for example, and save it for the next screen, like a type of selection. If anyone knows how to do it and save information for the next screen it woul help me a lot, because i have the code of the next screen for all the options, but i want to preselect in the first screen and then only use which i have selected. Also if anyone can help me, i want to know hoy to do the transition to another class (screen) when the button "Next" is pressed. I know this question are pretty simple but I'm new in kivy programming and some concepts are pretty difficult. Thanks.

推荐答案

你想要的是访问其他类中的变量。有时候这可能很烦人,你可以用所有 __ init __()和东西,或者...一个更简单的方法来实现它: get_running_app()

What you want is accessing variables in other classes. Sometimes this can be annoying and you can do it either hard way with all __init__() and stuff, or... a simplier way comes along: it's get_running_app().

您可以创建字典或其他内容,您可以在其中存储其他类需要访问的任何值。它类似于使用全局,它花费的代码行更少。例如在你的case你可以使用字典(或嵌套的字典,json,...)存储例如'checkboxes':'<检查的名称>'在每个初始化你可以循环这些值,使复选框活动

You can create a dictionary or something else, where you can store any value your other classes need to access. It's similar to using globals and it costs you less lines of code. For example in your case you could use a dictionary(or nested dictionaries, json, ...) to store for example 'checkboxes':'<names of checked ones>' and in each init you can loop over these values to make checkboxes active

基本上你需要的是 a = App.get_running_app()某处可以访问main - App - class。

Basically all you need is a = App.get_running_app() somewhere and something to access in main - App - class.

示例:

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 __(),如果你真的不需要,不需要写更多的代码行。

You can see there's no need to call __init__(), no need to write more lines of code if you really don't need to.

这篇关于Python中Kivy小部件之间的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆