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 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)在這個代碼條件一個用戶按回車也能觸發查詢按鈕
时间: 2024-04-01 09:35:46 浏览: 13
您可以将以下代码添加到query_entry Entry小部件的绑定中,以便在用户按下回车键时触发查询按钮:
query_entry.bind('<Return>', lambda event: query_button.invoke())
这将绑定“Return”键的事件处理程序,以调用query_button的invoke()方法,从而触发查询按钮的单击事件。
相关问题
# 创建左侧面板 self.panel_left = tk.Frame(master, width=150, bg='lightcyan') self.panel_left.pack(side=tk.LEFT, fill=tk.Y) # 创建Frame来包含标签和下拉框 input_frame = tk.Frame(self.panel_left) input_frame.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5) # 创建标签 ri = datetime.datetime.now().strftime('%Y/%m/%d') self.date_label = tk.Label(input_frame, text="點檢日期:") self.date_label.pack(side=tk.LEFT, padx=5, pady=5) # 创建下拉框 self.date_entry = tk.ttk.Combobox(input_frame, values=[ri]) self.date_entry.pack(side=tk.TOP, padx=5, pady=5) self.date_entry.current(0) # 创建标签 self.line_label1 = tk.Label(input_frame, text="线别:") self.line_label1.pack(side=tk.LEFT, padx=5, pady=5) # 创建下拉框 self.line_entry1 = tk.ttk.Combobox(input_frame,values=["PA綫", "PB綫", "PC綫", "PD綫", "NF綫", "NE綫", "ND綫", "NC綫", "NB綫","NA綫"]) self.line_entry1.pack(side=tk.TOP, padx=5, pady=5) self.line_entry1.current(0)將標簽和下拉輸入框對齊
要将标签和下拉输入框对齐,可以对它们的布局进行微调,具体地:
1. 将标签和下拉输入框的 `side` 参数都改为 `tk.LEFT`,表示它们在水平方向上排列在一列。
2. 将标签和下拉输入框的 `padx` 和 `pady` 参数都改为 `2`,以适当地减小组件之间的间距。
修改后的代码如下:
```python
# 创建左侧面板
self.panel_left = tk.Frame(master, width=150, bg='lightcyan')
self.panel_left.pack(side=tk.LEFT, fill=tk.Y)
# 创建 Frame 来包含标签和下拉框
input_frame = tk.Frame(self.panel_left)
input_frame.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
# 创建标签
ri = datetime.datetime.now().strftime('%Y/%m/%d')
self.date_label = tk.Label(input_frame, text="點檢日期:")
self.date_label.pack(side=tk.LEFT, padx=2, pady=2)
# 创建下拉框
self.date_entry = tk.ttk.Combobox(input_frame, values=[ri])
self.date_entry.pack(side=tk.LEFT, padx=2, pady=2)
self.date_entry.current(0)
# 创建标签
self.line_label1 = tk.Label(input_frame, text="线别:")
self.line_label1.pack(side=tk.LEFT, padx=2, pady=2)
# 创建下拉框
self.line_entry1 = tk.ttk.Combobox(input_frame, values=["PA綫", "PB綫", "PC綫", "PD綫", "NF綫", "NE綫", "ND綫", "NC綫", "NB綫", "NA綫"])
self.line_entry1.pack(side=tk.LEFT, padx=2, pady=2)
self.line_entry1.current(0)
```
这样就能将标签和下拉输入框对齐,并且适当地调整了组件之间的间距。
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)
```
这样每次运行时都会检查是否已经创建了文本框和按钮,如果已经创建了就不会再创建,而是直接使用已有的组件。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)