1

I have a text area created in initUI(self) function and would like to add to its contents in another one. When I initialize the text area as global on top of the class, the area is created in an another window, which is not what I want. I have seen questions related to the global variables but not something like this.

from Tkinter import*
textArea = Text() # creates another window
class test(Frame):
    def __init__(self, parent):
        Frame.__init__(self,parent)
        self.initUI()

    def initUI(self):
        mainFrame = Frame(self, parent)
        textArea  = Text(maınFrame, height=10, width=10)
        textArea.pack(side=BOTTOM)
        textArea.insert(INSERT, "abc")
    def changeText():
        global textArea
        textArea.insert(INSERT, "def")

thanks

Cugomastik
  • 911
  • 13
  • 22
  • You have two variables with the same name, one variable of instance and one global, it's a bad issue. – Trimax Jul 17 '14 at 11:22
  • I am using global in the function as suggested in many answers. "http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them" – Cugomastik Jul 17 '14 at 11:25
  • The example with the same variable names is only for explanation purpose. You should avoid doing so. – Trimax Jul 17 '14 at 11:46

1 Answers1

1

global isn't necessary when you need a variable to be shared between two methods belonging to the same class. You can just attach the variable you need to self.

from Tkinter import*
class test(Frame):
    def __init__(self, parent):
        Frame.__init__(self,parent)
        self.initUI()

    def initUI(self):
        mainFrame = Frame(self, parent)
        self.textArea  = Text(maınFrame, height=10, width=10)
        self.textArea.pack(side=BOTTOM)
        self.textArea.insert(INSERT, "abc")
    def changeText(self):
        self.textArea.insert(INSERT, "def")
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Hi Kevin, I don't have self as a parameter in changeText(). I wrote the code above to illustrate the problem. Do you know why I am having an error as "changeText takes exactly 1 argument (0 given)" when I put self in it and dont get any for other functions eventhough they have self as a parameter ? – Cugomastik Jul 17 '14 at 11:50
  • 1
    All non-static class methods should have `self` as their first paramter. How are you calling `changeText`? Like `myTestFrameInstance.changeText()`, right? – Kevin Jul 17 '14 at 11:51
  • its called when a button is pressed. button = Button(mainFrame, text="connect", command = changeText) in initUI() – Cugomastik Jul 17 '14 at 11:53
  • I tried that and had the error "class instance has no attribute 'changeText' .. thats insteresting because I don't get any error for the other function that stops mainloop – Cugomastik Jul 17 '14 at 11:56
  • Ok, I think I know why. Since I am using IDLE in Rpi as the compiler, I am not having indentation errors or anything else. the error I am having is probably due to spaces. Thanks for the help Kevin. – Cugomastik Jul 17 '14 at 12:15