jeudi 15 novembre 2018

tkinter get value from dynamic checkboxe

I built my first script with this nice GUI/Python. Despite tutorials and examples, i block to get values from checkboxes from function display_srv.

I took a look at this topic tkinter get values from dynamic checkboxes , but an error still exist:

I understand the command option is not allow, but how can i get values?

def Readstatus(key):
    print(var.get(key))

def display_srv():
    var = dict()
    frame_1 = tk.LabelFrame(root, text="List SRV")
    frame_1.grid(row=0, sticky='ew', padx=20, pady=20, ipadx=5, ipady=5)
    path = '/root/liste/*.txt'
    files=glob.glob(path)
    count = 0
    for file in files:
        with open(file, 'r') as lst_file:
            for item in lst_file:
                var[item]=IntVar()
                tk.Checkbutton(frame_1, variable=var[item], text=item.rstrip()).grid(row=count//10, column=count%10,
                               command=lambda key=item: Readstatus(key))
                count += 1
root = Tk()
menu = Menu(root)
root.geometry('1000x500')
root.title("Test Menu")
root.config(menu=menu)
testpsimenu = Menu(menu)
menu.add_cascade(label="Test PSI", menu=testpsimenu, font=("Arial", 12))
testpsimenu.add_command(label="Select Snap", font=("Arial", 10), command=frame_1)
testpsimenu.add_separator()
testpsimenu.add_command(label="Select SRV", font=("Arial", 10), command=display_srv)

exitmenu  = Menu(menu)
menu.add_cascade(label="Exit", menu=exitmenu, font=("Arial", 12))
exitmenu.add_command(label="Exit", command=root.quit, font=("Arial", 10))

tk.Button(root,text='get value',command=Readstatus).pack()
mainloop()

Error message when list all item:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib64/python3.6/tkinter/__init__.py", line 1702, in __call__
    return self.func(*args)
  File "./test_menu_7.py", line 29, in display_srv
    command=lambda key=item: Readstatus(key))
  File "/usr/lib64/python3.6/tkinter/__init__.py", line 2223, in grid_configure
    + self._options(cnf, kw))
_tkinter.TclError: bad option "-command": must be -column, -columnspan, -in, -ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky

Error when click on button "Get Values"

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib64/python3.6/tkinter/__init__.py", line 1702, in __call__
    return self.func(*args)
TypeError: Readstatus() missing 1 required positional argument: 'key'

Many thanks for help

Regards,




Aucun commentaire:

Enregistrer un commentaire