Tkinter框架尺寸不匹配主窗口及搜索窗口输入问题求助
解决Tkinter界面布局与搜索窗口输入问题
问题1:主窗口框架尺寸不匹配的修复
你设置的operations_frame和log_frame高度总和等于主窗口高度,但实际不匹配的核心原因:
- Tkinter的Frame默认会根据内部控件自动调整大小(
propagate=True),忽略手动设置的尺寸 - grid布局未给行分配权重,框架无法填充整个单元格空间
修复修改点:
# 主窗口设置行权重,按350:100的比例分配空间 self.root.grid_rowconfigure(0, weight=7) self.root.grid_rowconfigure(1, weight=2) self.root.grid_columnconfigure(0, weight=1) # 让列填充整个宽度 # Operations frame 修改 self.operations_frame = tkinter.Frame(self.root, width=700, height=350, bg='#293556') self.operations_frame.grid(row=0, sticky='nsew') # 填充整个单元格 self.operations_frame.grid_propagate(False) # 禁用尺寸自动调整,强制保持设置高度 # Log frame 修改 self.log_frame = tkinter.Frame(self.root, width=700, height=100, bg='#2E4583') self.log_frame.grid(row=1, sticky='nsew') self.log_frame.grid_propagate(False)
问题2:搜索窗口无法输入与布局混乱的修复
搜索窗口用了Label控件(只读,无法输入),且单列表局导致元素排版混乱。需要替换为标签+输入框的组合,用两列布局优化排版。
修复修改点(替换原search_details函数):
def search_details(): if not self.window_already_open: self.window_already_open = True search_window = tkinter.Toplevel() search_window.title('Search options') search_window.geometry('350x200') # 放大窗口适配布局 search_window.resizable(False, False) search_window.configure(bg='#293556') search_window.grid_columnconfigure(1, weight=1) # 让输入框列填充剩余空间 # 文件路径:标签+输入框 file_path_label = tkinter.Label(search_window, text="目标路径:", bg='#293556', fg='white') file_path_label.grid(row=0, column=0, sticky='w', padx=(15, 5), pady=(15, 0)) file_path_entry = tkinter.Entry(search_window, width=30) file_path_entry.grid(row=0, column=1, sticky='ew', padx=(0,15), pady=(15,0)) # 文件名:标签+输入框 file_name_label = tkinter.Label(search_window, text="文件名:", bg='#293556', fg='white') file_name_label.grid(row=1, column=0, sticky='w', padx=(15,5), pady=(15,0)) file_name_entry = tkinter.Entry(search_window, width=30) file_name_entry.grid(row=1, column=1, sticky='ew', padx=(0,15), pady=(15,0)) # 扩展名:标签+输入框 extension_label = tkinter.Label(search_window, text="扩展名:", bg='#293556', fg='white') extension_label.grid(row=2, column=0, sticky='w', padx=(15,5), pady=(15,0)) extension_entry = tkinter.Entry(search_window, width=30) extension_entry.grid(row=2, column=1, sticky='ew', padx=(0,15), pady=(15,0)) start_searching = tkinter.Button(search_window, text='Start searching', bg='#294F63', fg='white') start_searching.grid(row=3, column=0, columnspan=2, pady=(15,0)) # 按钮跨两列居中 def on_window_close(): self.window_already_open = False search_window.destroy() search_window.protocol("WM_DELETE_WINDOW", on_window_close)
完整修改后代码
import tkinter class MainBox: def __init__(self): # Main windows specifications self.root = tkinter.Tk() self.root.title(' Utility Box') self.root.geometry('700x450') self.root.resizable(False, False) self.root.configure(bg='red') self.root.iconbitmap(r'D:\2xProjects\Python\UtilityBox\resources\appicon.ico') self.window_already_open = False # 主窗口设置行和列的权重 self.root.grid_rowconfigure(0, weight=7) self.root.grid_rowconfigure(1, weight=2) self.root.grid_columnconfigure(0, weight=1) # Operations frame (search, sort, delete, rename etc) self.operations_frame = tkinter.Frame(self.root, width=700, height=350, bg='#293556') self.operations_frame.grid(row=0, sticky='nsew') self.operations_frame.grid_propagate(False) # Log frame # Will log the results of the operations (either success or failure) # Ex search: found the file/ files in the specified path self.log_frame = tkinter.Frame(self.root, width=700, height=100, bg='#2E4583') self.log_frame.grid(row=1, sticky='nsew') self.log_frame.grid_propagate(False) # Search window where user must fill the details required for the operation to start def search_details(): if not self.window_already_open: self.window_already_open = True search_window = tkinter.Toplevel() search_window.title('Search options') search_window.geometry('350x200') search_window.resizable(False, False) search_window.configure(bg='#293556') search_window.grid_columnconfigure(1, weight=1) # 文件路径标签+输入框 file_path_label = tkinter.Label(search_window, text="目标路径:", bg='#293556', fg='white') file_path_label.grid(row=0, column=0, sticky='w', padx=(15, 5), pady=(15, 0)) file_path_entry = tkinter.Entry(search_window, width=30) file_path_entry.grid(row=0, column=1, sticky='ew', padx=(0,15), pady=(15,0)) # Search based on the file name (ex: name or name.extension) file_name_label = tkinter.Label(search_window, text="文件名:", bg='#293556', fg='white') file_name_label.grid(row=1, column=0, sticky='w', padx=(15,5), pady=(15,0)) file_name_entry = tkinter.Entry(search_window, width=30) file_name_entry.grid(row=1, column=1, sticky='ew', padx=(0,15), pady=(15,0)) # Search files with the associated extension (ex: all .txt files) extension_label = tkinter.Label(search_window, text="扩展名:", bg='#293556', fg='white') extension_label.grid(row=2, column=0, sticky='w', padx=(15,5), pady=(15,0)) extension_entry = tkinter.Entry(search_window, width=30) extension_entry.grid(row=2, column=1, sticky='ew', padx=(0,15), pady=(15,0)) start_searching = tkinter.Button(search_window, text='Start searching', bg='#294F63', fg='white') start_searching.grid(row=3, column=0, columnspan=2, pady=(15,0)) def on_window_close(): self.window_already_open = False search_window.destroy() search_window.protocol("WM_DELETE_WINDOW", on_window_close) # Search functionality (button and description of the functionality) search_option = tkinter.Button(self.operations_frame, text='Search', bg='#294F63', fg='white', command=search_details) search_option.grid(row=0, column=0, sticky='nw', padx=15, pady=(15, 0)) search_description = tkinter.Label(self.operations_frame, text='Search description') search_description.grid(row=0, column=0, sticky='nw', padx=70, pady=(18, 0)) # Sort functionality (button and description of the functionality) sort_option = tkinter.Button(self.operations_frame, text='Sort', bg='#294F63', fg='white') sort_option.grid(row=1, column=0, sticky='nw', padx=15, pady=(5, 0)) sort_description = tkinter.Label(self.operations_frame, text='Sort description') sort_description.grid(row=1, column=0, sticky='nw', padx=55, pady=(7, 0)) # Delete functionality (button and description of the functionality) delete_option = tkinter.Button(self.operations_frame, text='Delete', bg='#294F63', fg='white') delete_option.grid(row=2, column=0, sticky='nw', padx=15, pady=(5, 0)) delete_description = tkinter.Label(self.operations_frame, text='Delete description') delete_description.grid(row=2, column=0, sticky='nw', padx=105, pady=(7, 0)) # Rename functionality (button and description of the functionality) rename_option = tkinter.Button(self.operations_frame, text='Rename', bg='#294F63', fg='white') rename_option.grid(row=3, column=0, sticky='nw', padx=15, pady=(5, 0)) rename_description = tkinter.Label(self.operations_frame, text='Rename description') rename_description.grid(row=3, column=0, sticky='nw', padx=105, pady=(7, 0)) # Duplication functionality (button and description of the functionality) duplication_option = tkinter.Button(self.operations_frame, text='Duplication', bg='#294F63', fg='white') duplication_option.grid(row=4, column=0, sticky='nw', padx=15, pady=(5, 0)) duplication_description = tkinter.Label(self.operations_frame, text='Duplication description') duplication_description.grid(row=4, column=0, sticky='nw', padx=105, pady=(7, 0)) # Permissions functionality (button and description of the functionality) permissions_option = tkinter.Button(self.operations_frame, text='Permissions', bg='#294F63', fg='white') permissions_option.grid(row=5, column=0, sticky='nw', padx=15, pady=(5, 0)) permissions_description = tkinter.Label(self.operations_frame, text='Permissions description') permissions_description.grid(row=5, column=0, sticky='nw', padx=105, pady=(7, 0)) # Compare functionality (button and description of the functionality) compare_option = tkinter.Button(self.operations_frame, text='Compare', bg='#294F63', fg='white') compare_option.grid(row=6, column=0, sticky='nw', padx=15, pady=(5, 0)) compare_description = tkinter.Label(self.operations_frame, text='Compare description') compare_description.grid(row=6, column=0, sticky='nw', padx=105, pady=(7, 0)) # Preview functionality (button and description of the functionality) preview_option = tkinter.Button(self.operations_frame, text='Preview', bg='#294F63', fg='white') preview_option.grid(row=7, column=0, sticky='nw', padx=15, pady=(5, 0)) preview_description = tkinter.Label(self.operations_frame, text='Preview description') preview_description.grid(row=7, column=0, sticky='nw', padx=105, pady=(7, 0)) # Compression/ decompression functionality (button and description of the functionality) compression_decompression_option = tkinter.Button(self.operations_frame, text='Compression/ Decompression', bg='#294F63', fg='white') compression_decompression_option.grid(row=8, column=0, sticky='nw', padx=15, pady=(5, 0)) compression_decompression_description = tkinter.Label(self.operations_frame, text='Compression/ decompression description') compression_decompression_description.grid(row=8, column=0, sticky='nw', padx=205, pady=(7, 0)) # Window running loop self.root.mainloop() if __name__ == '__main__': MainBox()
内容的提问来源于stack exchange,提问作者Dragos
相关产品推荐
相关产品推荐

