tkinter返回动态复选框的名称

时间:2018-11-15 21:33:02

标签: python checkbox tkinter

我正在尝试用python&tkinter编写我的第一个脚本。

当我需要单击按钮 Validate 时,当我需要输入选中的每个复选框的名称时,我被阻止并且迷路。

复选框是从文本文件动态生成的。样本文件:

item1
item2
...
item100

GUI的屏幕:

screenshot showing checkbuttons and validate button

这是我的代码:
(在#代码中,我尝试了但没有成功。)

from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob

class Example(Frame):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.master.title("My Menu")

        menubar = Menu(self.master)
        self.master.config(menu=menubar)

        fileMenu = Menu(menubar)

        submenu = Menu(fileMenu)
        submenu.add_command(label="lst1", command=self.onDisplay)
        submenu.add_command(label="lst2")
        submenu.add_command(label="lst3")
        fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)

        fileMenu.add_separator()

        fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
        menubar.add_cascade(label="File", underline=0, menu=fileMenu)

    ## Here the function which display checkboxes
    def onDisplay(self):
        self.pack(fill=BOTH, expand=True)
        frame1 = Frame(self)
        frame1.pack(fill=BOTH)
        lbl1 = Label(frame1, text="Choice", width=6)
        path = '/root/liste/*.txt'
        files=glob.glob(path)
        count = 0
        #var = dict()
        for file in files:
            with open(file, 'r') as lst_file:
                for item in lst_file:
                    #var[item]=IntVar()
                    #cb = Checkbutton(frame1, text=item.rstrip(), variable=var[item], command=self.cb)
                    ##Here all checkboxes generated dynamically
                    cb = Checkbutton(frame1, text=item.rstrip())
                    cb.grid(row=count//10, column=count%10)
                    #cb.pack()
                    count += 1
        #btn1 = Button(self, text='Validate', font=("Arial", 12), command=self.cb)
        btn1 = Button(self, text='Validate', font=("Arial", 12))
        btn1.pack(side=RIGHT, padx=5)

    def cb(self):
        print("variable is", self.var.get())

    def onExit(self):

        self.quit()

def main():

    root = Tk()
    root.geometry("800x550+300+300")
    app = Example()
    root.mainloop()


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

我无法弄清您的代码在处理多个文件方面想做的所有事情,但是以下内容显示了该文件的清理版本和功能版本,并提供了一种跟踪哪些Checkbutton拥有的方法由用户选择。

大多数重要更改都是对onDisplay()方法的更改-尽管我也将def cb(self):方法的名称更改为def validate(self):以匹配Button的名称(因为我发现称它为cb令人困惑,因为它也是onDisplay()中局部变量的名称。)

from tkinter import *
from tkinter.ttk import Frame, Label, Entry
import glob

class Example(Frame):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.master.title("My Menu")

        menubar = Menu(self.master)
        self.master.config(menu=menubar)

        fileMenu = Menu(menubar)

        submenu = Menu(fileMenu)
        submenu.add_command(label="lst1", command=self.onDisplay)
        submenu.add_command(label="lst2")
        submenu.add_command(label="lst3")
        fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)

        fileMenu.add_separator()

        fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
        menubar.add_cascade(label="File", underline=0, menu=fileMenu)

    ## Here is the function which displays checkboxes
    def onDisplay(self):
        self.pack(fill=BOTH, expand=True)
        frame1 = Frame(self)
        frame1.pack(fill=BOTH)
        lbl1 = Label(frame1, text="Choice", width=6)
#        path = '/root/liste/*.txt'
        path = './root_liste1.txt'  # changed for my testing.
        files=glob.glob(path)

        self.var = dict()
        count = 0
        for file in files:
            with open(file, 'r') as lst_file:
                for item in lst_file:
                    item = item.rstrip()
                    status = BooleanVar()
                    self.var[item] = status
                    cb = Checkbutton(frame1, text=item, variable=status)
                    cb.grid(row=count//10, column=count%10)
                    count += 1

        btn1 = Button(self, text='Validate', font=("Arial", 12),
                      command=self.validate)
        btn1.pack(side=RIGHT, padx=5)

    def validate(self):  # btn1 callback
        print('checked items:')
        for item, status in self.var.items():
            if status.get():  # Checked?
                print('  ', item)

    def onExit(self):
        self.quit()

def main():
    root = Tk()
    root.geometry("800x550+300+300")
    app = Example()
    root.mainloop()


if __name__ == '__main__':
    main()