简体   繁体   English

用户单击按钮后创建新字段时如何分配绑定函数?

[英]How do I assign bind functions when new fields are created after user clicks a button?

I have a form where a user creates a record of product attributes by clicking a button.我有一个表单,用户可以通过单击按钮创建产品属性记录。 Once clicked, four combo boxes, one label, and one entry field are loaded on the form that make up a record.单击后,四个组合框、一个标签和一个输入字段将加载到构成一条记录的表单上。

The dropdown list on the 4th combo box is dependent on the entries made in the first 3 combo boxes.第 4 个组合框中的下拉列表取决于前 3 个组合框中的条目。 The end user is likely to click the "Add" button bunch of times prior to making entries.最终用户可能会在输入之前多次单击“添加”按钮。 How do I make sure that when a user goes back to the first record after clicking the Add button "N" times and makes changes to the combo boxes of the first record, the appropriate values are loaded in the 4th combo box?我如何确保当用户在单击添加按钮“N”次后返回到第一条记录并对第一条记录的组合框进行更改时,适当的值会加载到第四个组合框中?

Here is what I have这是我所拥有的

def addItemInEdit():
    global rowNum2
    global brandDropdownList2, typeDropdownList2, sizeDropdownList2, qtyTextList2, itemList2, 
     packedDateDropdownList2

####################### Loading label, dropdowns, and text fields

    itemList2.append(ttk.Label(frameEdit, text=str(rowNum2 + 1) + ". ").grid(row=rowNum2, column=0))
    brandDropdownList2.append(ttk.Combobox(frameEdit, state='readonly', values=listOfBrands, width=10))
    brandDropdownList2[-1].grid(row=rowNum2, column=1, pady=5, padx=5)
    brandDropdownList2[-1].current(0)

    typeDropdownList2.append(ttk.Combobox(frameEdit, state='readonly', values=listOftypes, width=5))
    typeDropdownList2[-1].grid(row=rowNum2, column=2)
    typeDropdownList2[-1].current(0)

    sizeDropdownList2.append(ttk.Combobox(frameEdit, state='readonly', values=listOfSizes, width=10))
    sizeDropdownList2[-1].grid(row=rowNum2, column=3, pady=5, padx=5)
    sizeDropdownList2[-1].current(0)

    packedDateDropdownList2.append(ttk.Combobox(frameEdit, state='disabled', width=7))
    packedDateDropdownList2[-1].grid(row=rowNum2, column=4, pady=5)

    qtyTextList2.append(ttk.Entry(frameEdit, width=7))
    qtyTextList2[-1].grid(row=rowNum2, column=5, padx=5)
    qtyTextList2[-1].insert(0, "Qty")


####################### Adding bind functions.

    for item in range(len(brandDropdownList2)):
        def getDates(event):
            listOfDates=[]

################################## Making sure the use has made actual selections
            if brandDropdownList2[len(brandDropdownList2)-1].get()!="Brand" and typeDropdownList2[len(brandDropdownList2)-1].get()!="Type" and sizeDropdownList2[len(brandDropdownList2)-1].get()!="Size":
                packedDateDropdownList2[len(brandDropdownList2) - 1].config(state='readonly')
                packedDateDropdownList[len(brandDropdownList) - 1].set('')
                getDatesQuery = "SELECT [PackedOn] FROM SalesData where [Brand]=? AND [Type] = ? AND [Size]=?"
                conForDates = pyodbc.connect(dbPath)
                curForDates = conForDates.cursor()
                dateListOutput = curForDates.execute(getDatesQuery,(brandDropdownList2[len(brandDropdownList2)-1].get(),typeDropdownList2[len(brandDropdownList2)-1].get(),sizeDropdownList2[len(brandDropdownList2)-1].get())).fetchall()

                for item in dateListOutput:
                    if item[0] not in listOfDates:
                        listOfDates.append(item[0])

                packedDateDropdownList2[len(brandDropdownList2)-1].config(values=listOfDates)
            else:
                packedDateDropdownList2[len(brandDropdownList2) - 1].set('')
                packedDateDropdownList2[len(brandDropdownList2) - 1].config(state='disabled')


        brandDropdownList2[len(brandDropdownList)-1].bind("<<ComboboxSelected>>",getDates)
        typeDropdownList2[len(brandDropdownList)-1].bind("<<ComboboxSelected>>",getDates)
        sizeDropdownList2[len(brandDropdownList)-1].bind("<<ComboboxSelected>>",getDates)

    rowNum2 = rowNum2 + 1


I know the "For" loop for binding is wrong, but I feel there is a feature of Python that I don't know.我知道用于绑定的“For”循环是错误的,但我觉得 Python 有一个我不知道的特性。

  1. Your getDates() function always get the values from the last row of comboboxes which is not correct.您的getDates()函数总是从组合框的最后一行获取值,这是不正确的。
  2. You only need to bind the function to newly added comboboxes.您只需要将函数绑定到新添加的组合框。
  3. Better to put getDates() out of addItemInEdit() function.最好将getDates()放在addItemInEdit()函数之外。

Below is modified version of your code:以下是您的代码的修改版本:

def getDates(event):
    index = event.widget.row_id

    # get the dates from database
    # is SELECT DISTINCT supported???
    getDatesQuery = "SELECT [PackedOn] FROM SalesData where [Brand] = ? AND [Type] = ? AND [Size] = ?"
    conForDates = pyodbc.connect(dbPath)
    curForDates = conForDates.cursor()
    dateListOutput = curForDates.execute(getDatesQuery,(brandDropdownList2[index].get(), typeDropdownList2[index].get(), sizeDropdownList2[index].get())).fetchall()
    # should conForDates.close() be called???

    listOfDates = list(set(item[0] for item in dateListOutput))

    packedDateDropdownList2[index].config(state='readonly')
    packedDateDropdownList2[index].config(values=listOfDates)
    packedDateDropdownList2[index].set('')

def addItemInEdit():
    global rowNum2

    itemList2.append(ttk.Label(frameEdit, text=str(rowNum2 + 1) + ". ").grid(row=rowNum2, column=0))
    brandDropdownList2.append(ttk.Combobox(frameEdit, state='readonly', values=listOfBrands, width=10))
    brandDropdownList2[-1].grid(row=rowNum2, column=1, pady=5, padx=5)
    brandDropdownList2[-1].current(0)
    brandDropdownList2[-1].row_id = rowNum2 # save the current row number

    typeDropdownList2.append(ttk.Combobox(frameEdit, state='readonly', values=listOftypes, width=5))
    typeDropdownList2[-1].grid(row=rowNum2, column=2)
    typeDropdownList2[-1].current(0)
    typeDropdownList2[-1].row_id = rowNum2 # save the current row number

    sizeDropdownList2.append(ttk.Combobox(frameEdit, state='readonly', values=listOfSizes, width=10))
    sizeDropdownList2[-1].grid(row=rowNum2, column=3, pady=5, padx=5)
    sizeDropdownList2[-1].current(0)
    sizeDropdownList2[-1].row_id = rowNum2 # save the current row number

    packedDateDropdownList2.append(ttk.Combobox(frameEdit, state='disabled', width=7))
    packedDateDropdownList2[-1].grid(row=rowNum2, column=4, pady=5)
    packedDateDropdownList2[-1].row_id = rowNum2 # save the current row number

    qtyTextList2.append(ttk.Entry(frameEdit, width=7))
    qtyTextList2[-1].grid(row=rowNum2, column=5, padx=5)
    qtyTextList2[-1].insert(0, "Qty")
    qtyTextList2[-1].row_id = rowNum2 # save the current row number

    # bind the required callback to newly added comboboxes
    brandDropdownList2[-1].bind("<<ComboboxSelected>>", getDates)
    typeDropdownList2[-1].bind("<<ComboboxSelected>>", getDates)
    sizeDropdownList2[-1].bind("<<ComboboxSelected>>", getDates)

    rowNum2 += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户单击按钮时,如何显示新的 window? - How do I display a new window when the user clicks a button? 当用户单击按钮时,如何打开新窗口? - How can I open a new window when the user clicks the button? 如何为按钮指定一个功能,以便在单击时,它会在Text()窗口中显示一个字符串? - How do I assign a function to a button so that when it clicks, it displays a string onto a Text() window? 用户单击取消时如何替换背景图像? - How do I replace the background image when user clicks cancel? 如何在用户单击按钮后插入新文本之前测试 Tkinter 文本框小部件的当前文本? - How to test the current text of a Tkinter text box widget before inserting new text after user clicks on a button? 如何在用户单击按钮并生成新值时删除以前的 Tkinter 值? - How to get previous Tkinter value to delete when user clicks a button and generate a new value? 如何使用滚动条和按钮选择用户在python列表中单击的行? - How do I use a scrollbar and a button to select what line the user clicks on in a list in python? Pygame:如何在用户点击某个区域时显示一个新框,并在用户再次点击时关闭该框 - Pygame: How to display a new box when user clicks an area, and close the box when the user clicks again 当用户单击 Odoo 13 表单中的保存按钮时,我如何运行一段代码? - How can i run a piece of code when user clicks on save button in a form in Odoo 13? 如何在字典中分配函数? - How do I assign functions in a dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM