# 创建工具栏 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)將這段代碼中兩個按鈕放到中間
时间: 2023-12-02 13:05:42 浏览: 25
以下是将两个按钮放置在工具栏中间的代码:
```
# 创建工具栏
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)修改這段代碼,儅用戶運動鼠標到這些按鈕上時這些按鈕有顏色和視覺效果
您可以使用ttk模块中的Button来创建带有鼠标悬停效果的按钮。按照以下步骤进行操作:
1.导入ttk模块:`from tkinter import ttk`
2.创建一个ttk主题:`style = ttk.Style()`
3.设置按钮的样式,包括鼠标悬停时的颜色:`style.map('my.TButton', background=[('active', 'blue')])`
4.将样式应用于按钮:`ttk.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', 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.save_to_excel).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=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)
```