简体   繁体   English

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

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

I am working on building a simple GUI and I am running into an issue when trying to pass in an event variable into another function that already has an event variable. 我正在构建一个简单的GUI,并且试图将事件变量传递到已经具有事件变量的另一个函数中时遇到问题。

I am trying to use the variable "sqlItemx", which is an event variable for "self.item1", into the function "addNumbs". 我试图在函数“ addNumbs”中使用变量“ sqlItemx”,该变量是“ self.item1”的事件变量。

How can I pass this variable for later use? 如何传递此变量供以后使用?

I have tried simply passing the variable in the usual convention but this did not work for me. 我试图简单地按照通常的约定传递变量,但这对我不起作用。

An excerpt of my code is listed below: 我的代码节录如下:

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()

My end goal is to be able to able to pass the value of this variable into "addNumbs" and use it as one the values in variable "val" for my MySQL statement. 我的最终目标是能够将此变量的值传递到“ addNumbs”中,并将其用作我的MySQL语句的变量“ val”中的值之一。

You do not need to attach a command to a button via the bind method; 您不需要通过bind方法将命令附加到按钮上。 instead, you can use the command key word argument. 相反,您可以使用command关键字参数。

In order to pass the item_type argument, you need to iterate over the various arguments corresponding to each button created, and enclose them in the function call as the button is created.. 为了传递item_type参数,您需要遍历与创建的每个按钮相对应的各种参数,并在创建按钮时将它们包含在函数调用中。

Maybe something like this: 也许是这样的:

...
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