简体   繁体   中英

How to bind Functions to Ttk Combobox uniquely for every change in value of Combobox

I want to link Comboboxs in a pair uniquely, such as the values in a Combobox depends upon the current value in another Combobox. But i fail to get it to work. Only the initial value of one Combobox had effect on the Values in the other Combobox. Also I want the function to be called for every change in the value of combobox.

import tkinter as tk
from tkinter import ttk

class Win(tk.Tk):
    opt_dic1 = {'Option1' : ['1', '2'], 'Option2' : ['3', '4']}
    opt_dic2 = {'Option3' : ['5', '6'], 'Option4' : ['7', '8']}
    def __init__(self):
        super().__init__()
        top = tk.Frame(self)
        btm = tk.Frame(self)
        cmb1 = ttk.Combobox(top, values = list(Win.opt_dic1.keys()))
        cmb2 = ttk.Combobox(top)
        cmb3 = ttk.Combobox(btm, values = list(Win.opt_dic2.keys()))
        cmb4 = ttk.Combobox(btm)
        top.pack(side = 'top')
        btm.pack(side = 'bottom')
        cmb1.pack(side = 'left')
        cmb2.pack(side = 'left')
        cmb3.pack(side = 'left')
        cmb4.pack(side = 'left')
        cmb1.current(0)
        cmb3.current(0)
        cmb1.bind('<<ComboboxSelected>>', cmb2.config(values = Win.opt_dic1[cmb1.get()]))
        cmb3.bind('<<ComboboxSelected>>', cmb4.config(values = Win.opt_dic2[cmb3.get()]))

if __name__ == '__main__':
    Win().mainloop()

I expected for result as (1,2) in cmb2 for Option1 in cmb1, (3,4) in cmb2 for Option2 in cmb1 And (5,6) in cmb4 for Option3 in cmb3 and so on.

Well i just tried throwing the command with lambda: but then i got an error <lambda> takes 0....but 1 was given so i just add a argument in lambda function as lambda x: <function> and now i get the same expected result ie, the code works perfectly now, but even though i have got it to work but i still don't fully understand as to how it got solved, it'd be helpful if anyone could explain it.

Well i just tried throwing the command with lambda: but then i got an error takes 0....but 1 was given so i just add a argument in lambda function as lambda x: and now i get the same expected result ie, the code works perfectly now, but even though i have got it to work but i still don't fully understand as to how it got solved

Your lambda is being sent an event by the bind() . That is where your error comes from. By adding x to your lambda that took care of the event by allow one positional argument. The reason your code did not work without the properly formatted lambda is due having parenthesis cmb2.config() and the other config. When you assign some command/function to a bind() you need to save a reference to the command/function rather then calling it. This is done by not using parenthesis but at the same time you cannot pass arguments. That's what the lambda allows.

Example with lambda:

import tkinter as tk
from tkinter import ttk


class Win(tk.Tk):
    opt_dic1 = {'Option1': ['1', '2'], 'Option2': ['3', '4']}
    opt_dic2 = {'Option3': ['5', '6'], 'Option4': ['7', '8']}

    def __init__(self):
        super().__init__()
        top = tk.Frame(self)
        btm = tk.Frame(self)
        cmb1 = ttk.Combobox(top, values=list(Win.opt_dic1.keys()))
        cmb2 = ttk.Combobox(top)
        cmb3 = ttk.Combobox(btm, values=list(Win.opt_dic2.keys()))
        cmb4 = ttk.Combobox(btm)
        top.pack(side='top')
        btm.pack(side='bottom')
        cmb1.pack(side='left')
        cmb2.pack(side='left')
        cmb3.pack(side='left')
        cmb4.pack(side='left')
        cmb1.current(0)
        cmb3.current(0)
        cmb1.bind('<<ComboboxSelected>>', lambda x: cmb2.config(values=Win.opt_dic1[cmb1.get()]))
        cmb3.bind('<<ComboboxSelected>>', lambda x: cmb4.config(values=Win.opt_dic2[cmb3.get()]))


if __name__ == '__main__':
    Win().mainloop()

Example without lambda:

import tkinter as tk
from tkinter import ttk


class Win(tk.Tk):
    opt_dic1 = {'Option1': ['1', '2'], 'Option2': ['3', '4']}
    opt_dic2 = {'Option3': ['5', '6'], 'Option4': ['7', '8']}

    def __init__(self):
        super().__init__()
        top = tk.Frame(self)
        btm = tk.Frame(self)
        self.cmb1 = ttk.Combobox(top, values=list(Win.opt_dic1.keys()))
        self.cmb2 = ttk.Combobox(top)
        self.cmb3 = ttk.Combobox(btm, values=list(Win.opt_dic2.keys()))
        self.cmb4 = ttk.Combobox(btm)
        top.pack(side='top')
        btm.pack(side='bottom')
        self.cmb1.pack(side='left')
        self.cmb2.pack(side='left')
        self.cmb3.pack(side='left')
        self.cmb4.pack(side='left')
        self.cmb1.current(0)
        self.cmb3.current(0)
        self.cmb1.bind('<<ComboboxSelected>>', self.config_cmb2)
        self.cmb3.bind('<<ComboboxSelected>>', self.config_cmb4)

    def config_cmb2(self, _=None):
        self.cmb2.config(values=Win.opt_dic1[self.cmb1.get()])

    def config_cmb4(self, _=None):
        self.cmb4.config(values=Win.opt_dic2[self.cmb3.get()])


if __name__ == '__main__':
    Win().mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM