简体   繁体   中英

Using Kivy, how do I have a checkbox's active (or inactive) status rotate (or not rotate) an image on the second screen?

On the first screen, I have a checkbox that asks the user if they want their image in landscape mode. On the second screen, my code currently uses a canvas and rotates the image 90 degrees. If that box is not checked, that means the user wants the image to be portrait mode, and I need that canvas to be cleared.

my.kv

WindowManager:
    MainWindow:
    SecondWindow:

<MainWindow>:
    id: main_window
    name: "main"

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        padding: 50

        Label:
            text: "Email"
            color: 0,0,0,1
            font_size: 32

        BoxLayout:
            orientation: "horizontal"
            Label:
                text: "Email Address:"
                color: 0,0,0,1

            TextInput:
                size_hint_y: None
                pos_hint: {'center_y': .5}
                height: 38
                multiline: True
                padding: 10

        BoxLayout:
            orientation: "horizontal"

            Label:
                text: "Display Landscape Mode?"
                color: 0,0,0,1
            CheckBox:
                id: checkbox_confirm_mode
                on_active:
                    root.checkbox_click_mode(self, self.active)
                pos_hint: {'center_x': .5}

        BoxLayout:
            orientation: "horizontal"

            Label:
                text: "Stretch image to fill screen?"
                color: 0,0,0,1
            CheckBox:
                id: checkbox_confirm_stretch
                on_active:
                    root.checkbox_click_stretch(self, self.active)
                pos_hint: {'center_x': .5}

        BoxLayout:
            orientation: "horizontal"

            Label:
                text: "I double-checked that my email is typed correctly:"
                color: 0,0,0,1
            CheckBox:
                id: checkbox_confirm_email
                on_active:
                    root.checkbox_click(self, self.active)
                    root.disable_button()
                pos_hint: {'center_x': .5}

        BoxLayout
            orientation: "vertical"

            Button:
                id:submit_button
                text: "Submit"
                disabled: True
                size_hint: (0.2, None)
                pos_hint: {'center_x': .5}
                height: 50

                on_release:
                    app.root.current = "second"
                    root.manager.transition.direction = "left"

<SecondWindow>:
    id: second_window
    name: "second"
    canvas:
        Rotate:
            angle: 90
            origin: self.center

    Image:
        source: 'Troy.png'
        keep_ratio: True
        allow_stretch: False

main.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')


class MainWindow(Screen):
    def on_pre_enter(self):
        Window.size = (750,400)
        Window.clearcolor = (1, 1, 1, 1)

    def checkbox_click(self, instance, value):
        return value

    def checkbox_click_mode(self, instance, value):
        return value

    def checkbox_click_stretch(self, instance, value):
        return value

    def clear_canvas(self):
        self.canvas.clear()
        return

    def disable_button(self):
        if self.ids.checkbox_confirm_email.active == False:
            self.ids.submit_button.disabled = True
        else:
            self.ids.submit_button.disabled = False


class SecondWindow(Screen):
    def on_pre_enter(self):
        Window.size = (500,500)
        Window.clearcolor = (0,0,0,0)
    pass

class WindowManager(ScreenManager):
    pass

class MyMainApp(App):
    def build(self):
        return kv

kv = Builder.load_file("my.kv")

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

For your current design you can control it from outside as,

  1. First set properties in SecondWindow for setting the angle, stretch instruction etc. and then control them depending on the state of those (weak reference of the check boxes) checkbox_confirm_mode etc. from the MainWindow as,
class SecondWindow(Screen):
    angle = NumericProperty(0)
    allow_strech = BooleanProperty(False)

    def on_pre_enter(self):
        screen = self.manager.get_screen("main")
        angle_value = screen.ids.checkbox_confirm_mode.active
        stretch_value = screen.ids.checkbox_confirm_stretch.active
        self.angle = 90*angle_value # Less calculation.
        self.allow_strech = stretch_value # Less calculation.
        Window.size = (500,500)
        ...
  1. Now in the kvlang of SecondWindow ,
<SecondWindow>:
    id: second_window
    name: "second"
    canvas:
        Rotate:
            angle: root.angle
            origin: self.center

    Image:
        source: 'Troy.png'
        keep_ratio: True
        allow_stretch: root.allow_strech

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM