簡體   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?

在第一個屏幕上,我有一個復選框,詢問用戶是否希望他們的圖像處於橫向模式。 在第二個屏幕上,我的代碼當前使用canvas並將圖像旋轉 90 度。 如果未選中該框,則意味着用戶希望圖像為縱向模式,我需要清除 canvas。

我的.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

主程序

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

對於您當前的設計,您可以從外部控制它,因為,

  1. 首先在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. 現在在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