-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')

xml パーサー用に Python で記述する方法を知りたいのですが、true として返される「Image 2.jpg」という画像がある場合、何かできるという if ステートメントが含まれていますか?

4

1 に答える 1

2

textureが であるコントロールを見つけるようにコードを適応させる最も簡単な方法を次に示しますImage 2.jpg

まず、 という名前の要素を探していますtexturefocus。しかし、あなたの XML サンプルには、そのような要素はありません。あったとしても、探している要素の名前はtextureです。したがって、明らかにそれを修正する必要があります:

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

次に、イメージを探していますがImage 2.jpg、XML にそのようなイメージがないため、見つけることができません。がありますが、Image 2.pngそれは同じものではありません。したがって、おそらくそれも修正する必要があります。

そして今、ifステートメントは自明です:

if texture == 'Image 2.png':

問題は、それを見つけたらどうしたいですか?文字列を出力するだけでは、コードの残りの部分がその値を使用するのに役立ちません。

あなたがしたいことは、descriptionが である画像があればtextureImage 2.png返し、そうでなければ を返す関数を書くことだとしましょうNone。それで:

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 に答える