Skip to content

Need ability to change style of TEntry #155

Open
@kevin-partin

Description

@kevin-partin

Love your theme.

I need to be able to change the fieldbackground and foreground colors based upon data values. The following code works fine using 'clam' as the theme, but does not work with SV light. Using Fedora Workstation 41 and Gnome Shell 47.4. Any help is appreciated.

import time

import tkinter as tk
import tkinter.ttk as ttk

import sv_ttk

def dosomething():

    count = 2
    while True:
        try:
            if count == 1:
                entry.configure(style='TEntry')
                text  = 'Nominal'
                count = 2
            elif count == 2:
                entry.configure(style='Warning.TEntry')
                text  = 'Warning'
                count = 3
            elif count == 3:
                entry.configure(style='Bad.TEntry')
                text  = 'Bad'
                count = 1
                
            variable.set(text)
            entry.update()
            time.sleep(2)
        except KeyboardInterrupt:
            break

# Create Object
root = tk.Tk()

style = ttk.Style()
#sv_ttk.set_theme('light')
style.theme_use('clam')

variable = tk.StringVar(master=root)

style.configure('Warning.TEntry',
    fieldbackground = 'yellow',
    foreground      = 'blue',
    based_on        = 'TEntry',
)

style.configure('Bad.TEntry',
    fieldbackground = 'red',
    foreground      = 'black',
    based_on        = 'TEntry',
)

# Add Title
root.title('TEntry Demonstration')

# Add Geometry
root.geometry('500x300')

# Create Label
entry = ttk.Entry(
    master  = root,
    justify = tk.CENTER,
    style   = 'TEntry',
    state   = 'readonly',
    textvariable = variable,
)
entry.pack(pady=50)

variable.set('Nominal')

root.update

root.after(2000, dosomething)

root.mainloop()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions