Python tkinter在函数的TextBox中插入字符串

Python tkinter在函数的TextBox中插入字符串,python,function,listbox,Python,Function,Listbox,试图将函数中的文本添加到文本字段中,但不知道如何添加。 每次单击“开始”按钮时,都应将文本添加到文本字段 import Tkinter class GuiCreate: def __init__(self,parent): #Textbox window = Frame(width=620, height=50) window.place(x=25,y=320) vscroll = Scrollbar(window)

试图将函数中的文本添加到文本字段中,但不知道如何添加。 每次单击“开始”按钮时,都应将文本添加到文本字段

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()
获取错误:AttributeError:GuiCreate实例没有属性“listbox”

如何将函数中的字符串发送到文本框中?
THX

尝试使用
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()
您可以在下面的几段中了解有关python中的类和面向对象编程的更多信息 涉及使用
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')

忘记将listbox添加为属性。否则它只是init方法的本地..

然后我得到:self.listbox.pack(side=LEFT,fill=X,padx=5,pady=5,expand=1)name错误:名称“self”没有定义似乎没有。。。self.listbox=Text(父项)会这样做吗?抱歉,我不知道…@rainer
window
变量是init的本地变量,这意味着您将无法在其他类方法中引用它。window本身没有被其他类方法引用,或者它是被连接的,因为它是文本框的框架吗?@rainer很难说没有看到更多的代码。请确保
self.listbox=Text(window,height=10)
在您尝试和
.pack
之前被调用。我很困惑,window现在没有定义…self.listbox=Text(window,height=10)NameError:name'window'未定义。抱歉,对象对我来说有点新。更新了帖子。现在获取NameError:name'window'未定义。您对滚动条变量中的
listbox
有其他引用,而不是
self.listbox
。是的,我还在vscroll.config中添加了self(command=self.listbox.yview,relief=SUNKEN)。现在它完全如图所示。