如何在PySimpleGUI中更新组合框(combobox)的背景颜色?

huangapple go评论58阅读模式
英文:

How to update the background color of a combobox in PySimpleGUI?

问题

我想要能够将组合框的背景更改为红色,以指示用户忘记选择选项。我可以更改文本字段的背景颜色,但无法更改组合框的背景,如下所示,在最小可重现示例中。

import PySimpleGUI as sg

test_layout = [[sg.Combo([1, 2, 3], key="combo"), sg.Text("Name", key="text")]]

window = sg.Window(
    "Test",
    test_layout,
    location=(0, 0),
)

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

    window["combo"].Widget.config(background="red")
    window["text"].Widget.config(background="red")

是否有一种不同的方法来更新组合框的背景颜色?

谢谢你提前!

英文:

I want to be able to change the background of a combobox to red to indicate to the user that they forgot to select an option. I can change the background color of a text field, but not a combobox, as seen below, in the minimum reproducible example.

import PySimpleGUI as sg

test_layout = [[sg.Combo([1, 2, 3], key="combo"), sg.Text("Name", key="text")]]

window = sg.Window(
    "Test",
    test_layout,
    location=(0, 0),
)


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

    window["combo"].Widget.config(background="red")
    window["text"].Widget.config(background="red")

Is there a different way to update the background of the combobox?

Thank you in advance!

答案1

得分: 2

# 在`Combo`元素中需要ttk样式
import PySimpleGUI as sg

sg.set_options(font=("Courier New", 40))

layout = [
    [sg.Combo([1, 2, 3], background_color='blue', text_color='white', key="combo"),
     sg.Text("Name", key="text")],
    [sg.Button('Update')],
]
window = sg.Window('Title', layout, finalize=True)
combo = window['combo']

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'Update':
        combo_style = combo.ttk_style
        style_name  = combo.widget["style"]
        # style_name = combo.ttk_style_name if PySimpleGUI version 4.61.0.160
        combo_style.configure(style_name, foreground='yellow', fieldbackground='red')
window.close()

# 从Github更新到新版本
# 4.61.0.162
# Combo.update新增了一些参数,可以设置文本颜色和背景颜色,字体现在正确应用于下拉列表

# 然后,可以像这样做:
elif event == 'Update':
    window["combo"].update(text_color='yellow', background_color='red')
英文:

For entry field in the Combo element, ttk style required here

import PySimpleGUI as sg

sg.set_options(font=("Courier New", 40))

layout = [
    [sg.Combo([1, 2, 3], background_color='blue', text_color='white', key="combo"),
     sg.Text("Name", key="text")],
    [sg.Button('Update')],
]
window = sg.Window('Title', layout, finalize=True)
combo = window['combo']

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == 'Update':
        combo_style = combo.ttk_style
        style_name  = combo.widget["style"]
        # style_name = combo.ttk_style_name if PySimpleGUI version 4.61.0.160
        combo_style.configure(style_name, foreground='yellow', fieldbackground='red')
window.close()

Update to new version from Github

> 4.61.0.162
>
> Addition of new parms to Combo.update - text color, background color. Also font now applied correctly to dropdown list

Then, you can do it like

    elif event == 'Update':
        window["combo"].update(text_color='yellow', background_color='red')

huangapple
  • 本文由 发表于 2023年3月7日 13:35:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定