Kivy Checkbox限制点击

时间:2016-09-05 22:07:50

标签: python python-2.7 checkbox kivy kivy-language

我正在尝试在我的浮动布局区域中创建一个复选框。我正在获取生成的复选框,但每次单击它都会从True变为False,即使它不在框中。有人可以提供一些见解吗?

我的代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.checkbox import CheckBox
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget


#######``Windows``#######
class MainScreen(Screen):
    pass

class AnotherScreen(Screen):
    def add(self, *args):
        a = self.ids["a"].text
        a = int(a)
        self.ids["adds"]
        plus = 1 + a
        print plus

class ScreenManagement(ScreenManager):
    pass


presentation = Builder.load_file("GUI_Style.kv")

class MainApp(App):
    def build(self):
        return presentation

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

我的KV文件:

#: import NoTransition kivy.uix.screenmanager.NoTransition

ScreenManagement:
    transition: NoTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>:
    background_color: 0.5, 1, 0.22, 1
    name: "main"
    FloatLayout:
        Button:
            text: "Quit"
            font_size: 50
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3,0.2
            pos_hint: {"x":0, "y":0}
            on_release: app.window().stop()
        Button:
            on_release: app.root.current = "other"
            text: "Next Screen"
            font_size: 50
            color: 0,1,0,1
            font_size: 25
            size_hint: 0.3,0.2
            pos_hint: {"right":1, "top":1}

<AnotherScreen>:
    name: "other"
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        CheckBox:
            pos_hint: {"x":0.2, "y":0.2}
            on_release: True
        TextInput:
            id: a
            font_size: 25
            password: True
            pos_hint: {"x":0.5, "y":0.5}
            size_hint: 0.3,0.2
            text: "Insert Side A here"
        Button:
            id: adds
            background_color: 0,0,1,1
            color: 0,1,0,1
            font_size: 25
            on_release: root.add()
            pos_hint: {"x":0, "y":0}
            size_hint: 0.3,0.2
            text: "plus"
        Button:
            color: 0,1,0,1
            background_color: 0,0,1,1
            font_size: 25
            size_hint: 0.3,0.2
            text: "Back Home"
            on_release: app.root.current = "main"
            pos_hint: {"right":1, "top":1}

1 个答案:

答案 0 :(得分:0)

尝试将CheckBox放在Widget中,它可以正常工作。我的意思是:

FloatLayout:
    Widget:
        CheckBox:
            pos_hint: {"x":0.2, "y":0.2}
            on_release: True
    TextInput:
        id: a
        font_size: 25
        password: True
        pos_hint: {"x":0.5, "y":0.5}
        size_hint: 0.3,0.2
        text: "Insert Side A here"
    Button:
        id: adds
        background_color: 0,0,1,1
        color: 0,1,0,1
        font_size: 25
        on_release: root.add()
        pos_hint: {"x":0, "y":0}
        size_hint: 0.3,0.2
        text: "plus"

当然它会破坏你的布局,但它解决了你遇到的问题。