0

我正在尝试在 Python 中构建一个 GUI,其中滚动的文本窗口填充有从组合框中选择的一个或多个值。

例如,如果 Combobox 中的值是 1、2、3、4。如果我单击 1,它会出现在滚动文本窗口中,然后如果我单击 2,它也会添加到滚动文本窗口中,而不会删除先前的选择。

任何人都可以帮忙吗?以下是我到目前为止的代码。

#Library imports
from tkinter import *
from tkinter.ttk import *
from tkinter import scrolledtext

#window geometry
window = Tk()
window.geometry('180x220')
window.title('DFC Selector')

#Window Text
lbl = Label(window, text = "Select DFC", font = ("Arialbold",11))
lbl.grid(column=0, row=0)

# Read values from file
fcode = []
with open('DFCs.txt') as inFile:
    fcode = [line for line in inFile]
    fcode = sorted(fcode)

#Combo box
combo = Combobox(window,width=25)
combo['values']= (fcode)
combo.current(1) 
combo.grid(column=0, row=2)

#Text window
txt = scrolledtext.ScrolledText(window,command=clicked,width=20,height=10)
txt.grid(pady=15,padx=10,column=0,row=3)

#Fault code selection to text window
def clicked():
    txt.configure(text=combo.get)

#Start GUI
window.mainloop()
4

1 回答 1

0

用于txt.insert()在 Text 中插入文本并end作为参数插入到末尾

from tkinter import *
from tkinter.ttk import *
from tkinter import scrolledtext


def clicked(event):
    txt.insert("end", var.get()+'\n')

#window geometry
window = Tk()
window.geometry('180x220')
window.title('DFC Selector')

#Window Text
lbl = Label(window, text = "Select DFC", font = ("Arialbold",11))
lbl.pack()

# Read values from file
fcode = ['Hello', 'Random', 'BSAE']

var = StringVar()
#var.trace('w', insert)

#Combo box
combo = Combobox(window,width=25, textvariable=var, state="readonly")
combo.bind("<<ComboboxSelected>>", clicked)
combo['values']= (fcode)
combo.current(1) 
combo.pack()

#Text window
txt = scrolledtext.ScrolledText(window,width=20,height=10)
txt.pack()

#Fault code selection to text window


#Start GUI
window.mainloop()

于 2021-02-05T14:44:31.753 回答