简体   繁体   中英

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.

I am trying to use the variable "sqlItemx", which is an event variable for "self.item1", into the function "addNumbs".

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.

You do not need to attach a command to a button via the bind method; instead, you can use the command key word argument.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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