【问题标题】:How do I make my variables accessible from .py to .kv (kivy)如何使我的变量可以从 .py 访问到 .kv (kivy)
【发布时间】:2022-01-05 01:09:22
【问题描述】:

我正在尝试在 Python 的 Kivy 框架上制作一个移动应用程序

我遇到了个人问题,我无法从我的 .py 文件访问我的变量到我的 .kv 文件

我正在尝试制作的应用程序,它会扫描您周围的 wifi 网络并连接到它。所以意思是,我必须找到一种方法来访问我的变量从我的 .py 文件到我的 .kv 文件(kivy)

我正在尝试这个:text: {VARIABLE/FUNCTION HERE FROM .py}

这是我的 .PY 文件中的代码

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
#from wifidroid.wifi import WifiManager
from kivy.uix.textinput import TextInput
import kivymd, kivy
#import wifidroid
from kivy.uix.label import Label
from kivy.uix.button import Button

# I WANT TO DISPLAY THESE RESULTS IN MY .KV FILE.. (text:{variables/function})
''' 
wifi = WifiManager()
wifi.startScan()
for i in range(wifi.ScanResults.size()):
ssid = [wifi.ScanResults.get(i).SSID]
bssid = [wifi.ScanResults.get(i).BSSID]
levell = [wifi.ScanResults.get(i).level]
print(ssid[0]+" "+bssid[0]+" "+str(levell[0]))
'''



class Layout_For_App(FloatLayout):
    def test(self, dt):
        self.btn.text = str('Test')
    pass


class AndrdWifiApp(App):
    def build(self):
        return Layout_For_App()


AndrdWifiApp().run()

我评论了一些东西,因为我需要在 Android 上进行测试

这是我的 .KV 文件中的代码

<Layout_For_App>:
    Label:
        text: 'Developed By Anonymous'
        pos_hint: {"x":0,"y":0.45}

    Label:
        text: 'OUTPUT' # I WANT MY WIFI SCANS TO DISPLAY HERE (I NEED VARIABLES FROM .PY OR 
        A FUNCTION)
        background_color: (1, 1, 1, 1)
        size_hint: (0.451, 0.7)
        pos_hint: {"x":0.27,"y":0.17}
        valign: "middle"
        halign: "left"
        color: (0,1,0)
        canvas.before:
            Color:
                rgba: self.background_color
            Rectangle:
                size: self.size
                pos: self.pos
        border: 3, 3
    Button:
        text: 'CRACK'
        background_color: 1, 0, 0, 1
        color: 0, 1, 1
        size_hint: (0.4, 0.15)
        pos_hint: {"x":0.0199,"y":0.01}
        font_size: 30

    TextInput:
        size_hint: (0.5, 0.15)
        pos_hint: {'x':0.49,'y':0.01}
        font_size: 40

请帮帮我。

这是我的应用程序的图片: I need the OUTPUT to be the displays of wifi names

【问题讨论】:

  • 我知道你做不到——你必须用 Python 编写代码来更改文本
  • 也许你应该在kv 中定义变量,然后在__inti__ 中的Layout_For_App 类中为相同的Properties() 赋值
  • 我宁愿将 ID 添加到 Label 中的 kv 和稍后的 __init__ 我会将值复制到 self.ids.label_id.text
  • 我检查了documentation for kv 并且有一个示例Designing with the Kivy Languageinfo = StringProperty() 类中定义,然后它可以在kv 中以root.info 访问它。但它必须是类中的Property,而不是外部变量,并且您仍然必须运行__init__ 将值从外部变量复制到info

标签: python python-3.x kivy kivy-language kivymd


【解决方案1】:

我认为您不能访问不属于 Layout_For_App 类的变量。

基于documentation for kv 和示例Designing with the Kivy Language

您可能必须在类中将变量定义为Property

class Layout_For_App(FloatLayout):
    
    output = StringProperty()

然后你可以在kv 中使用root.

<Layout_For_App>:

    Label:
        text: 'OUTPUT' + root.output

但它还需要使用__init__来设置这个变量的值。

wifi = WifiManager()
wifi.startScan()

lines = []
for i in range(wifi.ScanResults.size()):
    ssid  = [wifi.ScanResults.get(i).SSID]
    bssid = [wifi.ScanResults.get(i).BSSID]
    level = [wifi.ScanResults.get(i).level]
    lines.append( f"{ssid[0]} {bssid[0]} {level[0]}" )

text = "\n".join(lines)

class Layout_For_App(FloatLayout):
    
    output = StringProperty()
    
    def __init__(self, *args, **kwargs):
        super().__init__(self, *args, **kwargs)

        self.output = text

最终你应该将id 分配给Label

<Layout_For_App>:

    Label:
        id: label_output

并在代码中使用此id 来设置text

class Layout_For_App(FloatLayout):
    
    def __init__(self, *args, **kwargs):
        super().__init__(self, *args, **kwargs)

        self.ids.label_output.text = text

在这两个版本中,您都必须在 Python 中运行代码才能将文本分配给这个 Label

【讨论】:

  • 谢谢!!它奏效了。
  • 您可以将我的答案标记为已接受,几分钟后您就可以投票了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 2015-07-24
相关资源
最近更新 更多