Answer the question
In order to leave comments, you need to log in
How to call an object from another object?
please help me fix the script.
import tkinter
import tkinter.messagebox
class Operations():
def new(self, parent):
global workArea
if workArea == True:
tkinter.messagebox.showwarning('Warning', 'New document is already open')
else:
self.new = WorkArea(parent)
workArea = True
def openFile(self):
global workArea
if workArea == True:
tkinter.messagebox.showwarning('Warning', 'Close the open document')
else:
try:
nameOpenFile = tkinter.filedialog.askopenfilename(title = 'Open textPad file', filetypes=[("textPad files", "*.tpd")])
except Exception:
pass
else:
try:
pointerFileOpened = open(nameOpenFile, 'rt')
except Exception:
tkinter.messagebox.showerror('Error', 'Open file error')
else:
self.new(root)
self.readFile(pointerFileOpened)
finally:
pointerFileOpened.close()
def readFile(self, pointerFileOpened):
while True:
line = pointerFileOpened.readline()
if len(line) == 0:
break
else:
self.new.insert(tkinter.END, line) #this is problem line
return
class WorkArea():
def __init__(self, parent):
parent.title("My default workarea")
workArea = tkinter.Text(parent)
workArea.pack(expand = 'yes', fill = 'both')
class ToolBar(Operations, tkinter.Frame):
def __init__(self, parent):
tkinter.Frame.__init__(self, parent)
self.parent = parent
self.makeToolBar()
def makeToolBar(self):
frame = tkinter.Frame(self.parent)
frame.pack(side = 'top', fill = 'x')
tool1 = tkinter.Button(frame, text = 'New', command = lambda: self.new(root))
tool1.pack(side = 'left')
tool2 = tkinter.Button(frame, text = 'Open', command = lambda: self.openFile())
tool2.pack(side = 'left')
workArea = False
root = tkinter.Tk()
root.geometry('900x500+200+100')
toolBar = ToolBar(root)
root.mainloop()
File “C:\Python33\projects\TEXTPADS\textPad_OOP\q.py”, line 32, in openFile
self.readFile(pointerFileOpened)
File “C:\Python33\projects\TEXTPADS\textPad_OOP\q.py”, line 42, in readFile
self.new.insert(tkinter.END, line) #this is problem line
AttributeError: ‘WorkArea’ object has no attribute ‘insert’
Answer the question
In order to leave comments, you need to log in
You seem to have just started learning programming. Firstly, it is worth translating the error message into Russian and trying to understand what exactly the interpreter does not like. Second, use Google before asking questions. And third, don't paste a piece of code and expect someone to read your code and try to figure it out.
Your mistake is that you are trying to call a method insert
on the "WorkArea" class. But this class has no such method. It only has a method __init__
. Apparently the insert method of the class should be called tkinter.Text
?
I fixed the WorkArea class from your example:
https://gist.github.com/anonymous/8851536
I can't check because I don't have tkinter installed.
Notes:
workArea
is bad practiceworkArea
almost the same as the class name WorkArea
- bad practice, it's easy to mix them up and get a lot of funny error messagesself.new = ...
replaces the "new" method with an object. You should not name the method and the field by the same name.tkinter.Frame
.Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question