Trouble with dirname (classes/objects?)

Hi,

I am quite new to wxPython and am trying to create a simple UI in which I can read files. I am trying to use ‘dirname’ but when I run the code, I get this error: ‘MainWindow’ object has no attribute ‘dirname’. (I have adapted this code based of an example from the getting started wxPython wiki).

This is my code:

import wx
import os

class MainWindow(wx.Frame):
	def __init__(self, parent, title):
		self.dirname== ''
		wx.Frame.__init__(self, parent, title=title, size=(200, -1))
		self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
		self.CreateStatusBar()

		filemenu = wx.Mennu()
		menuOpen = filemenu.Append(wx.ID_OPEN, "open file", "open a file to edit")

		menuBar = wx.MenuBar()
		menuBar.Append(filemenu, "File") #adds 'filemenu' to the MenuBar
		self.SetMenuBar(menuBar) #adds MenuBar to the Frame content

		#attaching clicking on file to opening a file
		self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)

	def OnOpen(self,e):
		dlg= wx.FileDialog(self, "choose a file", self.dirname, "", "*.*", wx.FD_OPEN)
		if dlg.ShowModal() == wx.ID_OK:
			self.filename = dlg.GetFilename()
			self.dirname =dlg.GetDirectory()
			f = open(os.path.join(self.dirname, self.filename), 'r')
			f.close()
		dlg.Destroy()


app = wx.App(False)
frame = MainWindow(None, "sample editor")
app.MainLoop()

I am not sure if this is related to classes/objects/functions but any help would be greatly appeciated!

Thanks!

The error message comes up with the self.dirname==’’ line

== is a comparison, not an assignment.
Remove one =

1 Like

What ist the “==” to mean ?

Best,
Karsten