Posts: 741
Threads: 122
Joined: Dec 2017
Jun-04-2023, 06:10 AM
(This post was last modified: Jun-04-2023, 06:10 AM by DPaul.)
Hi, a cosmetic question.
I have an app with some entry boxes, nothing special.
entryKl = Entry(frame1, font=myFont,bg='lightblue')
entryKl.pack(side=TOP, padx=10, pady=5)
entryKl.config(state=NORMAL) When I set an entry to "state=DISABLED" it turns into an 'ugly' white,
unlike other widgets, that are dimmed.
How can I keep the color of an entry widget in Disabled state.
thx,
Paul
Edit: found it , there seems to be an option:
entryKl.config(state=DISABLED, disabledbackground='lightblue') !
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 453
Threads: 16
Joined: Jun 2022
You could also code it as: Entry(frame1, font=myFont,bg='lightblue',disabledbackground='lightblue')
Then, when you change the 'state', the background colors are already specified.
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 1,144
Threads: 114
Joined: Sep 2019
another way
import tkinter as tk
def set_state(arg, btn):
if arg['state'] == 'disabled':
arg['state'] = 'normal'
btn['text'] = 'Disable'
else:
arg.delete(0, tk.END)
arg['disabledbackground'] = 'lightgray'
arg['state'] = 'disabled'
btn['text'] = 'Enable'
root = tk.Tk()
root['padx'] = 5
root['pady'] = 5
entry = tk.Entry(root, bg='antiquewhite')
entry.pack(expand=True, fill='x', pady=5)
btn = tk.Button(root, text='Disable', command=lambda: set_state(entry, btn))
btn.pack()
root.mainloop()
Posts: 741
Threads: 122
Joined: Dec 2017
Thanks Menator and Rob,
Fit faber fabricando.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 25
Threads: 0
Joined: Jul 2023
Jul-28-2023, 09:11 AM
(This post was last modified: Jul-28-2023, 09:36 AM by Gribouillis.)
Wehn you set the state of an Entry widget to DISABLED, it becomes non-editable, and by default, it appears in a grayed-out "disabled" state with a white background, which might not look good. To keep the color of an Entry widget in the disabled state, you can change the appearance by setting a specific background color explicitly.
Her is an example how you can do this:-
import tkinter as tk
def set_disabled_state(entry_widget):
entry_widget.configure(state=tk.DISABLED, disabledbackground="lightgray")
# Create the main Tkinter window
root = tk.Tk()
root.title("Customizing Disabled Entry")
# Create an Entry widget
entryKl = tk.Entry(root, font=("Arial", 12), bg='lightblue')
entryKl.pack(side=tk.TOP, padx=10, pady=5)
# Set the Entry to the DISABLED state and customize the disabled background color
set_disabled_state(entryKl)
root.mainloop()
Posts: 6,783
Threads: 20
Joined: Feb 2020
If you want to control the appearance of a widget in it's disabled state, configure the widget when you create it, or use a style.
import tkinter as tk
def toggle_state(widget):
state = tk.DISABLED if widget["state"] != tk.DISABLED else tk.NORMAL
widget.configure(state=state)
root = tk.Tk()
entry = tk.Entry(root, font=("Arial", 12), bg='lightblue')
entry.configure(disabledbackground=entry["bg"], disabledforeground=entry["fg"]) # Look same when disabled.
entry.pack(side=tk.TOP, padx=10, pady=5)
button = tk.Button(root, text="Toggle State", font=("Arial", 24), command=lambda: toggle_state(entry))
button.pack(side=tk.TOP, padx=10, pady=5)
root.mainloop()
|