3

I am trying to create a program where you input data directly into the tkinter interface and then displaying the data in a textbox. However, I am getting the following error

line 2122, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-textvariable"

from tkinter import *
from tkinter import ttk

def textMethod(*args):
    copytext = textinput1.get()
    textoutput1.set(copytext)

root = Tk()

root.title("Naynt Music Database")
frame = ttk.Frame(root,padding = "12 12 12 12")
frame.grid(column=0,row=1,sticky="N,S,E,W")
frame.columnconfigure(0,weight=1)
frame.rowconfigure(0,weight=1)

textinput1 = StringVar()
textoutput1 = StringVar()

text_entry = ttk.Entry(frame,width=20,textvariable = textinput1)
text_entry.grid(row=1,column=0,sticky=(W,E))
ttk.Button(frame,text="Add songname",command=textMethod).grid(column=1,row=1)

songListFrame = Frame(frame, bd=2, relief=SUNKEN)

frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)

xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
xscrollbar.grid(row=3, column=0, sticky=E+W)

yscrollbar = Scrollbar(frame)
yscrollbar.grid(row=2, column=1, sticky=N+S)

songList = Text(frame, wrap=NONE, bd=0,
            xscrollcommand=xscrollbar.set,
            yscrollcommand=yscrollbar.set,
            textvariable=textoutput1)

songList.grid(row=2, column=0, sticky=N+S+E+W)

xscrollbar.config(command=songList.xview)
yscrollbar.config(command=songList.yview)

frame.pack()


root.mainloop()
Cœur
  • 37,241
  • 25
  • 195
  • 267
naynt
  • 53
  • 2
  • 4

2 Answers2

2

The issue occurs because of the lines -

songList = Text(frame, wrap=NONE, bd=0,
        xscrollcommand=xscrollbar.set,
        yscrollcommand=yscrollbar.set,
        textvariable=textoutput1)

You cannot set textvariable for Text widget .

You should rather use Text.insert() method with END to insert your songs to the end in the Text widget. Example -

from tkinter import *
from tkinter import ttk

def textMethod(*args):
    copytext = textinput1.get()
    songList.insert(END, copytext + '\n')

root = Tk()

root.title("Naynt Music Database")
frame = ttk.Frame(root,padding = "12 12 12 12")
frame.grid(column=0,row=1,sticky="N,S,E,W")
frame.columnconfigure(0,weight=1)
frame.rowconfigure(0,weight=1)

textinput1 = StringVar()
textoutput1 = StringVar()

text_entry = ttk.Entry(frame,width=20,textvariable = textinput1)
text_entry.grid(row=1,column=0,sticky=(W,E))
ttk.Button(frame,text="Add songname",command=textMethod).grid(column=1,row=1)

songListFrame = Frame(frame, bd=2, relief=SUNKEN)

frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)

xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
xscrollbar.grid(row=3, column=0, sticky=E+W)

yscrollbar = Scrollbar(frame)
yscrollbar.grid(row=2, column=1, sticky=N+S)

songList = Text(frame, wrap=NONE, bd=0,
            xscrollcommand=xscrollbar.set,
            yscrollcommand=yscrollbar.set)

songList.grid(row=2, column=0, sticky=N+S+E+W)

xscrollbar.config(command=songList.xview)
yscrollbar.config(command=songList.yview)

frame.pack()


root.mainloop()

Added a newline to the line as well, so that each song comes in a new line .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1

The Text widget doesn't use a text variable. To read the text from the widget, use its get() method. I recommend John Shipman's intro to tkinter at infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html

saulspatz
  • 5,011
  • 5
  • 36
  • 47
  • And an example: https://web.archive.org/web/20230131025032/https://www.delftstack.com/howto/python-tkinter/how-to-get-the-input-from-tkinter-text-box/ – Lod Aug 21 '23 at 21:10