somefreakingguy
somefreakingguy

Reputation: 11

Passing an argument to create a window using wxpython?

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?

Upvotes: 1

Views: 828

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798556

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.

Upvotes: 1

Related Questions