[Tkinter] entry widget - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] entry widget (/thread-40108.html) |
entry widget - DPaul - Jun-04-2023 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') ! RE: entry widget - rob101 - Jun-04-2023 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. RE: entry widget - menator01 - Jun-05-2023 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() RE: entry widget - DPaul - Jun-05-2023 Thanks Menator and Rob, Fit faber fabricando. Paul RE: entry widget - Gaurav_Kumar - Jul-28-2023 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() RE: entry widget - deanhystad - Jul-28-2023 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() |