Z
Z
zlodiak2014-02-06 19:17:47
Python
zlodiak, 2014-02-06 19:17:47

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()

The problem is that when the user presses the “open” button and selects a file to open, the contents of the file are not displayed on the screen, but the following error message is displayed:
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’

if possible, please help me rewrite the WorkArea class in the right way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sheknitrtch, 2014-02-06
@zlodiak

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 inserton 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:

  1. Using a global boolean variable workAreais bad practice
  2. The variable name is workAreaalmost the same as the class name WorkArea- bad practice, it's easy to mix them up and get a lot of funny error messages
  3. The expression self.new = ...replaces the "new" method with an object. You should not name the method and the field by the same name.
  4. It is not clear why the ToolBar class inherits the other two classes. It seems to me that it is possible to combine the "Operations" and "TooBar" classes into one that would inherit from tkinter.Frame.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question