添加占位的 Label tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 添加“查询”按钮 ttk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(master))).pack( side=tk.LEFT, padx=2, pady=2, anchor=tk.CENTER) # 添加占位的 Label tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 添加“關閉”按钮 ttk.Button(toolbar, text="關閉", style='my.TButton', command=self.quit_app).pack(side=tk.LEFT, padx=2, pady=2, anchor=tk.CENTER) # 显示工具栏 toolbar.pack(side=tk.TOP, fill=tk.BOTH這個代碼
时间: 2024-02-21 13:00:51 浏览: 92
您可以通过将 `side` 参数设置为 `tk.LEFT` 来将占位 Label 移动到左侧,如下所示:
```
# 添加占位的 Label
tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 添加“查询”按钮
ttk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(master))).pack(
side=tk.LEFT, padx=2, pady=2, anchor=tk.CENTER)
# 添加占位的 Label
tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 添加“關閉”按钮
ttk.Button(toolbar, text="關閉", style='my.TButton', command=self.quit_app).pack(
side=tk.LEFT, padx=2, pady=2, anchor=tk.CENTER)
# 显示工具栏
toolbar.pack(side=tk.TOP, fill=tk.BOTH)
```
这将使占位 Label 和按钮都位于工具栏的左侧。
相关问题
toolbar_frame.pack(side='right', fill='y')bottom_frame.pack(side='bottom', fill='x')tk.Label(toolbar, text="当前选中:").pack(side=tk.LEFT, padx=0.02, pady=0.02)tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True)tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True)ttk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(master))).pack( side=tk.LEFT, padx=0.02, pady=0.02)tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True) ttk.Button(toolbar, text="關閉", style='my.TButton', command=self.quit_app).pack(side=tk.LEFT, padx=0.02, pady=0.02) # 显示工具栏 toolbar.pack(side=tk.TOP, fill=tk.BOTH)self.canvas.pack(side=tk.LEFT, fill=tk.BOTH)self.scrollbar.pack(side=tk.LEFT, fill=tk.BOTH) self.panel_right.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) input_frame.pack(side=tk.TOP, fill=tk.X, padx=0.05, pady=0.05)self.label1.pack(side=tk.LEFT, padx=0.02, pady=0.02)self.label2.pack(side=tk.LEFT, padx=0.02, pady=0.02)self.combobox2.pack(side=tk.LEFT, padx=0.02, pady=0.02)self.label3.pack(side=tk.LEFT, padx=0.02, pady=0.02)self.combobox3.pack(side=tk.LEFT, padx=0.02, pady=0.02) self.label5.pack(side=tk.LEFT, padx=0.02, pady=0.02)self.combobox5.pack(side=tk.LEFT, padx=0.05, pady=0.05)self.label6.pack(side=tk.LEFT, padx=0.05, pady=0.05)self.combobox6.pack(side=tk.LEFT, padx=0.05, pady=0.05)把這段代碼修改為grid的方法
把这段代码修改为grid的方法如下:
```python
toolbar_frame.grid(row=1, column=1, sticky='ns')
bottom_frame.grid(row=2, column=1, sticky='ew')
tk.Label(toolbar, text="当前选中:").grid(row=1, column=1, padx=5, pady=5, sticky='w')
tk.Label(toolbar).grid(row=1, column=2, padx=5, pady=5, sticky='we')
tk.Label(toolbar).grid(row=1, column=3, padx=5, pady=5, sticky='we')
ttk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(master))).grid(row=1, column=4, padx=5, pady=5, sticky='e')
tk.Label(toolbar).grid(row=1, column=5, padx=5, pady=5, sticky='we')
ttk.Button(toolbar, text="關閉", style='my.TButton', command=self.quit_app).grid(row=1, column=6, padx=5, pady=5, sticky='e')
toolbar.grid_columnconfigure(2, weight=1)
toolbar.grid_columnconfigure(3, weight=1)
self.canvas.grid(row=1, column=1, sticky='nsew')
self.scrollbar.grid(row=1, column=2, sticky='ns')
self.panel_right.grid(row=1, column=3, sticky='nsew')
input_frame.grid(row=1, column=1, sticky='ew', padx=5, pady=5)
self.label1.grid(row=1, column=1, padx=5, pady=5, sticky='w')
self.label2.grid(row=1, column=2, padx=5, pady=5, sticky='w')
self.combobox2.grid(row=1, column=3, padx=5, pady=5, sticky='we')
self.label3.grid(row=1, column=4, padx=5, pady=5, sticky='w')
self.combobox3.grid(row=1, column=5, padx=5, pady=5, sticky='we')
self.label5.grid(row=2, column=1, padx=5, pady=5, sticky='w')
self.combobox5.grid(row=2, column=2, padx=5, pady=5, sticky='we')
self.label6.grid(row=2, column=3, padx=5, pady=5, sticky='w')
self.combobox6.grid(row=2, column=4, padx=5, pady=5, sticky='we')
```
请注意,这只是一个可能的解决方案,具体的排列方式可能需要根据您的需求进行调整。
创建工具栏 toolbar = tk.Frame(master, height=0.2) # 创建样式 style = ttk.Style() style.configure('my.TButton', font=('Arial', 10)) # 设置鼠标悬停时的颜色 style.map('my.TButton', background=[('active', 'blue')]) # 添加占位的 Label tk.Label(toolbar, text="当前选中:").pack(side=tk.LEFT, padx=0.02, pady=0.02) self.selected_label = tk.Label(toolbar, text="", fg="red", font=("Arial", 12)) self.selected_label.pack(side=tk.LEFT, padx=0.02, pady=0.02) tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 添加占位的 Label tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 添加“查询”按钮 ttk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(master))).pack(side=tk.LEFT, padx=5, pady=5, anchor=tk.CENTER) tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True) ttk.Button(toolbar, text="關閉", style='my.TButton', command=self.quit_app).pack(side=tk.LEFT, padx=5, pady=5, anchor=tk.CENTER) # 显示工具栏 toolbar.pack(side=tk.TOP, fill=tk.BOTH)將工具欄中的兩個按鈕放到工具欄的中間
可以使用占位的 Label 来实现将两个按钮放到工具栏的中间。具体地,可以在两个按钮之间分别添加一个占位的 Label,将它们的 fill 属性设置为 BOTH,expand 属性设置为 True,这样它们就会平均地填充工具栏中两个按钮之间的空隙,从而实现将两个按钮放到工具栏的中间。代码如下:
```
# 添加占位的 Label
tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 添加“查询”按钮
ttk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(master))).pack(side=tk.LEFT, padx=5, pady=5, anchor=tk.CENTER)
# 添加占位的 Label
tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 添加“關閉”按钮
ttk.Button(toolbar, text="關閉", style='my.TButton', command=self.quit_app).pack(side=tk.LEFT, padx=5, pady=5, anchor=tk.CENTER)
# 显示工具栏
toolbar.pack(side=tk.TOP, fill=tk.BOTH)
```
阅读全文