创建工具栏 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)將工具欄中的兩個按鈕放到工具欄的中間
时间: 2024-02-21 19:00:04 浏览: 21
可以使用占位的 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)
```
相关问题
# 创建工具栏 toolbar = tk.Frame(master, height=30) # 创建样式 style = ttk.Style() style.configure('my.TButton', font=('Arial', 10)) # 设置鼠标悬停时的颜色 style.map('my.TButton', background=[('active', 'blue')]) # 添加按钮 ttk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(root))).pack( side=tk.LEFT, padx=2, pady=2) ttk.Button(toolbar, text="關閉", style='my.TButton', command=self.quit_app).pack(side=tk.LEFT, padx=2, pady=2) # 显示工具栏 toolbar.pack(side=tk.TOP, fill=tk.X)將這段代碼中兩個按鈕放到中間
以下是将两个按钮放置在工具栏中间的代码:
```
# 创建工具栏
toolbar = tk.Frame(master, height=30)
# 创建样式
style = ttk.Style()
style.configure('my.TButton', font=('Arial', 10))
# 设置鼠标悬停时的颜色
style.map('my.TButton', background=[('active', 'blue')])
# 添加“查询”按钮
ttk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(root))).pack(side=tk.LEFT, padx=2, pady=2)
# 添加占位的 Label
tk.Label(toolbar).pack(side=tk.LEFT, fill=tk.X, expand=True)
# 添加“关闭”按钮
ttk.Button(toolbar, text="關閉", style='my.TButton', command=self.quit_app).pack(side=tk.LEFT, padx=2, pady=2)
# 显示工具栏
toolbar.pack(side=tk.TOP, fill=tk.X)
```
我们添加了一个占位的 Label,使得两个按钮被推到了工具栏的中间位置。
创建工具栏 toolbar = tk.Frame(master, height=30) tk.Button(toolbar, text="查詢", command=lambda: QueryWindow(tk.Toplevel(root))).pack(side=tk.LEFT, padx=2, pady=2) tk.Button(toolbar, text="保存", command=self.save_to_excel).pack(side=tk.LEFT, padx=2, pady=2) tk.Button(toolbar, text="關閉", command=self.quit_app).pack(side=tk.LEFT, padx=2, pady=2) toolbar.pack(side=tk.TOP, fill=tk.X)給這段代碼將這些按鈕添加視覺和顔色效果
您可以使用tkinter的style类来为这些按钮添加视觉和颜色效果。您可以按照以下步骤进行操作:
1.导入ttk模块:`from tkinter import ttk`
2.创建一个ttk主题:`style = ttk.Style()`
3.设置按钮的样式,例如:`style.configure('my.TButton', foreground='blue', background='white', font=('Arial', 10))`
4.将样式应用于按钮:`tk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(root))).pack(side=tk.LEFT, padx=2, pady=2)`
5.重复步骤3和4,为其他按钮添加样式并将其应用。
这样,您就可以为这些按钮添加自定义样式了。
下面是一个完整的示例代码:
```
from tkinter import ttk
# 创建工具栏
toolbar = tk.Frame(master, height=30)
# 创建样式
style = ttk.Style()
style.configure('my.TButton', foreground='blue', background='white', font=('Arial', 10))
# 添加按钮
tk.Button(toolbar, text="查詢", style='my.TButton', command=lambda: QueryWindow(tk.Toplevel(root))).pack(side=tk.LEFT, padx=2, pady=2)
tk.Button(toolbar, text="保存", style='my.TButton', command=self.save_to_excel).pack(side=tk.LEFT, padx=2, pady=2)
tk.Button(toolbar, text="關閉", style='my.TButton', command=self.quit_app).pack(side=tk.LEFT, padx=2, pady=2)
# 显示工具栏
toolbar.pack(side=tk.TOP, fill=tk.X)
```