简体   繁体   English

使用 Kivy,如何让复选框的活动(或非活动)状态在第二个屏幕上旋转(或不旋转)图像?

[英]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.在第二个屏幕上,我的代码当前使用canvas并将图像旋转 90 度。 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.如果未选中该框,则意味着用户希望图像为纵向模式,我需要清除 canvas。

my.kv我的.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,首先在SecondWindow中设置属性以设置角度、拉伸指令等,然后根据MainWindow中的 state(复选框的弱引用) checkbox_confirm_mode等来控制它们,
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 ,现在在kvlangSecondWindow中,
<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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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