繁体   English   中英

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

[英]How to pass a python event variable into another function?

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

我试图在函数“ addNumbs”中使用变量“ sqlItemx”,该变量是“ self.item1”的事件变量。

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

我试图简单地按照通常的约定传递变量,但这对我不起作用。

我的代码节录如下:

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”中的值之一。

您不需要通过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)
...

暂无
暂无

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

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