0

Trying to add text from a function into a textfield, but can't figure out how. Every time when the Start button is clicked it should add text to the textfield.

import Tkinter

class GuiCreate:

    def __init__(self,parent):

        #Textbox
        window  = Frame(width=620, height=50)
        window.place(x=25,y=320)
        vscroll = Scrollbar(window)
        hscroll = Scrollbar(window, orient='horizontal')
        #  create instance variable with "self"
        self.listbox = Text(window, height=10) 
        self.listbox.pack(side=LEFT, fill=X, padx=5, pady=5, expand=1)
        vscroll.config(command=self.listbox.yview, relief=SUNKEN)
        hscroll.config(command=self.listbox.xview, relief=SUNKEN)
        self.listbox.config(yscrollcommand=vscroll.set, relief=SUNKEN)
        self.listbox.config(xscrollcommand=hscroll.set)

        f7 = Frame(width=30, height=20)
        f7.place(x=20,y=260)
        srcButton = Button(f7, text="START", command=self.startProcess)
        srcButton.pack(side='left')


    def startProcess(self):
        textinsert = 'abcdefg'
        self.listbox.insert('end', textinsert)


root = Tk()
root.title("Clipfinder")
root.geometry('650x550+200+100')
root.configure(background = 'gray')
gui=GuiCreate(root)
root.mainloop()

Getting the Error: AttributeError: GuiCreate instance has no attribute 'listbox'

How can I send the string out of a function into the textbox? THX

8
  • Make sure self.listbox = Text(window, height=10) is called before you try and .pack it. Commented Mar 6, 2014 at 16:22
  • I'm confused, window is now not defined... self.listbox = Text(window, height=10) NameError: name 'window' is not defined. Sorry Objects is a little bit new to me Commented Mar 6, 2014 at 16:49
  • updated the post. Now getting NameError: name 'window' is not defined. Commented Mar 6, 2014 at 17:14
  • you had other references to listbox instead of the self.listbox in the scrollbar variables. Commented Mar 6, 2014 at 17:28
  • yes i added the self also at the vscroll.config(command=self.listbox.yview, relief=SUNKEN). It is exactly now as shown here. Commented Mar 6, 2014 at 17:34

2 Answers 2

1
def __init__(self, parent):
    #Textbox
    window  = Frame(width=620, height=50)
    window.place(x=25,y=320)
    vscroll = Scrollbar(window)
    hscroll = Scrollbar(window, orient='horizontal')
    self.listbox = Text(window, height=10)
    self.listbox.pack(side=LEFT, fill=X, padx=5, pady=5, expand=1)
    vscroll.config(command=self.listbox.yview, relief=SUNKEN)
    hscroll.config(command=self.listbox.xview, relief=SUNKEN)
    self.listbox.config(yscrollcommand=vscroll.set, relief=SUNKEN)
    self.listbox.config(xscrollcommand=hscroll.set)

    f7 = Frame(width=30, height=20)
    f7.place(x=20,y=260)
    srcButton = Button(f7, text="START", command=self.startProcess)
    srcButton.pack(side='left')

Forgot to add listbox as an attribute. Otherwise it is just local to the init method..

Sign up to request clarification or add additional context in comments.

Comments

0

Try to create the listbox as an instance variable with self:

from Tkinter import *  

class GuiCreate:

    def __init__(self,parent):

        #Textbox
        window  = Frame(width=620, height=50)
        window.place(x=25,y=320)
        vscroll = Scrollbar(window)
        hscroll = Scrollbar(window, orient='horizontal')
        #  create instance variable with "self"
        self.listbox = Text(window, height=10) 
        self.listbox.pack(side=LEFT, fill=X, padx=5, pady=5, expand=1)
        vscroll.config(command=self.listbox.yview, relief=SUNKEN)
        hscroll.config(command=self.listbox.xview, relief=SUNKEN)
        self.listbox.config(yscrollcommand=vscroll.set, relief=SUNKEN)
        self.listbox.config(xscrollcommand=hscroll.set)

        f7 = Frame(width=30, height=20)
        f7.place(x=20,y=260)
        srcButton = Button(f7, text="START", command=self.startProcess)
        srcButton.pack(side='left')


    def startProcess(self):
        textinsert = 'abcdefg'
        self.listbox.insert('end', textinsert)


root = Tk()
root.title("Clipfinder")
root.geometry('650x550+200+100')
gui = GuiCreate(root)
root.configure(background = 'gray')
root.mainloop()

You can learn more about classes and object-orient programming in python here, a few paragraphs down it touches upon using self.

6 Comments

then i get : self.listbox.pack(side=LEFT, fill=X, padx=5, pady=5, expand=1) NameError: name 'self' is not defined
seems like not... would: self.listbox = Text(parent) do this? sorry, don't know it...
@rainer the window variable is local to your init which means you won't be able to reference it in other class methods.
window itself is not referenced else, or is that connected because its the frame of the textbox?
@rainer Its tough to say without seeing more of the code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.