簡體   English   中英

將變量值從.KV 文件雙向發送到 python 的最佳方法是什么?

[英]What is the best way to send variable values bidirectionally from .KV file to python?

從 KV 文件向 python 發送和接收數據的最佳實踐是什么? 試着做一個簡單的程序,用三個推子改變 object 的顏色。 我希望設置滑塊位置的初始基值和 colors 滑塊將控制的位置將在程序第一次運行時開始。 然后我希望能夠獲取 slider 值並在項目的其他部分使用這些值,然后在其他文本框中顯示值或其他值。

我確實打算在這個項目中添加一個串行或網絡控件。 基本上我想讓程序的多個實例運行並且一個實例的 slider 運動值來控制另一個實例。 實現這一點超出了項目當前階段的 scope,但如果有一些額外的步驟將滑塊的值連接到其他代碼塊來實現這樣的功能。 任何投入將不勝感激。

到目前為止,這就是我被困在.py 中的第 6 行的內容。 我不確定如何獲取 3 個 slider 值並將它們輸入文本框顏色值。 同時能夠在程序的其他部分(例如打印語句或另一個文本框)中顯示值。

from kivy.app import App
from kivy.uix.gridlayout import GridLayout

class MyLayout(GridLayout):
    def slide(self):
        self.ids["lable"].color = red,green,blue,1
        print(self.ids["red"].value)


class slider(App):

    def build(self):

        return MyLayout()

if __name__ == "__main__":
    window = slider()
    window.run()

.kv

#:kivy 1.10.0

<MyLayout>

    cols:2

    Slider:
        id: red
        min:0
        max:1
        #on_value:label.text = str(int(self.value))
        on_value: root.slide()


    Slider:
        id: green
        min:0
        max:1
        #on_value:label.text = str(int(self.value))
        on_value: root.slide()

    Slider:
        id: blue
        min:0
        max:1
        #on_value:label.text = str(int(self.value))
        on_value: root.slide()

    Label:
        id:label
        text:"0"

您看到的所有代碼都是我從我找到的教程中拼湊起來的代碼,所以如果我以錯誤的方式進行處理,因為我還不了解 Kivy 或 python。 大多數教程假設您非常了解 python 並且對面向 object 的編程有深入的了解。

您可以在您的slide()方法中調整Label的顏色:

def slide(self):
    self.ids["label"].color = self.ids.red.value, self.ids.green.value, self.ids.blue.value, 1
    print(self.ids["red"].value)

如果要在Label中顯示顏色:

Label:
    id:label
    color: 1, 0.5, 0.75, 1  # initial color
    text: str(round(self.color[0], 2)) + ', ' + str(round(self.color[1], 2)) + ', ' + str(round(self.color[2], 2))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM