python - 如何访问 kivy 中不同类的小部件

标签 python kivy kivy-language

如何在 AddLocationForm 中通过 Python 访问位于 CurrentWeather 小部件内的条件值?我想从 AddLocationForm 编辑条件值,而不是在 CurrentWeather 中进行编辑。到目前为止,这是我的代码:

Python:

from kivy.network.urlrequest import UrlRequest
import json
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.uix.listview import ListItemButton
from kivy.factory import Factory

Window.clearcolor = (0.45, 0.3, 1, 0.5)

class AddLocationForm(BoxLayout):
    search_input = ObjectProperty()
    search_results = ObjectProperty()
    currentWeather = ObjectProperty()

    def search_location(self):
        search_template = "http://api.openweathermap.org/data/2.5/weather?q={}&appid=f8020ca79e2de4c1dd5a125c3f22fa18"
        search_url = search_template.format(self.search_input.text)
        request = UrlRequest(search_url, self.found_location)

    def found_location(self, request, data):
        data = json.loads(data.decode()) if not isinstance(data, dict) else data
        weather = "%s" %(data["weather"][0]["description"])
        root.WeatherRoot.CurrentWeather.conditions = weather #Problem is here

class LocationButton(ListItemButton):
    pass

class WeatherApp(App):
    def build(self):
        global root
        root = self.root

if __name__ == '__main__':
    WeatherApp().run()

Kv:

#: import ListItemButton kivy.uix.listview.ListItemButton
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import main main

WeatherRoot:

<WeatherRoot>:
    AddLocationForm

<AddLocationForm>:
    orientation: "vertical"
    search_input: search_box 
    search_results: search_results_list
    BoxLayout:
        height: "40dp"
        size_hint_y: None
        TextInput:
            id: search_box 
            size_hint_x: 50
            multiline: False
        Button:
            text: "Search"
            size_hint_x: 25
            on_press: root.search_location()
            background_color: (1, 0.5, 0.656, 1)

<CurrentWeather@BoxLayout>:
    id: current_weather
    currentWeather: current_weather
    location: "" 
    conditions: ""
    temp: None
    temp_min: None
    temp_max: None

    orientation: "vertical"
    Label:
        text: root.location
        pos_hint: {'x': 0.435, "y": 1}
        font_size: '40dp'       
        size_hint_y: None
        size_hint_x: None
    BoxLayout:
        orientation: "horizontal"

        Label:
            id: curCon
            text: root.conditions

最佳答案

树:

WeatherRoot:           # root widget

<WeatherRoot>:         # root widget class
    AddLocationForm    # root widget's child

因此,您将首先访问您的 App 的实例 app 。构建方法之后的实例有 root property ,它只是存储的根小部件的一个实例。这就是为什么global root在较大的代码中是无用的,因为例如冲突、与其他名称混淆等,同时以一种漂亮的方式更容易访问。

要从 python 或 kv 访问在 kv 中动态创建的类,您需要使用小部件树(以获取已存在的实例):

app.root.children[0]...

Factory 创建小部件的实例,例如如果您想在评估某个条件后创建一个新条件。

但是!

您误解了动态类在 kv 中的工作方式 - <Name@Widget>:只是一个声明,而不是一个实例。就像一个模板。要实际使用它,您必须它放在某个地方,例如:

python :

cw = Factory.CurrentWeather()
somewhere.add_widget(cw)

kv:

WeatherRoot:
    CurrentWeather:  # here

<WeatherRoot>:
    AddLocationForm:
        CurrentWeather:  # or here

如果您选择在Python中添加它,您可以轻松地使用一些变量来存储实例,并且以后仍然可以通过小部件树访问它。另一方面,当添加到 kv 中时,您无权访问 __init__如果没有像这样愚蠢的黑客行为,你就无法真正将实例存储到变量中:

#:import Factory kivy.factory.Factory

<Blob@Button>:

BoxLayout:
    Button:
        on_release: self.new = Factory.Blob(); self.parent.add_widget(self.new)

因此,要访问它,您必须首先创建它的一个实例,然后存储该实例,或者遍历小部件树。

关于python - 如何访问 kivy 中不同类的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42405033/

相关文章:

android - Kivy - 如何从 ListView 调用函数?

python /基维 : How to put dynamic label widget and value

python - 了解张量点

python - 使用 kivy 窗口同时运行循环

python - Kivy on_touch_move 和 DragBehaviour 不移动 Image Widget

python - 在 Android 上的 Kivy Launcher 中启动应用程序时生成的日志

android - 如何为 Kivy 中的按钮添加功能

python - OpenCV - 在将图像写入文件时指定格式 (cv2.imwrite)

python - 关注 xs :include when parsing XSD as XML with lxml in Python

python - Beautiful Soup 将标准普尔变成标准普尔; AT&T 变成 AT&T; ?