【问题标题】:tkinter return names of dynamic checkboxestkinter 返回动态复选框的名称
【发布时间】:2018-11-17 00:47:08
【问题描述】:

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

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

复选框在文本文件中是动态的。示例文件:

item1
item2
...
item100

GUI 屏幕:

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

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()

【问题讨论】:

    标签: python checkbox tkinter


    【解决方案1】:

    我无法弄清楚您的代码在处理多个文件方面试图做的所有事情,但以下显示了它的清理和运行版本,显示了一种跟踪已选择 Checkbuttons 的方法由用户。

    大部分重要的更改是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()
    

    【讨论】:

    • 非常感谢 Martineau 的解释。这正是我搜索的内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多