gpt4 book ai didi

python - 返回 tkinter 列表框中选定值的列表

转载 作者:行者123 更新时间:2023-12-01 09:31:27 24 4
gpt4 key购买 nike

我正在尝试创建一个 tkinter 列表框并将用户选择的选项打印为列表。到目前为止我已经:

from tkinter import *
from tkinter import ttk

main = Tk()
main.title('Test GUI')
main.geometry('700x500')

nb = ttk.Notebook(main)
nb.grid(row=1, column=0, columnspan=50, rowspan=49, sticky='NESW')

page1 = ttk.Frame(nb)
nb.add(page1, text='Search Terms')

p1_IncludeLanguage = Listbox(page1)
p1_IncludeLanguage.configure(selectmode=MULTIPLE, width=9, height=5)
p1_IncludeLanguage.grid(row=0, column=0)

for lang in ["English", "German", "French", "Italian", "Portuguese"]:
p1_IncludeLanguage.insert(END, lang)

main.mainloop()

我想打印列表框中选定的选项,我尝试首先使用 p1_IncludeLanguage.curselection() 获取它们的行号,但这会导致 TclError 错误: 无效的命令名称“.!notebook.!frame.!listbox”

还有其他方法可以实现这一点吗?为什么 .curselection() 不起作用?

最佳答案

这对你来说有什么作用?我添加了一个按钮,用于获取所选项目的列表。

from tkinter import *
from tkinter import ttk

def get_selection():
items = [languages[int(item)] for item in p1_IncludeLanguage.curselection()]
print(items)

main = Tk()
main.title('Test GUI')
main.geometry('700x500')

nb = ttk.Notebook(main)
nb.grid(row=1, column=0, columnspan=50, rowspan=49, sticky='NESW')

page1 = ttk.Frame(nb)
nb.add(page1, text='Search Terms')

p1_IncludeLanguage = Listbox(page1)
p1_IncludeLanguage.configure(selectmode=MULTIPLE, width=9, height=5)
p1_IncludeLanguage.grid(row=0, column=0)

btnGet = Button(page1,text="Get Selection",command=get_selection)
btnGet.grid()

languages = ["English", "German", "French", "Italian", "Portuguese"]

for lang in languages:
p1_IncludeLanguage.insert(END, lang)



main.mainloop()

关于python - 返回 tkinter 列表框中选定值的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49932566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com