如何将python事件变量传递给另一个函数?

时间:2019-04-22 17:54:50

标签: python function events tkinter parameters

我正在构建一个简单的GUI,并且在尝试将事件变量传递到已经具有事件变量的另一个函数时遇到问题。

我正在尝试将变量“ sqlItemx”(它是“ self.item1”的事件变量)用于函数“ addNumbs”。

如何传递此变量供以后使用?

我尝试按照常规惯例简单地传递变量,但这对我不起作用。

下面摘录了我的代码:

def listitems(self, event):
    for widget in self.itemFrame.winfo_children():
        widget.destroy()
    searchLettersx = event.widget['text']
    mycursor.execute("SELECT Name FROM inventory WHERE Name LIKE '" + searchLettersx + "%' ")
    myresult = mycursor.fetchall()
    rcount = 0
    ccount = 0
    for y in myresult:
        self.item1 = tk.Button(self.itemFrame, text=y, bg='white', fg='deep sky blue', font=("Helvetica", 18), height='4', width='22')
        self.item1.grid(column=ccount, row=rcount)
        self.item1.bind('<Button-1>', self.addtoCart)
        if rcount >= 5:
            ccount += 1
            rcount = 0
        rcount += 1  

 def addtoCart(self, event):
    win = tk.Toplevel(bg="white")
    win.wm_title("Add to Cart")

    sqlItemx = event.widget['text']

    frame = tk.Frame(win)
    frame.grid()
    self.numpadFrame = tk.Frame(win, pady='20', bg="white")
    self.numpadFrame.grid(column='0', row='2')

    itemLabel = tk.Label(win, text=sqlItemx, bg="white", font=("Helvetica", 18), pady="30")
    itemLabel.grid(row=0, column=0)

    numLabel = tk.Label(win, text="How many? ", bg="white", font=("Helvetica", 18), pady="30", anchor='e', justify='left')
    numLabel.grid(row=1, column=0)

    numbers = ["1", "2", "3", "4" , "5" , "6" , "7" , "8" , "9" , " " , "0" , " "]
    count = 0

    for r in range(4):
        for c in range(3):
            numpad = tk.Button(self.numpadFrame, text=numbers[count], height='4', width='8', bg='white', fg='deep sky blue', font=("Helvetica", 18))
            numpad.grid(row=r+1,column=c)
            numpad.bind('<Button-1>', self.addNumbs)
            count += 1

    finalize = tk.Button(win, text="Submit", height='2', width='14', bg='white', fg='deep sky blue', font=("Helvetica", 18))
    finalize.grid(column='0', row='7', columnspan='4')
    finalize["command"] = win.destroy

def addNumbs(self, event):

    numPadx = event.widget['text']

    sql = "INSERT INTO tempcart (item, count) VALUES (%s, %s)"
    val = ("item", numPadx)
    addtempCart.execute(sql, val)
    mydb.commit()

    gettempCartID.execute("SELECT ID FROM tempcart ORDER BY ID DESC LIMIT 1")
    gettempCartID_result = gettempCartID.fetchall()

    gettempCart.execute("SELECT item, count FROM tempcart")
    gettempCart_result = gettempCart.fetchall()

    for y in gettempCart_result:
        for z in gettempCartID_result: 
            self.cartItem = tk.Label(self.cartFrame, text=y, bg="white", font=("Helvetica", 20))
            self.cartItem.grid(column="0", row=z)

    mydb.commit()

我的最终目标是能够将此变量的值传递给“ addNumbs”,并将其用作我的MySQL语句的变量“ val”中的值。

1 个答案:

答案 0 :(得分:0)

您不需要通过bind方法将命令附加到按钮;相反,您可以使用command关键字参数。

为了传递item_type参数,您需要遍历与创建的每个按钮相对应的各种参数,并在创建按钮时将它们包含在函数调用中。

也许是这样的:

...
list_of_items = [item0, item1, item2, ...]
item_buttons = []   # keep a reference on each button

for y, item in enumerate(list_of_items):
    btn = tk.Button(self.itemFrame, command=lambda item=item: self.addtoCart(item), ...)
    item_buttons.append(btn)
    btn.grid(column=ccount, row=rcount)
...