[英]How can I take Input from multiple tkinter listboxes?
I have created two Listboxes and I want to be able to take the selection from both of them.我创建了两个列表框,我希望能够从它们中进行选择。 Initially, I was only able to take the selection from one but I couldn't select both Listboxes as it would lose the selection, I solved that using
exportselection = False
, however, when I ask for the selection from the Card_Type
Listbox, it returns the result from the Player_Name
Listbox.最初,我只能从一个中选择,但我不能 select 两个列表框,因为它会丢失选择,我解决了使用
exportselection = False
的问题,但是,当我要求从Card_Type
列表框中进行选择时,它返回Player_Name
列表框的结果。 The code for both of the Listboxes are shown below:两个列表框的代码如下所示:
Player_Name = Listbox(WABOT_U, font=("Rockwell","30"), yscrollcommand = Player_Name_Scroll_Bar.set, justify = CENTER, activestyle = "dotbox")
Player_Name.place(x = 150, y = 590, height = 100, width = 710)
Card_Type = Listbox(WABOT_U, font=("Rockwell","30"), yscrollcommand = Card_Type_Scroll_Bar.set, justify = CENTER, activestyle = "dotbox")
Card_Type.place(x = 150, y = 590, height = 100, width = 710)
Card_Type.config(exportselection = False)
This is an example of how I have tried to retrieve the selection from both Listboxes:这是我如何尝试从两个列表框中检索选择的示例:
try:
Name = Player_Name.selection_get()
except:
Name = ""
try:
Card = Card_Type.selection_get()
except:
Card = ""
This code returns the Name for both selections.此代码返回两个选择的名称。 I assume this has something to do with the
exportselection = False
, however, I have no other work around for selection from different Listboxes, so if someone could provide an alternate method of doing that or simply a solution to the problem that would be awesome.我认为这与
exportselection = False
有关,但是,我没有其他工作可以从不同的列表框中进行选择,因此如果有人可以提供另一种方法来做到这一点,或者只是解决这个问题的解决方案,那就太棒了。
Example Code:示例代码:
def Submit():
try:
Name = Player_Name.selection_get()
except:
Name = ""
try:
Card = Card_Type.selection_get()
except:
Card = ""
print(Name, Card)
Window = Tk()
Window.geometry("1920x1080")
Window.wm_attributes('-fullscreen', 1)
Player_Name_Scroll_Bar = Scrollbar(Window)
Player_Name_Scroll_Bar.pack(side = RIGHT, fill = Y)
Player_Name = Listbox(Window, font=("Rockwell","30"), yscrollcommand = Player_Name_Scroll_Bar.set, justify = CENTER, activestyle = "dotbox")
Player_Name.place(x = 150, y = 590, height = 100, width = 710)
Player_Name_Scroll_Bar.config(command = Player_Name.yview)
Player_Name.insert(0, "Michael")
Player_Name.insert(1, "John")
Card_Type_Scroll_Bar = Scrollbar(Window)
Card_Type_Scroll_Bar.pack(side = RIGHT, fill = Y)
Card_Type = Listbox(Window, font=("Rockwell","30"), yscrollcommand = Card_Type_Scroll_Bar.set, justify = CENTER, activestyle = "dotbox")
Card_Type.place(x = 1060, y = 590, height = 100, width = 710)
Card_Type.config(exportselection = False)
Card_Type_Scroll_Bar.config(command = Card_Type.yview)
Card_Type.insert(0, "Yellow")
Card_Type.insert(1, "Blue")
Submit_Button = Button(Window, borderwidth=0, highlightthickness = 0, text = "Submit", justify=CENTER, command = Submit)
Submit_Button.place(x = 560, y = 900, height = 150, width = 800)
A program can only have one selection at a time, and selection_get
will return that one selection regardless of which widget has the selection.一个程序一次只能有一个选择,并且
selection_get
将返回那个选择,而不管哪个小部件有选择。 In other words, selection_get
won't return the selection of the widget, it returns the selection owned by the program as a whole.换句话说,
selection_get
不会返回小部件的选择,它返回整个程序拥有的选择。
You should be calling the listbox method get
and curselection
to get the currently selected value for each listbox.您应该调用列表框方法
get
和curselection
来获取每个列表框的当前选定值。
selection = Player_Name.curselection()
Name = Player_Name.get(selection[0]) if selection else ""
selection = Card_Type.curselection()
Card = Card_Type.get(selection[0]) if selection else ""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.