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

Problems displaying input data into a textbox using tkinter,python

我正在尝试创建一个程序,您可以在其中将数据直接输入 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()

出现问题的原因是行 -

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

您不能为 Text 小部件设置 textvariable

您应该使用 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()

在该行中也添加了换行符,以便每首歌曲都换行。

文本小部件不使用文本变量。要从小部件读取文本,请使用其 get() 方法。我在 infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html

推荐 John Shipman 的 tkinter 介绍