用kivy做一个窗口

2024-06-16 10:27:15 发布

您现在位置:Python中文网/ 问答频道 /正文

  • 控件值:“ID”、“measuredH和measuredW”、“typeID”、“TitleWindow”、“x”、“y”和“zOrder”。

  • 键属性的每个值。

  • 控件值:“measuredH”、“measuredW”、“mockupH”:“mockupW”和“version”:“1.0”

Json文件-数据/BalsamiqJsonData.json文件你知道吗

{
"mockup": {
    "controls": {
        "control": [
            {
                "ID": "0",
                "h": "390", //  Height - Window
                "measuredH": "400",
                "measuredW": "450",
                "properties": {
                    "bottomheight": "29",
                    "text": "W4 System", // Title Window
                    "topheight": "26"
                },
                "typeID": "TitleWindow", // Know this control is a window.
                "w": "438", // Width Window
                "x": "181", // Position X Window.
                "y": "19", // Position Y Window.
                "zOrder": "0"
            }
        ]
    },
    "measuredH": "409",
    "measuredW": "619",
    "mockupH": "390",
    "mockupW": "438",
    "version": "1.0"
}

}

文件-主.py你知道吗

import kivy
kivy.require("1.9.1")

from kivy.app import App
from kivy.config import Config
from element import *


path_file = 'Data/BalsamiqJsonData.json'

class WinBallApp(App):     

    Config.rsizable = 0

    def build(self):
       Element().open_file(path_file)


if __name__ == "__main__":
    WinBallApp().run()

文件-元素.py你知道吗

import json
from kivy.config import Config
from kivy.core.window import Window
from kivy.core.window import WindowBase

class Element(): 

    def __init__(self, elem_id = 0, elem_type = "", elem_caption = "", 
        elem_height = 0, elem_width = 0, elem_pos_x = 0, elem_pos_y = 0, 
        elem_z_order = 0, elem_text = "", version = 0):

        self.elem_id = elem_id
        self.elem_type = elem_type
        self.elem_caption = elem_caption
        self.elem_height = elem_height
        self.elem_width = elem_width
        self.elem_pos_x = elem_pos_x
        self.elem_pos_y = elem_pos_y
        self.elem_z_order = elem_z_order
        self.elem_text = elem_text

    def show_data(self, *args):
        print(args)


    def make_window(self):


        Window.set_title(self.elem_caption)
        Window.size = (self.elem_height, self.elem_width)


    def check_type(self, elem_type):

        if elem_type == 'TitleWindow':
            self.make_window()     


    def manipulate_data(self, data):

        for mockup_key, mockup_value in data.items():

            mockup_control = mockup_value['controls']['control']

            self.elem_id = int(mockup_control[0]['ID'])
            self.elem_type = mockup_control[0]['typeID']
            self.elem_caption = mockup_control[0]['properties']['text']
            self.elem_height = int(mockup_control[0]['h'])
            self.elem_width = int(mockup_control[0]['w'])
            self.elem_pos_x = int(mockup_control[0]['x'])
            self.elem_pos_y = int(mockup_control[0]['y'])
            self.elem_z_order = int(mockup_control[0]['zOrder']) 

            self.show_data(self.elem_id, self.elem_type, self.elem_caption, self.elem_height,
                self.elem_width, self.elem_pos_x, self.elem_pos_y, self.elem_z_order)

            self.check_type(self.elem_type)   


    def open_file(self, path_file):
        with open(path_file, mode='r') as jsonData:            
            load_data = json.load(jsonData)

        self.manipulate_data(load_data) 

Ask - How I change the settings window using the values of json file like title, position x, position y, height, width ?


Tags: posimportselfdatadeftypewindowwidth
1条回答
网友
1楼 · 发布于 2024-06-16 10:27:15

使用org.simple.json

您可能注意到每个get方法都返回一个Object。这就是为什么这个库有“simple”这个词的原因,它是一个非常小的库,您需要独立地强制转换每个对象。因此,链接操作不能干净地完成。你知道吗

JSONParser parser = new JSONParser();

try {
    JSONObject obj = (JSONObject) parser.parse(new FileReader("data.json"));

    JSONObject mockup = (JSONObject) obj.get("mockup");
    JSONObject controls = (JSONObject) mockup.get("controls");
    JSONArray control = (JSONArray) controls.get("control");

    for (int i = 0; i < control.size(); i++) {
        JSONObject controlObj = (JSONObject) control.get(i);
        System.out.println(String.valueOf(controlObj.get("typeID")));
    }

} catch (ParseException e) {
    e.printStackTrace();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

相关问题 更多 >