Python Forum
[Tkinter] entry widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] entry widget
#1
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'.
Reply
#2
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
Reply
#3
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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
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'.
Reply
#5
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()
Gribouillis write Jul-28-2023, 09:36 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#6
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 719 Mar-06-2024, 08:42 PM
Last Post: russellm44
  Tkinter Exit Code based on Entry Widget Nu2Python 6 3,020 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,268 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  Entry Widget issue PA3040 16 6,881 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] password with Entry widget TAREKYANGUI 9 6,014 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,432 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to retreive the grid location of an Entry widget kenwatts275 7 4,643 Apr-24-2020, 11:39 PM
Last Post: Larz60+
  POPUP on widget Entry taratata2020 4 3,774 Mar-10-2020, 05:04 PM
Last Post: taratata2020
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,483 Jan-23-2020, 09:00 PM
Last Post: HBH
  The coordinates of the Entry widget (canvas) when moving berckut72 8 5,968 Jan-07-2020, 09:26 AM
Last Post: berckut72

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020