PySimpleGUI / PySimpleGUI

Python GUIs for Humans! PySimpleGUI is the top-rated Python application development environment. Launched in 2018 and actively developed, maintained, and supported in 2024. Transforms tkinter, Qt, WxPython, and Remi into a simple, intuitive, and fun experience for both hobbyists and expert users.
https://www.PySimpleGUI.com
Other
13.44k stars 1.83k forks source link

[ Enhancement] Update text_color for radio buttons (like checkboxes have). #3382

Closed tehguitarist closed 1 year ago

tehguitarist commented 4 years ago

Type of Issues: Enhancement

Operating System All

Python version 3.6.8

PySimpleGUI Port and Version

Ports = tkinter

PySimpleGUI Version: 4.29.0

tkinter version 8.6.9:

Your Experience Levels In Months or Years

6 months of python, smattering of programming here and there over 10 years.

You have completed these steps:

[ Y ] Read instructions on how to file an Issue [ Y ] Searched through main docs http://www.PySimpleGUI.org for your problem [ Y ] Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi) [ Y ] Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com [ Y ] Note that there are also Demo Programs under each port on GitHub [ Y ] Run your program outside of your debugger (from a command line) [ Y ] Searched through Issues (open and closed) to see if already reported [ Y ] Try again by upgrading your PySimpleGUI.py file to use the current one on GitHub. Your problem may have already been fixed but is not yet on PyPI.

Description of Problem / Question / Details

Checkboxes have text color as an update option, would be great to have this on radio buttons too!

Code To Duplicate

import PySimpleGUI as sg

layout = [[sg.Radio('Turn Red', 'group', key = '-R1-'), sg.Radio('Stay White', 'group', key = '-R2-')],
          [sg.Button('Go')]]

window = sg.Window('Test', layout)

while True:
    event, values = window.Read()
    if event == sg.WIN_CLOSED:
        break
    elif event == 'Go':
        window['-R1-'].update(text_color = 'Red’)

window.close()
PySimpleGUI commented 4 years ago

While I'm working on the addition, you can use the element.Widget member variable to gain access to the tkinter widget and directly manipulate the colors.

Here's a little demo program for you that shows the 2 statements you'll need to set the foreground and background colors...

import PySimpleGUI as sg

layout = [  [sg.Text('My Window')],
            [sg.Radio('Radio', 1, k='-RADIO1-'), sg.Radio('Radio', 1, k='-RADIO2-')],
            [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Window Title', layout)

while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Go':
        window['-RADIO1-'].Widget.config(fg='red')
        window['-RADIO1-'].Widget.config(bg='yellow')
window.close()
PySimpleGUI commented 4 years ago

Okey dokey.... your change is all done and checked into GitHub in version 4.29.0.12.

Here's a test harness that demonstrates usage:

import PySimpleGUI as sg

layout = [  [sg.Text('My Window')],
            [sg.Radio('Radio', 1, k='-RADIO1-'), sg.Radio('Radio', 1, k='-RADIO2-')],
            [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Window Title', layout)

while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Go':
        window['-RADIO1-'].update(value=True, background_color='#83142c', text_color='#f9d276')
window.close()

Initially the window looks like this:

image

After pressing "Go" it changes to this:

image

I chose the colors in the example from the Dark Red theme so that you can see how the circle portion is colored differently than the background. This follows the same algorithm as the checkbox element. If a hex value is specified for both the text and background colors, then the circle color is computed from them. The reason for this is that windows looked really "flat" before because the background color of the window was identical to the checkbox and the radio button colors.

If you look carefully, you'll see that the circle colors for the radio buttons (squares for the checkbox) are ever so slightly darker than the background.

Lemme know if you have any questions or run into a problem.

RobertoZaffa commented 1 year ago

Hello, this works also for me but i need to change the color as soon the radio choice is done and not after the event generated by click a button. To solve, I think would be possible to tests values[] without an event is generated. Thanks. image

PySimpleGUI commented 1 year ago

If you need your window to be more "responsive", for example, have something happen when a Radio element is clicked, then you'll want to enable events for the Radio element. Buttons clearly generate events.... and so can nearly all other elements. Set the enable_events parameter when you make your layout.

Here's one way to do it

import PySimpleGUI as sg

radio_keys = ('-R1-', '-R2-', '-R3-')
layout = [[sg.Radio('Radio 1', 1, key='-R1-', enable_events=True), sg.Radio('Radio 2', 1, key='-R2-', enable_events=True), sg.Radio('Radio 3', 1, key='-R3-', enable_events=True)],
          [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Radio Text Color', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event in radio_keys:
        # Reset all Radio text to original color
        for key in radio_keys:
            window[key].update(text_color=sg.theme_element_text_color())
        # Set the Radio button text selected to red
        window[event].update(text_color='yellow')

window.close()

python_o4j6i3iVsB

RobertoZaffa commented 1 year ago

Perfect!! Tanks a lot. I've just started using PySimpleGUI but I'm already appreciating the power and simplicity. image

PySimpleGUI commented 1 year ago

I've just started using PySimpleGUI but I'm already appreciating the power and simplicity.

What a great message to start the day with.

image

Please please post that screenshot over in Issue #10!

I image it and I'm sure others will too. Thrilled your enjoying and making progress!