Question

A preface: I'm a beginner at Python. I've tried guides for learning but I am awful at learning like that, so I'm trying to make a super simple update checker to get started that I slowly build upon. I've grabbed some code I found on here and modified it a bit, and alas, it doesn't work. It reads the local and external .txt files and prints their output (just to check that it's reading them correctly). It then fails at the if/elif/elif/else statement thing in some way, so some help there would be great!

It's currently telling me "NameError: global name 'i' is not defined" however I've gone through several different errors at this point and am really just looking for a solution, and from there I can work backwards. Thanks!

import Tkinter
import urllib
import time

print "test"

#previously self within the brackets
def updateCheck():
    update = False

    updateWindow = Tkinter.Toplevel()
    updateWindow.title(string="Update Checker")
    updateWindow.resizable(False, False)

    #gets local version (file currently says "1.0")
    localSource = open('version.txt', 'r')
    localContents = localSource.read()
    print "local version = " + localContents

    #gets server version (file currently says "1.1")
    serverSource = urllib.urlopen("http://raw.github.com/SamHH/ccr-version/master/version.txt")
    serverContents = serverSource.read()
    print "server version = " + serverContents

    #checks for updates by comparing the above two -- doesn't work
    if serverContents[i] > localContents[i]:
        dataLabel = Tkinter.Label(updateWindow,text="\n\nThere is an update available.\n\n")
        dataLabel.pack()
        #need a way of opening a .url file in the same folder here, if possible
    elif serverContents[i] < localContents[i]:
        dataLabel = Tkinter.Label(updateWindow,text="\n\nYour installation appears to be broken.\n\n")
        dataLabel.pack()
        #need a way of opening a .url file in the same folder here, if possible, again
    elif serverContents[i] == localContents[i]:
        versionLabel = Tkinter.Label(updateWindow,text="\n\nYou are already running the most up to date version.\n\n")
        versionLabel.pack()
        #need a way of opening a .exe file in the same folder this time, if possible
    else:
        versionLabel = Tkinter.Label(updateWindow,text="\n\nYour install is corrupted. Doh!\n\n")
        versionLabel.pack()

updateCheck()
Was it helpful?

Solution

if serverContents[i] > localContents[i]:

Notice that you never initialized i to a default value. It is looking up in your code to see if you had defined and set it outside the function (which you did not).

Put in a loop

for i in range(len(serverContents)):

You should also check that both lists are the same size or you will get an error when you try to index past the end.

Note that this assumes that the serverContents and localContents are both lists each of whose elements is a value to be compared. If the contents are text strings. then you will be looping over each character in the string. If that is what you want, you do not need to do it

f = '1.1a'
g = '1.1a'

f == g # shows True
f is g # shows False

This will mean that '1.1a' and '01.1a' will show different

However, this will allow for the case where version number is not totally numeric, which is a requirement if you use float(serverContents).

OTHER TIPS

If both local and remove 'files' contain just a float, read one line from each and turn that into a float() so you can compare:

try:
    localSource = open('version.txt', 'r')
    localContents = float(localSource.readline())
except (IOError, ValueError):
    versionLabel = Tkinter.Label(updateWindow,text="\n\nYour install is corrupted. Doh!\n\n")
    versionLabel.pack()
    return

serverSource = urllib.urlopen("http://raw.github.com/SamHH/ccr-version/master/version.txt")
serverContents = float(serverSource.readline())

You then use the localContents and serverContents names to compare:

if serverContents > localContents:
    # etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top