致敬!我正在尝试用 Tkinter 做一个简单的计算器,我想要的是,当我按下按钮时,它不会改变浮雕并保持
FLAT
。问题是它不断地变成GROOVE
。
这是代码行:
button1 = tk.Button(self, text="1", command=lambda: self.config(relief=FLAT), relief=FLAT,
font=myfont, bg="ghost white", activebackground="LightSteelBlue2")
我将不胜感激。
编辑
如果您希望即使在受压时也能保持平整,您可以使用标签。我想我不知道为什么你不想要一些视觉q来显示它已被点击。
这是使用按钮和标签的示例。请注意,当我们使用
bind()
时,标签就像一个按钮,但视觉上没有改变。
import tkinter as tk
def do_something(value):
print(value)
root = tk.Tk()
# Normal button behavior.
tk.Button(root, text="Button 1", relief='flat', bg="ghost white",
activebackground="LightSteelBlue2",
command=lambda: do_something("Button 1")).pack()
# Is a button but does not change visuals when clicked due to state='disabled' and bind combination.
# Downside the text is greyed out.
button_2 = tk.Button(root, text="Button 2", relief='flat', bg="ghost white",
activebackground="LightSteelBlue2", state='disabled')
button_2.pack()
button_2.bind("<Button-1>", lambda e: do_something("Button 2"))
# Is a label but works like a button due to bind.
# This method should fit your needs.
label_1 = tk.Label(root, text="Label 1", bg="ghost white", activebackground="LightSteelBlue2")
label_1.pack()
label_1.bind("<Button-1>", lambda e: do_something("Label 1"))
root.mainloop()
更新:
根据您下面的评论“我希望按钮在按下时改变颜色,松开时返回正常状态。”这就是我获得您正在寻找的功能的方式。
import tkinter as tk
def change_color(event):
event.widget.config(bg='green')
entry.insert('end', event.widget['text'])
def change_back(event):
event.widget.config(bg='ghost white')
root = tk.Tk()
num_pad = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
entry = tk.Entry(root, width=50)
entry.grid(row=0, column=0)
pad_frame = tk.Frame(root)
pad_frame.grid(row=1, column=0)
# Is a label but works like a button due to bind.
# This method should fit your needs.
for ndex, sub_list in enumerate(num_pad):
for sub_ndex, sub_value in enumerate(sub_list):
lbl = tk.Label(pad_frame, text=sub_value, bg="ghost white", activebackground="LightSteelBlue2",
height=2, width=3)
lbl.grid(row=ndex, column=sub_ndex, padx=2, pady=2)
lbl.bind("<Button-1>", change_color)
lbl.bind("<ButtonRelease-1>", change_back)
lbl.bind("<Leave>", change_back)
root.mainloop()
这可能看起来很麻烦,但您可以通过 ttk 使用
ttk.Style()
来完成此操作。
您可以将大部分按钮设置逻辑保留在
style.config
中,并且可以使用 style.map
为一个或多个样式的配置选项指定特定于状态的变体
import tkinter as tk
from tkinter import ttk
# You can put this in function and call it when your main app initializes.
def configure_styles():
style = ttk.Style()
# General configuration
style.configure(
'MyButton.TButton',
font=('Helvetica', 12, 'bold'),
background='ghost white',
padding=6,
anchor='center',
)
# State-specific variations
style.map(
'MyButton.TButton',
background=[
('active', '!pressed', 'ghost white'),
('active', 'pressed', 'LightSteelBlue2'), # Change colors when pressed.
],
relief=[
('pressed', tk.FLAT), # Stay flat when pressed
('!pressed', tk.FLAT),
],
)
在主应用程序中,您可以访问您创建的样式,并通过 style 参数将其应用到任意数量的按钮。
在示例中,我们为 Button
'.TButton'
类型创建了一个样式,并将其命名为 'MyButton'
button = ttk.Button(self, text="1", style="MyButton.TButton", command=lambda: self.do_stuff)
button.pack(pady=10)
您的主应用程序可能看起来像这样:
class MainApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("Styled ttk Widgets App")
self.geometry("400x200")
self.config(background='ghost white')
configure_styles() # Initialize the styles
button = ttk.Button(self, text="1", style="MyButton.TButton", command=lambda: self.do_stuff)
button.pack(pady=10)
def do_stuff(self):
print("Doing stuff!!1")
if __name__ == "__main__":
app = MainApp()
app.mainloop()
您可以在官方 Tk 文档中阅读有关 TTk:样式 的更多信息。