从绑定事件中检索Tkinter中的组合框名称

时间:2019-03-09 11:26:15

标签: python-3.x tkinter combobox

我试图在tkinter python中创建一个设置窗口,我试图为所有组合框创建相同的回调函数,但是我不知道如何识别哪个组合框是调用方。示例代码:

def Open_settings_Form():
    def callbackFunc(event):
        print(event.widget.current())

    global SETTINGS
    Settings_Form = Toplevel(Main_Form)
    Settings_Form.title("LOG Settings")
    Settings_Form.geometry('900x300')  

    labelTop = Label(Settings_Form, text = "Log Parsing Order")
    labelTop.grid(column=0, row=0)

    combo0 = ttk.Combobox(Settings_Form, state="readonly", values=Log_Fields)
    combo0.bind("<<ComboboxSelected>>", callbackFunc)
    combo0.current(SETTINGS['LOG_Order'][0])
    combo0.grid(column=0, row=1)
    combo1 = ttk.Combobox(Settings_Form, state="readonly", values=Log_Fields)
    combo1.bind("<<ComboboxSelected>>", callbackFunc)
    combo1.current(SETTINGS['LOG_Order'][1])
    combo1.grid(column=1, row=1)
    combo2 = ttk.Combobox(Settings_Form, state="readonly", values=Log_Fields)
    combo2.bind("<<ComboboxSelected>>", callbackFunc)
    combo2.current(SETTINGS['LOG_Order'][2])
    combo2.grid(column=2, row=1)
    combo3 = ttk.Combobox(Settings_Form, state="readonly", values=Log_Fields)
    combo3.bind("<<ComboboxSelected>>", callbackFunc)
    combo3.current(SETTINGS['LOG_Order'][3])
    combo3.grid(column=3, row=1)

在我的callbackFunc中,我可以获取组合框的选定项,但无法找到触发该事件的组合框。除了正在使用的 ComboboxSelected 以外,还有其他绑定方法吗? 以下是event.widget对象的方法/参数:

['_Misc__winfo_getint', '_Misc__winfo_parseitem', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_bind', '_configure', '_displayof', '_do', '_getboolean', '_getconfigure', '_getconfigure1', '_getdoubles', '_getints', '_grid_configure', '_gridconvvalue', '_last_child_ids', '_name', '_nametowidget', '_noarg_', '_options', '_register', '_report_exception', '_root', '_setup', '_subst_format', '_subst_format_str', '_substitute', '_tclCommands', '_w', '_windowingsystem', 'after', 'after_cancel', 'after_idle', 'anchor', 'bbox', 'bell', 'bind', 'bind_all', 'bind_class', 'bindtags', 'cget', 'children', 'clipboard_append', 'clipboard_clear', 'clipboard_get', 'columnconfigure', 'config', 'configure', 'current', 'delete', 'deletecommand', 'destroy', 'event_add', 'event_delete', 'event_generate', 'event_info', 'focus', 'focus_displayof', 'focus_force', 'focus_get', 'focus_lastfor', 'focus_set', 'forget', 'get', 'getboolean', 'getdouble', 'getint', 'getvar', 'grab_current', 'grab_release', 'grab_set', 'grab_set_global', 'grab_status', 'grid', 'grid_anchor', 'grid_bbox', 'grid_columnconfigure', 'grid_configure', 'grid_forget', 'grid_info', 'grid_location', 'grid_propagate', 'grid_remove', 'grid_rowconfigure', 'grid_size', 'grid_slaves', 'icursor', 'identify', 'image_names', 'image_types', 'index', 'info', 'insert', 'instate', 'keys', 'lift', 'location', 'lower', 'mainloop', 'master', 'nametowidget', 'option_add', 'option_clear', 'option_get', 'option_readfile', 'pack', 'pack_configure', 'pack_forget', 'pack_info', 'pack_propagate', 'pack_slaves', 'place', 'place_configure', 'place_forget', 'place_info', 'place_slaves', 'propagate', 'quit', 'register', 'rowconfigure', 'scan_dragto', 'scan_mark', 'select_adjust', 'select_clear', 'select_from', 'select_present', 'select_range', 'select_to', 'selection_adjust', 'selection_clear', 'selection_from', 'selection_get', 'selection_handle', 'selection_own', 'selection_own_get', 'selection_present', 'selection_range', 'selection_to', 'send', 'set', 'setvar', 'size', 'slaves', 'state', 'tk', 'tk_bisque', 'tk_focusFollowsMouse', 'tk_focusNext', 'tk_focusPrev', 'tk_setPalette', 'tk_strictMotif', 'tkraise', 'unbind', 'unbind_all', 'unbind_class', 'update', 'update_idletasks', 'validate', 'wait_variable', 'wait_visibility', 'wait_window', 'waitvar', 'widgetName', 'winfo_atom', 'winfo_atomname', 'winfo_cells', 'winfo_children', 'winfo_class', 'winfo_colormapfull', 'winfo_containing', 'winfo_depth', 'winfo_exists', 'winfo_fpixels', 'winfo_geometry', 'winfo_height', 'winfo_id', 'winfo_interps', 'winfo_ismapped', 'winfo_manager', 'winfo_name', 'winfo_parent', 'winfo_pathname', 'winfo_pixels', 'winfo_pointerx', 'winfo_pointerxy', 'winfo_pointery', 'winfo_reqheight', 'winfo_reqwidth', 'winfo_rgb', 'winfo_rootx', 'winfo_rooty', 'winfo_screen', 'winfo_screencells', 'winfo_screendepth', 'winfo_screenheight', 'winfo_screenmmheight', 'winfo_screenmmwidth', 'winfo_screenvisual', 'winfo_screenwidth', 'winfo_server', 'winfo_toplevel', 'winfo_viewable', 'winfo_visual', 'winfo_visualid', 'winfo_visualsavailable', 'winfo_vrootheight', 'winfo_vrootwidth', 'winfo_vrootx', 'winfo_vrooty', 'winfo_width', 'winfo_x', 'winfo_y', 'xview', 'xview_moveto', 'xview_scroll']

非常感谢

1 个答案:

答案 0 :(得分:1)

您可以将名称传递给combobox小部件,然后通过event.widget回调对其进行检索。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

def details(event):
    print (event.widget)

combo1 = ttk.Combobox(root,name="box1")
combo1["values"] = ("A","B","C")
combo1.pack()
combo1.bind("<<ComboboxSelected>>",details)

combo2 = ttk.Combobox(root,name="box2")
combo2["values"] = ("D","E","F")
combo2.pack()
combo2.bind("<<ComboboxSelected>>",details)

root.mainloop()