2015-08-13 5 views
1

J'essaie de créer un programme dans lequel vous entrez des données directement dans l'interface tkinter, puis affichez les données dans une zone de texte. Cependant, je reçois l'erreur suivanteProblèmes d'affichage des données d'entrée dans une zone de texte à l'aide de tkinter, python

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() 

Répondre

1

Le problème se produit à cause des lignes -

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

Vous ne pouvez pas définir textvariable pour Text un widget.

Vous devriez plutôt utiliser la méthode Text.insert() avec END pour insérer vos chansons à la fin dans le widget Text. Exemple -

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() 

Ajout d'une nouvelle ligne à la ligne, de sorte que chaque chanson apparaisse dans une nouvelle ligne.

+0

Merci beaucoup, c'était extrêmement utile! – naynt

+0

Si la réponse vous a été utile, je voudrais vous conseiller d'accepter la réponse (en cliquant sur la coche à gauche de la réponse), ce serait utile pour la communauté. –

0

Le widget Texte n'utilise pas de variable de texte. Pour lire le texte du widget, utilisez sa méthode get(). Je recommande l'introduction de John Shipman à tkinter à infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html