table.get_children
时间: 2023-10-11 13:08:31 浏览: 15
`table.get_children` 是一个常用的函数,用于获取一个表格中的所有子组件(children)。它可以用于检索表格中的所有行、列或单元格等子组件,并返回一个包含这些组件的列表。你可以进一步处理这个列表,比如进行数据分析、筛选或修改等操作。
请注意,`table.get_children` 的具体用法可能会根据你使用的编程语言或框架而有所不同。如果你能提供更多上下文信息,我将能够给出更具体的答案。
相关问题
def show_excel(self):query_entry = tk.Entry(self.container1) query_entry.pack(side=tk.LEFT, padx=5, pady=5) query_button = tk.Button(self.container1, text='查询', command=lambda: self.query_data(query_entry.get(), table2)) query_button.pack(side=tk.LEFT, padx=5, pady=5) def on_return_key(event): query_button.invoke() query_entry.bind('<Return>', on_return_key) def query_data(self, query_str, table): # 清空表格内容 table.delete(*table.get_children()) # 获取查询条件 query_col = 8 # 遍历表格数据,找到符合条件的数据 for row in self.data_sheet.iter_rows(min_row=2, values_only=True): if str(row[query_col - 1]).startswith(query_str): row_values = [str(cell) if cell is not None else "" for cell in row] table.insert("", tk.END, values=row_values)每運行一次就新建一個輸入框和按鈕怎麽解決
这个问题可以通过将创建 Entry 和 Button 的代码放在初始化方法中,并把它们作为实例变量保存起来来解决。这样每次调用 `show_excel` 方法时,就不会重新创建新的组件了。具体实现可以参考下面的代码:
```
def __init__(self, parent):
self.parent = parent
self.query_entry = tk.Entry(self.container1)
self.query_entry.pack(side=tk.LEFT, padx=5, pady=5)
self.query_entry.bind('<Return>', self.on_return_key)
self.query_button = tk.Button(self.container1, text='查询', command=lambda: self.query_data(self.query_entry.get(), table2))
self.query_button.pack(side=tk.LEFT, padx=5, pady=5)
def show_excel(self):
pass # 其他代码略
def on_return_key(self, event):
self.query_button.invoke()
def query_data(self, query_str, table):
# 清空表格内容
table.delete(*table.get_children())
# 获取查询条件
query_col = 8
# 遍历表格数据,找到符合条件的数据
for row in self.data_sheet.iter_rows(min_row=2, values_only=True):
if str(row[query_col - 1]).startswith(query_str):
row_values = [str(cell) if cell is not None else "" for cell in row]
table.insert("", tk.END, values=row_values)
```
这样,每次调用 `show_excel` 方法时,就不会创建新的 Entry 和 Button 了,而是直接使用已有的实例变量。
query_entry = tk.Entry(self.container1) query_entry.pack(side=tk.LEFT, padx=5, pady=5) query_button = tk.Button(self.container1, text='查询', command=lambda: self.query_data(query_entry.get(), table2)) query_button.pack(side=tk.LEFT, padx=5, pady=5) def on_return_key(event): query_button.invoke() query_entry.bind('<Return>', on_return_key) def query_data(self, query_str, table): # 清空表格内容 table.delete(*table.get_children()) # 获取查询条件 query_col = 8 # 遍历表格数据,找到符合条件的数据 for row in self.data_sheet.iter_rows(min_row=2, values_only=True): if str(row[query_col - 1]).startswith(query_str): row_values = [str(cell) if cell is not None else "" for cell in row] table.insert("", tk.END, values=row_values)這個代碼運行一次就會再建一個按鈕和文本框,怎麽解決
这个问题可能是因为你的代码在每次运行时都会重新创建一个新的文本框和按钮,而不是复用已经创建的组件。解决方法是在创建文本框和按钮之前,先检查一下是否已经创建过了,如果已经创建过了就不再创建,而是直接使用已有的组件。你可以将创建文本框和按钮的代码放在一个单独的方法中,然后在初始化方法中先检查一下是否已经创建过了,如果没有才调用创建方法。例如:
```
def __init__(self, parent):
self.parent = parent
self.query_entry = None
self.query_button = None
self.create_widgets()
def create_widgets(self):
if self.query_entry is None:
self.query_entry = tk.Entry(self.container1)
self.query_entry.pack(side=tk.LEFT, padx=5, pady=5)
self.query_entry.bind('<Return>', self.on_return_key)
if self.query_button is None:
self.query_button = tk.Button(self.container1, text='查询', command=lambda: self.query_data(self.query_entry.get(), table2))
self.query_button.pack(side=tk.LEFT, padx=5, pady=5)
def on_return_key(self, event):
self.query_button.invoke()
def query_data(self, query_str, table):
# 清空表格内容
table.delete(*table.get_children())
# 获取查询条件
query_col = 8
# 遍历表格数据,找到符合条件的数据
for row in self.data_sheet.iter_rows(min_row=2, values_only=True):
if str(row[query_col - 1]).startswith(query_str):
row_values = [str(cell) if cell is not None else "" for cell in row]
table.insert("", tk.END, values=row_values)
```
这样每次运行时都会检查是否已经创建了文本框和按钮,如果已经创建了就不会再创建,而是直接使用已有的组件。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![epub](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)