Вопрос

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

Это было полезно?

Решение 2

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.

Другие советы

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..

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top