Greatings! I'm trying to do a simple calculator with Tkinter, what I want is that when I press a button it does not change relief and keeps being FLAT
. The problem is that it keeps changing to GROOVE
.
Here is the code line:
button1 = tk.Button(self, text="1", command=lambda: self.config(relief=FLAT), relief=FLAT,
font=myfont, bg="ghost white", activebackground="LightSteelBlue2")
I would appreciate the help.
EDIT
What I want the button to do is something like this, like the buttons of the Windows Calculator
You can use a label if you want it to stay flat even when being pressed. Thought I am not sure why you do not want some visual q to show it has been clicked.
Here is an example using A button and a Label. Notice the label works like a button when we use bind()
but without visually changing.
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()
Update:
Based on your comments below "I want the button to change its color when is pressed and when is unpressed it returns to its normal state." this is how I would get the functionality you are looking for.
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()