Domanda

Vorrei ottenere il tuo aiuto con il mio .py script.

Sto usando il file XML per memorizzare il percorso del parser per le immagini.

Ecco il file XML che uso:

<?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>
.

Ecco lo script .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')
.

Ecco il parser 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))
.

Ho provato con:

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

Voglio sapere come utilizzare per scrivere in Python per XML Parser include se dichiarando che se ho l'immagine chiamata "immagine 2.jpg" che è il restituito come vero, allora potrei fare qualcosa?

È stato utile?

Soluzione

Ecco il modo più semplice per adattare il tuo codice per trovare il controllo il cui texture è Image 2.jpg:

In primo luogo, stai cercando un elemento denominato texturefocus. Ma nel tuo campione XML, non esiste un elemento del genere - e anche se c'erano, quello che stai cercando è denominato texture. Quindi ovviamente è necessario risolvere questo:

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

Secondo, stai cercando un'immagine Image 2.jpg, ma non c'è immagine del genere nel tuo XML, quindi non lo troverai. C'è un Image 2.png, ma non è la stessa cosa. Quindi, presumibilmente è necessario fissarlo.

E ora, l'istruzione if è banale:

if texture == 'Image 2.png':
.

La domanda è, cosa vuoi fare quando lo trovi? Basta stampare una stringa non aiuterà il resto del tuo codice utilizzare quel valore.

Diciamo che ciò che vuoi fare è scrivere una funzione che restituisce il description se c'è un'immagine il cui texture è Image 2.png o restituisce altrimenti None. Quindi:

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
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top