如何对使用网格/列表创建的条目使用entry.bind("<FocusIn>", self.method_calling)

问题描述 投票:0回答:1

在此链接上是此代码示例,它使用 get(self) 和 set(set) 方法在类中生成条目网格:

from tkinter import * class Table: def __init__(self, root, values): total_rows = len(values) total_columns = len(values[0]) # create a 2-D list to store the entry widgets self.entries = [[None]*total_columns for _ in range(total_rows)] # code for creating table for i in range(total_rows): for j in range(total_columns): e = Entry(root, width=10, fg='blue', font=('Arial',16,'bold')) e.grid(row=i, column=j) e.insert(END, values[i][j]) self.entries[i][j] = e # store entry into list # function to get value of entry at (row, col) def get(self, row, col): try: return self.entries[row][col].get() except IndexError as ex: print("Error on Table.get():", ex) # function to set value of entry at (row, col) def set(self, row, col, value): try: self.entries[row][col].delete(0, "end") self.entries[row][col].insert("end", value) except IndexError as ex: print("Error on Table.set():", ex) # take the data lst = [(1,'Raj','Mumbai',19), (2,'Aaryan','Pune',18), (3,'Vaishnavi','Mumbai',20), (4,'Rachna','Mumbai',21), (5,'Shubham','Delhi',21)] # create root window root = Tk() root.geometry("800x600") t = Table(root, lst) # pass lst to Table() # update entry at row 0 column 1 t.set(0, 1, "hello") root.mainloop()
通常,对于“手动”放置的条目,我可以使用:

self.entry_path = Entry(root, width=72, font= ('Helvetica 13')) self.entry_path.insert(INSERT, "Text Entry") self.entry_path.bind("<FocusIn>", self.method_calling)
调用事件方法获取:

def method_calling(self, event): self.entry_path.delete(0, END) self.entry_path.config(state= "disabled")
但我不知道如何从网格/列表中 .bind 或 .config 条目。

编辑: 如果我这样使用:

class Table: def __init__(self, root, values): total_rows = len(values) total_columns = len(values[0]) # create a 2-D list to store the entry widgets self.entries = [[None]*total_columns for _ in range(total_rows)] # code for creating table for i in range(total_rows): for j in range(total_columns): e = Entry(root, width=10, fg='blue', font=('Arial',16,'bold')) e.grid(row=i, column=j) e.bind("<FocusIn>", lambda: self.method_calling(i,j)) e.insert(END, values[i][j]) self.entries[i][j] = e # store entry into list # function to get value of entry at (row, col) def get(self, row, col): try: return self.entries[row][col].get() except IndexError as ex: print("Error on Table.get():", ex) # function to set value of entry at (row, col) def set(self, row, col, value): try: self.entries[row][col].delete(0, "end") self.entries[row][col].insert("end", value) except IndexError as ex: print("Error on Table.set():", ex) def method_calling(self,event,i,j): print("row_"+str(i)+" column_"+str(j)) # take the data lst = [(1,'Raj','Mumbai',19), (2,'Aaryan','Pune',18), (3,'Vaishnavi','Mumbai',20), (4,'Rachna','Mumbai',21), (5,'Shubham','Delhi',21)] # create root window root = Tk() root.geometry("800x600") t = Table(root, lst) # pass lst to Table() # update entry at row 0 column 1 t.set(0, 1, "hello") root.mainloop()
当我点击一个单元格时,它给了我这个错误:

enter image description here

如果我使用命令中没有“lambda::”且调用方法中没有“event”的代码,如下所示:

e.grid(row=i, column=j) e.bind("<FocusIn>", self.method_calling(i,j))
....
....

def method_calling(self,i,j): print("row_"+str(i)+" column_"+str(j))
无需按任何东西即可自动触发。

enter image description here

python tkinter widget grid bind
1个回答
0
投票
您需要使用参数的

默认值来获取所需的行和列,如下所示:

e.bind("<FocusIn>", lambda evt, i=i, j=j: self.method_calling(i, j))

method_calling()

的定义应该是:

def method_calling(self, i, j): print(f"row_{i} column_{j}")
    
© www.soinside.com 2019 - 2024. All rights reserved.