简体   繁体   English

使用 Combobox 值填充滚动文本 window (python)

[英]populate scrolled text window with Combobox value (python)

I'm trying to build a GUI in Python where a scrolled text window is populated with one or more values selected from a Combobox.我正在尝试在 Python 中构建一个 GUI,其中滚动文本 window 填充了从 Combobox 中选择的一个或多个值。

eg if the values in the Combobox are 1, 2, 3, 4. if I click 1 it appears in the scrolled text window, then if I clicked 2 that is also added to the scrolled text window without erasing the previous selection.例如,如果 Combobox 中的值是 1、2、3、4。如果我单击 1,它会出现在滚动文本 window 中,然后如果我单击 2,它也会添加到滚动文本 Z05B8C745CBD96FBF2F4Z4C1 中而没有时代。

Can anyone help?任何人都可以帮忙吗? Below is the code I have so far.以下是我到目前为止的代码。

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

use txt.insert() to insert text into Text and use end as a parameter to insert at the end使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM