style1 = ttk.Style() style1.configure('my.TButton1', font=('Courier New', 5)) Button2 = ttk.Button(self.initface, text='返回', command=lambda: self.change1(), width=18, style='my.TButton1') Button2.place(x=0, y=0)
时间: 2024-04-01 20:35:22 浏览: 7
根据你提供的代码,出现这个错误的原因可能是你在使用ttk样式配置时,没有正确地定义“my.TButton1”这个样式。请确保你已经正确地定义了这个样式,并且你的代码可以正确地访问它。
另外,请注意,如果你正在使用place布局管理器,你需要确保你的按钮的位置是正确的。在上面的代码中,你将按钮的位置设置为(0,0),这意味着它可能会与其他组件重叠或被隐藏。你需要根据你的实际需求调整按钮的位置。
相关问题
# 创建工具栏 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,使得两个按钮被推到了工具栏的中间位置。
创建样式 style = ttk.Style() style.configure('my.TButton', font=('Arial', 10)) # 设置鼠标悬停时的颜色 style.map('my.TButton', background=[('active', 'blue')]) self.button = tk.Button(input_frame, text="保存", style='my.TButton', command=self.save_to_excel).pack(side=tk.LEFT, padx=5, pady=5)報這個self.tk.call( _tkinter.TclError: unknown option "-style"怎麽修改
这个错误可能是因为您正在使用的 Tkinter 版本不支持 ttk 样式。您可以尝试在创建窗口时导入 ttk 并使用 ttk.Button 代替 tk.Button,这样就可以使用 ttk 样式了。以下是一个示例代码:
```
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
style = ttk.Style()
style.configure('my.TButton', font=('Arial', 10))
style.map('my.TButton', background=[('active', 'blue')])
button = ttk.Button(root, text="保存", style='my.TButton', command=lambda: print("保存"))
button.pack(side=tk.LEFT, padx=5, pady=5)
root.mainloop()
```
如果您仍然想使用 tk.Button 并且使用 ttk 样式,您可以尝试安装更新的 Tkinter 版本或升级您的 Python 版本。