TypeError: __init__() takes at least 3 arguments (1 given)
You set up the init to accept 3 values and then you don't pass it anything. Because this frame will be a top level window, you can pass it a parent of None and some kind of title string:
frame = MainWindow(None, "test")
The next issue is that you try to use both initialization routines: the super and the regular. You can only use one or the other, but not both! I left the super one intact because it's shorter and commented out the latter. I also change self.filename to an empty string as "filename" is obviously not defined and I commented out the calls to build the other widgets as the code is incomplete.
import wx
import os.path
class MainWindow(wx.Frame):
def __init__(self, parent, title, *args, **kwargs):
super(MainWindow,self).__init__(parent, title = title, size = (800,600))
#wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 800,608 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
grid = wx.GridSizer( 2, 2, 0, 0 )
self.filename = ""
self.dirname = '.'
#self.CreateInteriorWindowComponents()
#self.CreateExteriorWindowComponents()
def CreateInteriorWindowComponents(self):
staticbox = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"Enter text" ), wx.VERTICAL )
staticbox.Add( self.m_textCtrl1, 0, wx.ALL|wx.EXPAND, 5 )
self.m_textCtrl1 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( -1,250 ), wx.TE_MULTILINE)
self.m_textCtrl1.SetMaxLength(10000)
self.Submit = wx.Button( self, wx.ID_ANY, u"Submit", wx.DefaultPosition, wx.DefaultSize, 0 )
staticbox.Add( self.Submit, 0, wx.ALL, 5 )
def CreateExteriorWindowComponents(self):
''' Create "exterior" window components, such as menu and status
bar. '''
self.CreateMenu()
self.CreateStatusBar()
self.SetTitle()
def CreateMenu(self):
fileMenu = wx.Menu()
for id, label, helpText, handler in \
[(wx.ID_ABOUT, '&About', 'Storyteller 1.0 -',
self.OnAbout),
(wx.ID_OPEN, '&Open', 'Open a new file', self.OnOpen),
(wx.ID_SAVE, '&Save', 'Save the current file', self.OnSave),
(wx.ID_SAVEAS, 'Save &As', 'Save the file under a different name',
self.OnSaveAs),
(None, None, None, None),
(wx.ID_EXIT, 'E&xit', 'Terminate the program', self.OnExit)]:
if id == None:
fileMenu.AppendSeparator()
else:
item = fileMenu.Append(id, label, helpText)
self.Bind(wx.EVT_MENU, handler, item)
menuBar = wx.MenuBar()
menuBar.Append(fileMenu, '&File') # Add the fileMenu to the MenuBar
self.SetMenuBar(menuBar) # Add the menuBar to the Frame
def SetTitle(self):
# MainWindow.SetTitle overrides wx.Frame.SetTitle, so we have to
# call it using super:
super(MainWindow, self).SetTitle('Editor %s'%self.filename)
# Helper methods:
def defaultFileDialogOptions(self):
''' Return a dictionary with file dialog options that can be
used in both the save file dialog as well as in the open
file dialog. '''
return dict(message='Choose a file', defaultDir=self.dirname,
wildcard='*.*')
def askUserForFilename(self, **dialogOptions):
dialog = wx.FileDialog(self, **dialogOptions)
if dialog.ShowModal() == wx.ID_OK:
userProvidedFilename = True
self.filename = dialog.GetFilename()
self.dirname = dialog.GetDirectory()
self.SetTitle() # Update the window title with the new filename
else:
userProvidedFilename = False
dialog.Destroy()
return userProvidedFilename
# Event handlers:
def OnAbout(self, event):
dialog = wx.MessageDialog(self, 'A sample editor\n'
'in wxPython', 'About Sample Editor', wx.OK)
dialog.ShowModal()
dialog.Destroy()
def OnExit(self, event):
self.Close() # Close the main window.
def OnSave(self, event):
textfile = open(os.path.join(self.dirname, self.filename), 'w')
textfile.write(self.control.GetValue())
textfile.close()
def OnOpen(self, event):
if self.askUserForFilename(style=wx.OPEN,
**self.defaultFileDialogOptions()):
textfile = open(os.path.join(self.dirname, self.filename), 'r')
self.control.SetValue(textfile.read())
textfile.close()
def OnSaveAs(self, event):
if self.askUserForFilename(defaultFile=self.filename, style=wx.SAVE,
**self.defaultFileDialogOptions()):
self.OnSave(event)
app = wx.App()
frame = MainWindow(None, "test")
frame.Show()
app.MainLoop()
boltthrower
Mostly writing Python, SQL & Java. Data Science | Software | Music Information Retrieval
Updated on June 04, 2022Comments
-
boltthrower almost 2 years
This is my code for a GUI :
import wx import os.path class MainWindow(wx.Frame): #def __init__(self, filename=''): #super(MainWindow, self).__init__(None, size=(800,600)) def __init__(self, parent, title, *args, **kwargs): super(MainWindow,self).__init__(parent, title = title, size = (800,600)) wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 800,608 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL ) self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize ) grid = wx.GridSizer( 2, 2, 0, 0 ) self.filename = filename self.dirname = '.' self.CreateInteriorWindowComponents() self.CreateExteriorWindowComponents() def CreateInteriorWindowComponents(self): staticbox = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"Enter text" ), wx.VERTICAL ) staticbox.Add( self.m_textCtrl1, 0, wx.ALL|wx.EXPAND, 5 ) self.m_textCtrl1 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( -1,250 ), wx.TE_MULTILINE) self.m_textCtrl1.SetMaxLength(10000) self.Submit = wx.Button( self, wx.ID_ANY, u"Submit", wx.DefaultPosition, wx.DefaultSize, 0 ) staticbox.Add( self.Submit, 0, wx.ALL, 5 ) def CreateExteriorWindowComponents(self): ''' Create "exterior" window components, such as menu and status bar. ''' self.CreateMenu() self.CreateStatusBar() self.SetTitle() def CreateMenu(self): fileMenu = wx.Menu() for id, label, helpText, handler in \ [(wx.ID_ABOUT, '&About', 'Storyteller 1.0 -', self.OnAbout), (wx.ID_OPEN, '&Open', 'Open a new file', self.OnOpen), (wx.ID_SAVE, '&Save', 'Save the current file', self.OnSave), (wx.ID_SAVEAS, 'Save &As', 'Save the file under a different name', self.OnSaveAs), (None, None, None, None), (wx.ID_EXIT, 'E&xit', 'Terminate the program', self.OnExit)]: if id == None: fileMenu.AppendSeparator() else: item = fileMenu.Append(id, label, helpText) self.Bind(wx.EVT_MENU, handler, item) menuBar = wx.MenuBar() menuBar.Append(fileMenu, '&File') # Add the fileMenu to the MenuBar self.SetMenuBar(menuBar) # Add the menuBar to the Frame def SetTitle(self): # MainWindow.SetTitle overrides wx.Frame.SetTitle, so we have to # call it using super: super(MainWindow, self).SetTitle('Editor %s'%self.filename) # Helper methods: def defaultFileDialogOptions(self): ''' Return a dictionary with file dialog options that can be used in both the save file dialog as well as in the open file dialog. ''' return dict(message='Choose a file', defaultDir=self.dirname, wildcard='*.*') def askUserForFilename(self, **dialogOptions): dialog = wx.FileDialog(self, **dialogOptions) if dialog.ShowModal() == wx.ID_OK: userProvidedFilename = True self.filename = dialog.GetFilename() self.dirname = dialog.GetDirectory() self.SetTitle() # Update the window title with the new filename else: userProvidedFilename = False dialog.Destroy() return userProvidedFilename # Event handlers: def OnAbout(self, event): dialog = wx.MessageDialog(self, 'A sample editor\n' 'in wxPython', 'About Sample Editor', wx.OK) dialog.ShowModal() dialog.Destroy() def OnExit(self, event): self.Close() # Close the main window. def OnSave(self, event): textfile = open(os.path.join(self.dirname, self.filename), 'w') textfile.write(self.control.GetValue()) textfile.close() def OnOpen(self, event): if self.askUserForFilename(style=wx.OPEN, **self.defaultFileDialogOptions()): textfile = open(os.path.join(self.dirname, self.filename), 'r') self.control.SetValue(textfile.read()) textfile.close() def OnSaveAs(self, event): if self.askUserForFilename(defaultFile=self.filename, style=wx.SAVE, **self.defaultFileDialogOptions()): self.OnSave(event) app = wx.App() frame = MainWindow() frame.Show() app.MainLoop()
I'm getting the
TypeError:: __init__() takes at least 3 arguments (1 given)
on the 3rd last line
frame = MainWindow()
How do I make sure the parameter list matches? I think I'm a little confused on passing self, parent or something.
Help please!
EDIT: @mhlester : I made the change you suggested but now I have a different error :
TypeError: in method 'new_Frame', expected argument 1 of type 'wxWindow *'
Infact this is what the complete text looks like :
Traceback (most recent call last): File "C:\Users\BT\Desktop\BEt\gui2.py", line 115, in <module> frame = MainWindow(app,'Storyteller') File "C:\Users\BT\Desktop\BE\gui2.py", line 9, in __init__ super(MainWindow,self).__init__(parent, title = title, size = (800,600)) File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_windows.py", line 580, in __init__ _windows_.Frame_swiginit(self,_windows_.new_Frame(*args, **kwargs)) TypeError: in method 'new_Frame', expected argument 1 of type 'wxWindow *'
-
mhlester over 10 years@Bhavika it looks like I aws wrong about what
parent
needs to be. It says it needs to be anotherwxWindow
. This is all I know, sorry. Maybe someone else has the answer to this new problem. -
boltthrower over 10 yearsYes I tried what you said, and it didnt work. See the solution below!
-
mhlester over 10 years@Bhavika, Excellent! glad someone knew what they were doing.