Read game options from file

I wrote a function to save window size to a file using pythons functions but I have a problem reading it.I save
to a py file window size as a variable like this -‘resolution-value 800, 600’ and set window size like this - ‘wp.setSize(800, 600)’, but I use arg as an variable to change the resolution and save it to file, thats why I can only use it to read from file.But setSize() accepts only int and thats why I cant set window size like this ‘wp.setSize(arg)’.Maybe theres another way to do it.

resb = DirectOptionMenu(parent = options, scale = (0.1, 1, 0.15), items = ['800, 600', '1024, 768', '1152, 864', '1280, 960', '1280, 1024'], pos = (-1.3, 0, 0.4), command = self.resolution)


def resolution(self, arg):
        cfgFile = open('Config.py').read()
        resvar = 'resolution-value ' +arg

        if arg == '800, 600':
            wp.setSize(800, 600)
            for arg in cfgFile:
                resvar = resvar.replace('arg', '800, 600')
                cfgFile = open('Config.py', 'w')

        if arg == '1024, 768':
            wp.setSize(1024, 768)
            for arg in cfgFile:
                resvar = resvar.replace('arg', '1024, 768')
                cfgFile = open('Config.py', 'w')

        if arg == '1152, 864':
            wp.setSize(1152, 864)
            for arg in cfgFile:
                resvar = resvar.replace('arg', '1152, 864')
                cfgFile = open('Config.py', 'w')

        if arg == '1280, 960':
            wp.setSize(1280, 960)
            for arg in cfgFile:
                resvar = resvar.replace('arg', '1280, 960')
                cfgFile = open('Config.py', 'w')

        if arg == '1280, 1024':
            wp.setSize(1280, 1024)
            for arg in cfgFile:
                resvar = resvar.replace('arg', '1280, 1024')
                cfgFile = open('Config.py', 'w')

        cfgFile.write(resvar)
        cfgFile.close()

I’m honestly not entirely sure of what you’re attempting to do there, but then I am very tired at the moment and thus may be missing something - my apologies if that is the case.

As far as I gather, you want a method that, given a resolution taken from a menu, saves it to file, is that correct? In that case, I suggest str.partition, something like so:

def resolution(self, arg):
   # convert "arg" into a list of strings,
   # using the comma character as a separator.
   # Note that the result is a tuple, and that
   # the second element should be the separator.
   argParts = arg.partition(",") 

   width = argParts[0]
   height = argParts[2]

On the other hand, given the format of your “args”, you might be able to use “eval”, like so:

def resolution(self, arg):
   # To give an example, if "arg" were "1024, 768",
   # the following should amount to:
   # sizeTuple = (1024, 768)
   # Note the inverted commas around the brackets below.
   sizeTuple = eval("("+arg+")")

   # The width should now be "sizeTuple[0]" and
   # the height "sizeTuple[1]"

If I may ask, why are you opening the config file at the start, given that you only seem to want to write to it, and why do you have the code to open the file for writing in each of the if statements, rather than separate from them, given that you seem to write to it regardless of the results of those if-statements?
[/code]

Thanks for reply Ill try your solution.

You have a point there.I wrote it at night so I wasnt very bright.