代码之家  ›  专栏  ›  技术社区  ›  AhmedWas

如何重新配置Tkinter滚动文本

  •  1
  • AhmedWas  · 技术社区  · 6 年前

    在这种情况下,我需要将scrolledText设置为只读,这样用户只能通过按钮向它添加值,如下所示:

    from tkinter import *
    from tkinter import scrolledtext
    
    startingWin = Tk()
    
    myEntry = Entry(startingWin, font=("Courier",25, "bold"), width=15)    
    myScrolledText = scrolledtext.ScrolledText(startingWin, font=("Courier",25, "bold"),width=15, height=3, state='disabled')
    
    def addEntryContentToScrolledText(entry, scrolledText):
        entryValue = str((entry.get()).strip())
        scrolledtext.config(state='normal')
        if entryValue not in str(scrolledText.get('1.0', 'end-1c')) and entryValue != "":
            scrolledText.insert(INSERT, (entryValue + "\n"))
        scrolledtext.config(state='disabled')    
    
    myAddButton = Button(startingWin, text="Add")
    myAddButton.config(command=lambda: addEntryContentToScrolledText(myEntry, myScrolledText))
    
    myScrolledText.grid(row=0, column=1)
    myEntry.grid(row=0, column=0)
    myAddButton.grid(row=1, column=0)
    startingWin.mainloop()
    

    但是,我得到了这个错误 AttributeError: module 'tkinter.scrolledtext' has no attribute 'config'

    如何重新配置滚动文本?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Reblochon Masque    6 年前

    你的小工具名是 myScrolledText ,不是 scrolledtext :(scrolledtext是导入的模块名)

    from tkinter import *
    from tkinter import scrolledtext
    
    
    startingWin = Tk()
    
    myEntry = Entry(startingWin, font=("Courier",25, "bold"), width=15)    
    myScrolledText = scrolledtext.ScrolledText(startingWin, font=("Courier",25, "bold"),width=15, height=3, state='disabled')
    
    def addEntryContentToScrolledText(entry, textwidget):
        entryValue = str((entry.get()).strip())
        textwidget.configure(state='normal')
        if entryValue not in str(textwidget.get('1.0', 'end-1c')) and entryValue != "":
            textwidget.insert(INSERT, (entryValue + "\n"))
        textwidget.configure(state='disabled') 
    
    
    myAddButton = Button(startingWin, text="Add", command=lambda: addEntryContentToScrolledText(myEntry, myScrolledText))
    
    myScrolledText.grid(row=0, column=1)
    myEntry.grid(row=0, column=0)
    myAddButton.grid(row=1, column=0)
    startingWin.mainloop()