使用tkinter,python将输入数据显示到文本框中时出现问题

时间:2015-08-13 03:58:23

标签: python input tkinter interface tkinter-entry

我正在尝试创建一个程序,您可以将数据直接输入到tkinter界面,然后在文本框中显示数据。但是,我收到以下错误

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

2 个答案:

答案 0 :(得分:1)

由于行 -

而出现问题
songList = Text(frame, wrap=NONE, bd=0,
        xscrollcommand=xscrollbar.set,
        yscrollcommand=yscrollbar.set,
        textvariable=textoutput1)

您无法为textvariable小部件设置Text

您应该使用Text.insert()方法和END将歌曲插入Text窗口小部件的末尾。示例 -

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

也为该行添加了一个换行符,以便每首歌曲都换一行。

答案 1 :(得分:0)

“文本”小部件不使用文本变量。要从窗口小部件中读取文本,请使用其get()方法。我推荐John Shipman在infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html上介绍tkinter