Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tkinter Python列表框_Python_User Interface_Tkinter_Listbox - Fatal编程技术网

Tkinter Python列表框

Tkinter Python列表框,python,user-interface,tkinter,listbox,Python,User Interface,Tkinter,Listbox,我是一个新的python用户。我习惯于在matlab上编程。 我试图用Tkinter pack制作一个简单的GUI,但我遇到了一些问题。我已经阅读并搜索了我想要的东西,但我无法开发它 我试图做的是创建一个列表框,当我选择一个(或多个)选项时,索引将作为变量(数组或向量)返回(并存储),该变量可用于索引另一个数组 我得到的最好结果是一个列表框,其中打印了索引,但没有作为变量存储(至少它没有显示在变量列表中) 我用的是spyder(蟒蛇) 我试过很多密码,但我已经没有了 对不起,这个愚蠢的问题。我想

我是一个新的python用户。我习惯于在matlab上编程。 我试图用Tkinter pack制作一个简单的GUI,但我遇到了一些问题。我已经阅读并搜索了我想要的东西,但我无法开发它

我试图做的是创建一个列表框,当我选择一个(或多个)选项时,索引将作为变量(数组或向量)返回(并存储),该变量可用于索引另一个数组

我得到的最好结果是一个列表框,其中打印了索引,但没有作为变量存储(至少它没有显示在变量列表中)

我用的是spyder(蟒蛇)

我试过很多密码,但我已经没有了


对不起,这个愚蠢的问题。我想我仍然在用Matlab的方式编写,首先导入tkinter,然后创建列表框。然后,您可以使用
curselection
获取列表框的内容

import tkinter as tk
root = tk.Tk() #creates the window
myListbox = tk.Listbox(root, select=multiple) #allows you to select multiple things
contentsOfMyListbox = myListbox.curselection(myListbox) #stores selected stuff in tuple

请参阅文档。

要使此应用程序保持简单,最好的选择是在需要使用列表框时获取列表框选择:

from tkinter import Tk, Listbox, MULTIPLE, END, Button

def doStuff():
    selected = lb.curselection()
    if selected: # only do stuff if user made a selection
        print(selected)
        for index in selected:
            print(lb.get(index)) # how you get the value of the selection from a listbox

def clear(lb):
    lb.select_clear(0, END) # unselect all

root = Tk()

lb = Listbox(root, selectmode=MULTIPLE) # create Listbox
for n in range(5): lb.insert(END, n) # put nums 0-4 in listbox
lb.pack() # put listbox on window

# notice no parentheses on the function name doStuff
doStuffBtn = Button(root, text='Do Stuff', command=doStuff)
doStuffBtn.pack()

# if you need to add parameters to a function call in the button, use lambda like this
clearBtn = Button(root, text='Clear', command=lambda: clear(lb))
clearBtn.pack()

root.mainloop()

我还添加了一个按钮来清除列表框选择,因为默认情况下无法取消选择项。

curselection
是列表框类的一个方法,必须像
.curselection()一样调用。非常感谢。你的代码解决了我的问题。现在我了解了按钮和列表框的组合是如何工作的。我习惯于使用Matlab中的GUIDE工具箱构建GUI,在这里,小部件的功能可以随时使用,而无需进行配置。