Pysimplegui: [Enhancement] sg.Input color change on fly

Created on 27 Oct 2019  路  2Comments  路  Source: PySimpleGUI/PySimpleGUI

Enhancement

Hi.

Idea: change bockground and foreground color in sg.Input element with .Update() method, by adding 2 key-word parameters bg and fg.

Operating System

All

Python version

3

PySimpleGUI Port and Version

Latest

Your Experience Levels In Months or Years

_____( 汀掳 蜏蕱 汀掳)____ Python programming experience
_____( 汀掳 蜏蕱 汀掳)____ Programming experience overall
_____yes____ Have used another Python GUI Framework (tkiner, Qt, etc) previously (yes/no is fine)?

You have completed these steps:

  • [x] Read instructions on how to file an Issue
  • [x] Searched through main docs http://www.PySimpleGUI.org for your problem
  • [x] Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [x] Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
  • [x] Note that there are also Demo Programs under each port on GitHub
  • [x] Run your program outside of your debugger (from a command line)
  • [x] Searched through Issues (open and closed) to see if already reported

Code or partial code causing the problem

MWE (minimal working example):

import PySimpleGUI as sg, random

window = sg.Window('Test this', [
    [sg.I(key='-name-', change_submits=True)]
])

while True:
    event, values = window.read()

    if event in (None, 'Exit'): break

    if event == '-name-':
        bg_color = random.choice(['white', 'orange', 'green'])
        fg_color = random.choice(['red', 'pink', 'blue'])
        window['-name-'].update(bg=bg_color,
                                fg=fg_color)

window.close()

Code for PySimpleGUI.py wtih added __docstring__ (two params's guys)

    def Update(self, value=None, disabled=None, select=None, visible=None, move_cursor_to='end', bg=None, fg=None):
        """
        Changes some of the settings for the Input Element. Must call `Window.Read` or `Window.Finalize` prior

        :param value: (str) new text to display as default text in Input field
        :param disabled: (bool) disable or enable state of the element (sets Entry Widget to readonly or normal)
        :param select: (bool) if True, then the text will be selected
        :param visible: (bool) change visibility of element
        :param move_cursor_to: Union[int, str] Moves the cursor to a particular offset. Defaults to 'end'
        :param bg: (str) color of background text
        :param fg: (str) color of foreground text
        """
        if self.Widget is None:
            warnings.warn('You cannot Update element with key = {} until the window has been Read or Finalized'.format(self.Key), UserWarning)
            return
        if bg != None:
            self.TKEntry.configure(background=bg)
        if fg != None:
            self.TKEntry.configure(foreground=fg)
        if disabled is True:
            self.TKEntry['state'] = 'readonly'
        elif disabled is False:
            self.TKEntry['state'] = 'normal'
        if value is not None:
            try:
                self.TKStringVar.set(value)
            except:
                pass
            self.DefaultText = value
            if move_cursor_to == 'end':
                self.TKEntry.icursor(tk.END)
            elif move_cursor_to is not None:
                self.TKEntry.icursor(move_cursor_to)
        if select:
            self.TKEntry.select_range(0, 'end')
        if visible is False:
            self.TKEntry.pack_forget()
        elif visible is True:
            self.TKEntry.pack()


Picture

testing

Cross-port Needed Done - Download from GitHub enhancement

Most helpful comment

The Multiline Element has parameters for changing the background and the text color. Rather than fg and bg, terms used by tkinter, I used text_color and background_color in the multiline Update method.

Here is that definition:

    def Update(self, value=None, disabled=None, append=False, font=None, text_color=None, background_color=None,  visible=None, autoscroll=None):

The code from that update looks essentially the same as what's proposed above, which is an excellent confirmation that we're on the same page 馃憤馃憤

        if background_color is not None:
            self.TKText.configure(background=background_color)
        if text_color is not None:
            self.TKText.configure(fg=text_color)

And, WOW you file amazing Issues! Animated GIF?? Are you kidding me?

Since this is such an easy change, I'll drop it in and pop it up on GitHub.

All 2 comments

The Multiline Element has parameters for changing the background and the text color. Rather than fg and bg, terms used by tkinter, I used text_color and background_color in the multiline Update method.

Here is that definition:

    def Update(self, value=None, disabled=None, append=False, font=None, text_color=None, background_color=None,  visible=None, autoscroll=None):

The code from that update looks essentially the same as what's proposed above, which is an excellent confirmation that we're on the same page 馃憤馃憤

        if background_color is not None:
            self.TKText.configure(background=background_color)
        if text_color is not None:
            self.TKText.configure(fg=text_color)

And, WOW you file amazing Issues! Animated GIF?? Are you kidding me?

Since this is such an easy change, I'll drop it in and pop it up on GitHub.

I updated the source code - it works good!

1forgif

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ihouses picture ihouses  路  6Comments

xuguojun168 picture xuguojun168  路  3Comments

mozesa picture mozesa  路  4Comments

MikeTheWatchGuy picture MikeTheWatchGuy  路  6Comments

DKatarakis picture DKatarakis  路  6Comments