the-generalist.com

  • Increase font size
  • Default font size
  • Decrease font size
joomla templates, wordpress themes, drupal, datalife engine, graphics, seo,
Home

Simple Eve Online PyQt Python Example

E-mail Print PDF

PyQt is excellent for rapid program development. I've decided to create an example of how to quickly use data from the Eve Online API with PyQt. This example loads the Characters info into a table. I want to keep the first example as simple as possible to just help someone get started. What you'll need is Python (I recommend 2.7) and the matching version of PyQt. Here's the code, I'll explain below:

The Code

import sys
import urllib
import xml.dom.minidom
from PyQt4 import QtGui

#Enter your UserID and API Keys here
UserID = 'XXXXXXX'
Key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
class MainWindow(QtGui.QWidget):
    def __init__(self):
        #Create the layout and the table widget.
        QtGui.QMainWindow.__init__(self)
        layout = QtGui.QHBoxLayout(self)
        self.table = QtGui.QTableWidget(self)
        layout.addWidget(self.table)
        self.setLayout(layout)

        #Download the data and fill out the table.
        self.downloadData()
        self.setTableHeaders()
        self.setTableData()

    def downloadData(self):
        #These lines download the data from the website and load it into an XML parser
        downloadedData = urllib.urlopen('http://api.eve-online.com/account/Characters.xml.aspx?userid=%s&apikey=%s' % (UserID, Key))
        self.XMLData = xml.dom.minidom.parse(downloadedData)

    def setTableHeaders(self):
        #Get the column titles from the XML and load them into the table
        headerNode = self.XMLData.getElementsByTagName("rowset")[0]
        self.columnHeaders = headerNode.attributes['columns'].value.split(',')
        self.table.setColumnCount(len(self.columnHeaders))
        self.table.setHorizontalHeaderLabels(self.columnHeaders)

    def setTableData(self):
        #iterate through the data in the XML and load it into the table    
        dataNodes = self.XMLData.getElementsByTagName("row")
        self.table.setRowCount(len(dataNodes))
        for row, dataNode in enumerate(dataNodes):
            for col, columnHeader in enumerate(self.columnHeaders):
                self.table.setItem(row, col, QtGui.QTableWidgetItem(dataNode.attributes[columnHeader].value))

def main():
    app = QtGui.QApplication(sys.argv)
    window=MainWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

Code Breakdown

I tried to breakdown the code and comment it in such a way that I don't need to explain what each line does. I will try to clear up a few things here though. If you have a question about how something works, leave a comment and I'll respond with an explanation.

class MainWindow(QtGui.QWidget):

This is the definition for the main window's class. I use a QWidget instead of a QMainWindow because I want to choose the layout I'm using.

 

downloadedData = urllib.urlopen('http://api.eve-online.com/account/Characters.xml.aspx?userid=%s&apikey=%s' % (UserID, Key))

There is a more proper way to pass in the parameters. In this case though, I know that the values I'm passing in have no spaces or special characters. I'm also not worried about preventing junk values from being sent. For this reason, I've simplified the download process into one line by just building the entire URL myself. By the way, if you copy and paste the URL into a web browser you'll see the XML that is returned. I recommend you do this to understand what the XML parser is loading. Make sure to replace both %s with the actual values.

 

for row, dataNode in enumerate(dataNodes):
    for col, columnHeader in enumerate(self.columnHeaders):
        self.table.setItem(row, col, QtGui.QTableWidgetItem(dataNode.attributes[columnHeader].value))

Here I'm just iterating through the rows of data and the columns to fill out the table. The interesting part is the use of enumerate. I didn't know about this command until I'd been using Python for over a year. It's a useful shortcut in that it eliminates the need to create and increment your own indexes. In this loop row and col are the indexes for the current position.

I'm working on a more complicated example, but I hope this is enough to get someone who is interested started.

Last Updated on Friday, 03 June 2011 16:50  

Comments  

 
0 #1 JordanX 2017-09-18 07:18
I have noticed that your website needs some fresh articles.
Writing manually takes a lot of time, but there is tool
for this time consuming task, search for: Wrastain's tools for content
Quote
 

Add comment


Security code
Refresh

Main Menu