views:

41

answers:

1

I am trying to learn how to make a GUI in python! Following an online tutorial, I found that the following code 'works' in creating an empty window:

import wx
from sys import argv

class bucky(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Frame aka window', size=(300, 200))

if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=bucky(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

That gives me a window, which is great. However, what if I want to get an argument passed onto the program to determine the window size? I thought something like this ought to do the trick:

import wx
from sys import argv

script, x, y = argv

class mywindow(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Frame aka window', size=(x, y))

if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=mywindow(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

But, alas, that does not work! I keep getting the following error:

Traceback <most recent call last):
    File "C:\DOCUME~1\OWNER\DESKTOP\pw2.py", line 12, in <module>
        frame=mywindow(parent=None, id=-1)
    File "C:\DOCUME~1\OWNER\DESKTOP\pw2.py", line 8, in __init__
        wx.Frame.__init))(self.parent, id, 'Frame aka window', size=(x, y))
    File "C:\Python26\lib\site-packagaes\wx-2.8-msw-unicode\wx\_widows.py", line 5
05, in __init__
    _windows_.Frame_swiginit(self, _windows_.new_Frame(*args, **kwargs))
TypeError: Expected a 2-tuple of integers or a wxSize object.

How do I create a window depending on user input as I've tried to above? Thanks for the help!

+1  A: 

The elements of sys.argv are strings; you need to convert them to integers before using them. Consider passing them to the constructor though, instead of relying on global state.

Ignacio Vazquez-Abrams
Ignacio,Thank you for your help! That worked marvelously. Do you suggest that the most elegant and conventional method for me to convert the string to an integer is to use the int() function?
somefreakingguy
Yes, but don't forget to handle the exception that will result if it can't be converted.
Ignacio Vazquez-Abrams