Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/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 3.6.5获取复选框名称并将其保存到列表中_Python_Python 3.x_Checkbox_Tkinter_Tkinter Entry - Fatal编程技术网

如何使用tkinter、python 3.6.5获取复选框名称并将其保存到列表中

如何使用tkinter、python 3.6.5获取复选框名称并将其保存到列表中,python,python-3.x,checkbox,tkinter,tkinter-entry,Python,Python 3.x,Checkbox,Tkinter,Tkinter Entry,我想使用tkinter和复选框选择目录中的文件,并在按下按钮时将这些文件名保存在列表中: import speech_recognition as sr import playsound import os import glob import unidecode import pickle import random import tkinter from tkinter.constants import * ldv = os.listdir("D:/FFOutput/") i = 0 ld

我想使用tkinter和复选框选择目录中的文件,并在按下按钮时将这些文件名保存在列表中:

import speech_recognition as sr
import playsound
import os
import glob
import unidecode
import pickle
import random
import tkinter
from tkinter.constants import *

ldv = os.listdir("D:/FFOutput/")
i = 0
ldv1 = []
while i < len(ldv):
    ldv1.append(unidecode.unidecode(ldv[i]))
    i += 1
print(ldv1)

root = tkinter.Tk()
frame = tkinter.Frame(root, relief=RIDGE, borderwidth=10)
frame.pack(fill=BOTH, expand=1)
label = tkinter.Label(frame, text="choose file(s)")
label.pack(fill=X, expand=1)


a = 0
while a < len(ldv1):
    bouton = tkinter.Checkbutton(root, text=ldv1[a], command=print(ldv1[a]))
    a += 1
    bouton.pack()

button = tkinter.Button(frame, text="Exit", command=root.destroy)
button.pack(side=BOTTOM)

lr = []

buttonregister = tkinter.Button(root, text="Register checked files names in list lr and close tk")
buttonregister.pack(side=BOTTOM)

print(lr)

root.mainloop()
将语音识别作为sr导入
导入播放声音
导入操作系统
导入glob
导入单解码
进口泡菜
随机输入
进口tkinter
从tkinter.constants导入*
ldv=os.listdir(“D:/FFOutput/”)
i=0
ldv1=[]
而i
当我点击按钮Register时,我想将文件名附加到列表lr中并关闭框架


在该示例中,我希望在单击按钮时在shell中打印(lr)
“['alors soyez pret.mp3','c'est bien.mp3']”

要使Checkbutton保存值,必须使用tkinter中的布尔值(或任何其他变量)。这通常非常乏味,因为您必须为每个Checkbutton创建一个变量。这可以通过对Checkbutton进行子分类并为变量添加存储来避免。因为您也需要文本,所以我们也可以使用类来存储文本值

用下面的类替换checkbutton将完成此操作

class CheckBox(tkinter.Checkbutton):
    boxes = []  # Storage for all buttons

    def __init__(self, master=None, **options):
        tkinter.Checkbutton.__init__(self, master, options)  # Subclass checkbutton to keep other methods
        self.boxes.append(self)
        self.var = tkinter.BooleanVar()  # var used to store checkbox state (on/off)
        self.text = self.cget('text')  # store the text for later
        self.configure(variable=self.var)  # set the checkbox to use our var
然后,我们将使用该类制作如下按钮:

a=0
while a<len(ldv1):
   bouton=CheckBox(tk, text=ldv1[a], command=print(ldv1[a]))  # Replace Checkbutton
   a=a+1
   bouton.pack()

非常好,谢谢你,泽基。默认情况下,复选框按钮应与复选框相同。
for box in CheckBox.boxes:
    if box.var.get():  # Checks if the button is ticked
        lr.append(box.text)

print(lr)