-1

我想获得您对我的 .py 脚本的帮助。

我正在使用 xml 文件来存储图像的解析器路径。

这是我使用的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<window type="dialog">
    <allowoverlay>no</allowoverlay>
<coordinates>
<system>1</system>
<posx>0</posx>
<posy>0</posy>
</coordinates>

<controls>
    <control type="image" id="1">
       <posx>0</posx>
       <posy>0</posy>
       <width>1280</width>
       <height>720</height>
       <texture>background-defeat.png</texture>
       <animation effect="fade" start="0" end="100" time="6500">WindowOpen</animation>
    </control>

     <control type="image" id="2">
      <description>Image 2</description>
      <posx>307</posx>
      <posy>7</posy>
      <width>154</width>
      <height>95</height>
      <visible>true</visible>
      <texture>Image 2.png</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>    

     <control type="image" id="3">
      <description>Image 3</description>
      <posx>460</posx>
      <posy>7</posy>
      <width>188</width>
      <height>95</height>
      <visible>true</visible>
      <texture>Image 3.jpg</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>

    <control type="image" id="4">
      <description>Image 4</description>
      <posx>648.5</posx>
      <posy>7</posy>
      <width>165</width>
      <height>95</height>
      <visible>true</visible>
      <texture>recorded_blue.jpg</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>

    <control type="image" id="5">
      <description>Image 5</description>
      <posx>813.5</posx>
      <posy>7</posy>
      <width>149</width>
      <height>95</height>
      <visible>true</visible>
      <texture>Image 5.jpg</texture>
      <animation effect="fade" start="0" end="100" time="1500">WindowOpen</animation>
    </control>
</controls>
</window>

这是 .py 脚本:

import xbmc 
import xbmcgui
import os

#get actioncodes from keymap.xml
ACTION_MOVE_LEFT = 1
ACTION_MOVE_RIGHT = 2
ACTION_MOVE_UP = 3
ACTION_MOVE_DOWN = 4
ACTION_PREVIOUS_MENU = 10
ACTION_BACKSPACE = 110



class MyClass(xbmcgui.WindowXML):
  def onAction(self, action):

    if action == ACTION_BACKSPACE:
         self.close()

    if action == ACTION_PREVIOUS_MENU:
         self.close()

    if action == ACTION_MOVE_LEFT:

         if os.path.exists('Q:\\resources\skins\Default\media\image 4.jpg') == True:
             self.strAction = xbmcgui.ControlLabel(300, 200, 600, 200, '', 'font14', '0xFF00FF00')
             self.addControl(self.strAction)
             self.strAction.setLabel('Image is exist')

这是xml解析器:

import xml.etree.ElementTree as ET

filename = 'script-tvguide-mainmenu.xml'
tree = ET.parse(filename)
root = tree.getroot()
controls = root.find('controls')

for control in controls.findall('control'):
    #how do you create the if statement to check for the image through on xml if they are exist?
    # Here are the image filenames, focus and nofocus.
    focus = control.find('texturefocus').text
    nofocus = control.find('texturenofocus').text
    print('texturefocus={0}, texturenofocus={1}'.format(focus, nofocus))

我试过:

if action == ACTION_MOVE_LEFT:
         filename = 'script-tvguide-mainmenu.xml'
         tree = ET.parse(filename)
         root = tree.getroot()
         controls = root.find('controls')

         for control in controls.findall('control'):
             texture = control.find('texture').text

             if texture == 'tvguide_yellow.png':
                  self.strAction = xbmcgui.ControlLabel(300, 200, 600, 200, '', 'font14', '0xFF00FF00')
                  self.addControl(self.strAction)
                  self.strAction.setLabel('Image is exisit')

我想知道你如何使用python为xml解析器编写包含if语句,如果我有一个名为“Image 2.jpg”的图像,它返回为真,那么我可以做些什么?

4

1 回答 1

2

Here's the simplest way to adapt your code to find the control whose texture is Image 2.jpg:

First, you're looking for an element named texturefocus. But in your XML sample, there is no such element—and even if there were, the one you're looking for is named texture. So obviously you need to fix that:

texture = control.find('texture').text

Second, you're looking for an image Image 2.jpg, but there is no such image in your XML, so you're not going to find it. There is an Image 2.png, but that's not the same thing. So, presumably you need to fix that as well.

And now, the if statement is trivial:

if texture == 'Image 2.png':

The question is, what do you want to do when you find it? Just printing out a string isn't going to help the rest of your code use that value.

Let's say that what you want to do is to write a function that returns the description if there's an image whose texture is Image 2.png, or returns None otherwise. Then:

def find_image2(filename):
    tree = ET.parse(filename)
    root = tree.getroot()
    controls = root.find('controls')

    for control in controls.findall('control'):
        texture = control.find('texture')
        if texture and texture.text == 'Image 2.png':
            return control.find('description').text
于 2014-01-09T20:34:06.277 回答