Python 如果由TKINTER checkbuttons选择,则从列表中提取值

Python 如果由TKINTER checkbuttons选择,则从列表中提取值,python,tkinter,Python,Tkinter,我正在尝试制作一个程序,只选择在checkbutton中选中的列表元素,然后在新列表中返回所选元素。 我知道创建一个类可能更好(如果这是您的方法,可以尝试一下),但目前我的代码没有。 代码的主要部分是功能性的,如果我在主功能中使用列表,甚至可以显示正确的选择,但目标是通过选择缩小列表的范围 这是我的密码: from tkinter import * import random #the main list with all players lista_players = ["Cubanao

我正在尝试制作一个程序,只选择在checkbutton中选中的列表元素,然后在新列表中返回所选元素。 我知道创建一个类可能更好(如果这是您的方法,可以尝试一下),但目前我的代码没有。 代码的主要部分是功能性的,如果我在主功能中使用列表,甚至可以显示正确的选择,但目标是通过选择缩小列表的范围

这是我的密码:

from tkinter import *
import random


#the main list with all players

lista_players = ["Cubanao", "martim","miner","Toninho","Bruno Pires", "Cacatuaba", "Dukalove", "Jhonathan", "Maximus",
                 "Rafael GX", "Sikilho", "Spatodea", "Taimica", "Tanaka", "zntta"]



#function that select players from the list based on the entries

def seletor_times():
    list1.delete(0,END)
    list2.delete(0,END)
    time_1 = random.sample(selecionados, k=n1.get())
    lista_players_restantes =  [x for x in selecionados if x not in time_1]
    time_2 = random.sample(lista_players_restantes, k= n2.get())
    list1.insert(END,*time_1)
    list2.insert(END,*time_2)



#Creation of the tkinter gui

window = Tk()
window.wm_title("VWB Pro Team Selector")



l1 = Label(window, text="Escolha os jogadores :")
l1.grid(row=0, column=0)



l2 = Label(window, text="Jogadores no time 1 :")
l2.grid(row=0, column=1)



n1=IntVar()
e1=Entry(window,textvariable=n1)
e1.grid(row=0,column=2)




l3 = Label(window, text="  ")
l3.grid(row=0, column=3)


l4 = Label(window, text="Jogadores no time 2 :")
l4.grid(row=0, column=4)


n2=IntVar()
e2=Entry(window,textvariable=n2)
e2.grid(row=0,column=5)



l5 = Label(window, text="Time 1:")
l5.grid(row=1, column=4)

list1=Listbox(window, height=6,width=35)
list1.grid(row=1,column=3,rowspan=6,columnspan=2)


l6 = Label(window, text="Time 2:")
l6.grid(row=8, column=4)


list2=Listbox(window, height=6,width=35)
list2.grid(row=9,column=3,rowspan=6,columnspan=2)



b1=Button(window,text="Gerar Times", width=24,command=seletor_times)
b1.grid(row=2,column=1,columnspan = 2)

b2=Button(window,text="Sair", width=24,command=window.destroy)
b2.grid(row=3,column=1,columnspan = 2)

##Creation of the checkboxes with a for loop

p=0
for player in lista_players:
    cb=Checkbutton(window,text=player, width=24, variable= p)
    cb.grid(row=p+1,column=0)
    p +=1    

# The Checkbuttons should populate this list:

selecionados=[]


window.mainloop()


在D-00和jasonharper的建议下,我设法为每个条目创建了一个复选按钮。这不是最好的办法,但它奏效了。在我将变量和按钮放在两个列表中,并使用变量的Boll值来获取状态和它们之后,我得到了按钮名称中的字符串,代码如下:

#the function:

def ponderado():
    i=0
    selecionados=[]
    list1.delete(0,END)
    list2.delete(0,END)
    lista= [c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17]
    lista2=[ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9,ch10,ch11,ch12,ch13,ch14,ch15,ch16,ch17]
    while i <  len(lista):
        if lista[i].get() == True:
            selecionados.append(lista2[i].cget("text"))
        i +=1

    d2= {}
    for i in selecionados:
        d2[i] = players[i]

    d2 = {k: v for k, v in sorted(d2.items(), key=lambda item: item[1])}
    time_1=[]
    x=0
    limite = sum(d2.values())/2
    for i in sorted(d2):
        a = random.choice(list(d2))
        if x >= limite or len(time_1) == 5:
            time_2 =  [x for x in selecionados if x not in time_1]
            if len(time_2) > 5 and len (time_1) < 5:
                time_1.append(time_2[-1])
                time_2.pop()

        elif a not in time_1:

            x += d2[a]
            time_1.append(a)


    list1.insert(END,*time_1)
    list2.insert(END,*time_2)


#The buttons (example)

c1 = BooleanVar()
ch1 = Checkbutton(window, text="Tomtom", variable=c1)
ch1.grid(row=1,column=0)

c2 = BooleanVar()
ch2 = Checkbutton(window, text="Tanakir", variable=c2)
ch2.grid(row=2,column=0)

#功能:
def poundado():
i=0
selecionados=[]
列表1.删除(0,结束)
列表2.删除(0,结束)
列表A=[c1、c2、c3、c4、c5、c6、c7、c8、c9、c10、c11、c12、c13、c14、c15、c16、c17]
列表A2=[ch1、ch2、ch3、ch4、ch5、ch6、ch7、ch8、ch9、ch10、ch11、ch12、ch13、ch14、ch15、ch16、ch17]
而我=limite或len(时间1)==5:
时间_2=[x代表x,如果x不在时间_1中]
如果len(时间_2)>5且len(时间_1)<5:
时间1.追加(时间2[-1])
时间2.pop()
elif a不及时_1:
x+=d2[a]
时间1.追加(a)
列表1.插入(结束,*time_1)
列表2.插入(结束,*time_2)
#按钮(示例)
c1=布尔值()
ch1=检查按钮(窗口,text=“Tomtom”,变量=c1)
ch1.网格(行=1,列=0)
c2=布尔值()
ch2=检查按钮(窗口,text=“Tanakir”,变量=c2)
ch2.网格(行=2,列=0)

在D-00和jasonharper的建议下,我设法为每个条目创建了一个复选按钮。这不是最好的办法,但它奏效了。在我将变量和按钮放在两个列表中,并使用变量的Boll值来获取状态和它们之后,我得到了按钮名称中的字符串,代码如下:

#the function:

def ponderado():
    i=0
    selecionados=[]
    list1.delete(0,END)
    list2.delete(0,END)
    lista= [c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17]
    lista2=[ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9,ch10,ch11,ch12,ch13,ch14,ch15,ch16,ch17]
    while i <  len(lista):
        if lista[i].get() == True:
            selecionados.append(lista2[i].cget("text"))
        i +=1

    d2= {}
    for i in selecionados:
        d2[i] = players[i]

    d2 = {k: v for k, v in sorted(d2.items(), key=lambda item: item[1])}
    time_1=[]
    x=0
    limite = sum(d2.values())/2
    for i in sorted(d2):
        a = random.choice(list(d2))
        if x >= limite or len(time_1) == 5:
            time_2 =  [x for x in selecionados if x not in time_1]
            if len(time_2) > 5 and len (time_1) < 5:
                time_1.append(time_2[-1])
                time_2.pop()

        elif a not in time_1:

            x += d2[a]
            time_1.append(a)


    list1.insert(END,*time_1)
    list2.insert(END,*time_2)


#The buttons (example)

c1 = BooleanVar()
ch1 = Checkbutton(window, text="Tomtom", variable=c1)
ch1.grid(row=1,column=0)

c2 = BooleanVar()
ch2 = Checkbutton(window, text="Tanakir", variable=c2)
ch2.grid(row=2,column=0)

#功能:
def poundado():
i=0
selecionados=[]
列表1.删除(0,结束)
列表2.删除(0,结束)
列表A=[c1、c2、c3、c4、c5、c6、c7、c8、c9、c10、c11、c12、c13、c14、c15、c16、c17]
列表A2=[ch1、ch2、ch3、ch4、ch5、ch6、ch7、ch8、ch9、ch10、ch11、ch12、ch13、ch14、ch15、ch16、ch17]
而我=limite或len(时间1)==5:
时间_2=[x代表x,如果x不在时间_1中]
如果len(时间_2)>5且len(时间_1)<5:
时间1.追加(时间2[-1])
时间2.pop()
elif a不及时_1:
x+=d2[a]
时间1.追加(a)
列表1.插入(结束,*time_1)
列表2.插入(结束,*time_2)
#按钮(示例)
c1=布尔值()
ch1=检查按钮(窗口,text=“Tomtom”,变量=c1)
ch1.网格(行=1,列=0)
c2=布尔值()
ch2=检查按钮(窗口,text=“Tanakir”,变量=c2)
ch2.网格(行=2,列=0)

我看到的一些基本问题:您必须使用Tkinter的一种Var类型(IntVar、BooleanVar等)作为复选按钮的
变量=
选项,而不是普通的Python变量;如果要独立检查,则必须为每个Checkbutton创建不同的Var;您必须将这些变量保存在列表中,以便以后可以对每个变量调用
.get()
,以获取它们的状态。我尝试在创建复选按钮的相同for循环中创建此变量。但无法将新列表与旧列表链接。但我会再试一次。试着删除
variable=p
,将所有
检查按钮逐一放入
按钮列表中,并替换为
按钮[…].state()
以获取它们的状态。我看到一些基本问题:必须使用Tkinter的一种Var类型(IntVar、BooleanVar等)对于复选按钮的
变量=
选项,不是普通的Python变量;如果要独立检查,则必须为每个Checkbutton创建不同的Var;您必须将这些变量保存在列表中,以便以后可以对每个变量调用
.get()
,以获取它们的状态。我尝试在创建复选按钮的相同for循环中创建此变量。但无法将新列表与旧列表链接。但我会再试一次。尝试删除
variable=p
,将所有
检查按钮逐个放入列表
按钮中,并用
按钮[…]替换。state()
获取它们的状态。