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?

2
  • Your traceback does not match your code. Please get your story straight. Commented May 17, 2010 at 3:57
  • thanks for the catch, i must have pasted the wrong one. Commented May 17, 2010 at 4:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
Yes, but don't forget to handle the exception that will result if it can't be converted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.